Once you have your contour, you could apply the following steps
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
Asked: 2017-03-23 00:46:12 -0600
Seen: 589 times
Last updated: Mar 24 '17
OpenCV DescriptorMatcher matches
Conversion between IplImage and MxArray
Video On Label OpenCV Qt :: hide cvNamedWindows
Problems using the math.h class with OpenCV (c++, VS2012)
How to reduce false positives for face detection
Area of a single pixel object in OpenCV
build problems for android_binary_package - Eclipse Indigo, Ubuntu 12.04
Can't compile .cu file when including opencv.hpp
Using OpenCV's stitching module, strange error when compositing images
do you mean rotate?
Yes I want it to be rotated