Ask Your Question
-1

The cropped roi image is just tilted.I wanted it to be tilted to correct rectangular axes.What method should be used to do.Pls help me with a solution

asked 2017-03-23 00:46:12 -0600

Ashiq KS gravatar image

image description

edit retag flag offensive close merge delete

Comments

do you mean rotate?

jsxyhelu gravatar imagejsxyhelu ( 2017-03-23 05:35:27 -0600 )edit

Yes I want it to be rotated

Ashiq KS gravatar imageAshiq KS ( 2017-03-23 09:08:53 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-03-24 08:48:41 -0600

updated 2017-03-24 08:50:13 -0600

Once you have your contour, you could apply the following steps

  • Find the bounding rotated rectangle of the contour
  • Use the angle parameter to warp back the image

Some code that could do this

/// Get the RotatedRect associated with the contour
RotatedRect rect = minAreaRect(contour);
/// Also select the corner points for rotating back
Point2f pts[4]; rect.points(pts);
/// Define the angle of rotation and rotate the selection to an upright position
double angle = 0.0;
if((int)rect.angle < -45){
   angle = fmod((rect.angle + 90), 90);
}else{
   angle = (int)rect.angle;
}
/// Warp back based on the angle
Mat r = getRotationMatrix2D(Point2f(img.cols/2, image.rows/2), angle, 1.0);
warpAffine(image.clone(), image, r, cv::Size(image.cols, image.rows));
// Finally warp the corner points from the original image using same matrix to obtain the mask to be cut out
Mat coordinates = (Mat_<double>(3,4) << pts_original[0].x, pts_original[1].x, pts_original[2].x, pts_original[3].x ,pts_original[0].y, pts_original[1].y, pts_original[2].y, pts_original[3].y, 1   , 1  ,  1   , 1    );
Mat result = r * coordinates;
Point p1_back, p2_back, p3_back, p4_back;
vector<Point> collection;
p1_back.x=(int)result.at<double>(0,0); p1_back.y=(int)result.at<double>(1,0);
p2_back.x=(int)result.at<double>(0,1); p2_back.y=(int)result.at<double>(1,1);
p3_back.x=(int)result.at<double>(0,2); p3_back.y=(int)result.at<double>(1,2);
p4_back.x=(int)result.at<double>(0,3); p4_back.y=(int)result.at<double>(1,3);

This above code in snipped from some larger code and might need some adaptations (larger final canvas). But I will not do all the work for you :D

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-23 00:46:12 -0600

Seen: 601 times

Last updated: Mar 24 '17