0

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.

after running code this is what I get

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);
OneTwo
  • 2,291
  • 6
  • 33
  • 55
  • Sorry but I don't understand what is going wrong and I'm on the tablet so cannot try your code. Can you upload some images on what are you trying to achieve, what are you getting and a complete test example? – nkint Mar 25 '14 at 21:23
  • I'm trying to rotate an image. I followed [this](http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/) tutorial to do this, although the points.push_back threw an exception. So instead I rotated the image without calculating the box. But I can't crop the image probably because instead of the tutorials points.push_back I used the four points (0, 0),(0,img.cols)... But this throws an exception when I try to run getRectSubPix and when I use your (roi) other method you advised. I don't understand why box.size isn't just the original size of the original un-rotated image? Why use boxes? – OneTwo Mar 25 '14 at 21:41
  • @nkint Have you read below comments in that article. – Haris Mar 26 '14 at 03:39
  • @Haris yes but I don't know what could it be. Do you have any ideas? – nkint Mar 26 '14 at 08:36
  • @Haris probably you should ask to the OP, not to me. I do not understand what the problem of the OP is, and he is following the http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/ tutorial that is a really good one and the code in that tutorial is perfectly working. – nkint Mar 26 '14 at 10:56
  • @nkint Ok, I am relay sorry, I just mistaken on that. – Haris Mar 26 '14 at 11:00
  • When I run the code above and show img it comes out inverted. I know why but why does he invert it? Secondly the cropped image crops it wrong and often I can't even see the image at all just black. (Unless the angle is small) – OneTwo Mar 26 '14 at 11:27
  • I have added a picture of what I get after running the code. – OneTwo Mar 26 '14 at 11:33
  • 1
    You could see the answer here http://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c/22042434#22042434 , probably your are looking for this. – Haris Mar 26 '14 at 12:24

0 Answers0