I have rotated an image and now I want to crop it so that the borders are cut out and the full cropped image is viewable. I tried a method I found on the net but it didn't work and to be honest I don't fully understand what it was trying to do. I understand the rotating part. What points is he pushing to points? Is it the four corners of the image? As his method of pushing those points does not work for me. I used the four corners of the image instead which worked for rotating but when I try to run getRectSubPix an exception occurs probably because my box is wrong but I'm not sure what getRectSubPix needs then?
Here is the output I get after running this code. Clearly the output is iverted (Why does he do this is it important?) and also the crop doesn't work.
cv::bitwise_not(img, img);
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = img.begin<uchar>();
cv::Mat_<uchar>::iterator end = img.end<uchar>();
for (; it != end; ++it)
if (*it)
points.push_back(it.pos());
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
cv::Mat rot_mat = cv::getRotationMatrix2D(box.center, angle, 1);
cv::Mat rotated;
cv::warpAffine(img, rotated, rot_mat, img.size(), cv::INTER_CUBIC);
cv::Size box_size = box.size;
if (box.angle < -45.)
std::swap(box_size.width, box_size.height);
cv::Mat cropped;
cv::getRectSubPix(rotated, box_size, box.center, cropped);