2017-02-13 4 views
1

を私はCVの新しい位置を取得したい:: RECT(ROI)次のコードを使用して画像を回転させた後:品種の新しい位置を取得する:: warpAffine(OpenCVの、C++)による回転画像の後RECT

cv::Point2f center(image.cols/2.0, image.rows/2.0); 

cv::Rect ROI = cv::Rect(100,200,50,100); 

cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1.0); 
cv::Rect bbox = cv::RotatedRect(center,image.size(), angle).boundingRect(); 

rot.at<double>(0,2) += bbox.width/2.0 - center.x; 
rot.at<double>(1,2) += bbox.height/2.0 - center.y; 


cv::warpAffine(image, image, rot, bbox.size(),cv::INTER_LINEAR,cv::BORDER_CONSTANT, 
       cv::Scalar(255, 255, 255)); 

どうすればいいですか?次の関数を使用してその角のそれぞれを変換する必要がCV :: RECT(ROI)の新しい位置を取得するために

答えて

3

回転行列があるので、cv::transform関数を使用してROI矩形を回転できます。まず、その長方形の点の配列が必要です。

vector<Point2f> roi_points = { 
     {roi.x,    roi.y}, 
     {roi.x + roi.width, roi.y}, 
     {roi.x + roi.width, roi.y + roi.height}, 
     {roi.x,    roi.y + roi.height} 
}; 

その後、あなたはcv::transformを使用することができます。

vector<Point2f> rot_roi_points; 
transform(roi_points, rot_roi_points, rot); 

この方法は、rot_roi_pointsは、変換された矩形のポイントを保持しています。私は、次のようにあなたの関数を変更し

enter image description here ==> enter image description here

2

は:

cv::Point2f Convert(const cv::Point2f & p, const cv::Mat & t) 
{ 
    float x = p.x*t.at<double>((0, 0) + p.y*t.at<double>((0, 1) + t.at<double>((0, 2); 
    float y = p.x*t.at<double>((1, 0) + p.y*t.at<double>((1, 1) + t.at<double>((1, 2); 
    return cv::Point2f(x, y); 
} 

変換行列を使用するために使用されるものと同じです画像回転。

+0

感謝: CV :: Point2fは(constのCV :: Point2f&P、constのCV ::マット&T)に変換 { float x = px * t.at (0、0)+ py * t.at (0,1)+ t.at (0,2); float y = p.x * t.at (1,0)+ p.y * t.at (1,1)+ t.at (1,2); return cv :: Point2f(x、y); } この関数を次のように呼び出します。 cv :: Point2f c1 = Convert(cv :: Point2f(ROI.x、ROI.y)、rot); しかし、私は非常に大きな浮動小数点値を得ます(exp:-3.99357e + 15)。なにが問題ですか ? –

+0

変換行列にはdouble型があるためです。 – ErmIg

関連する問題