3

I've been trying to make this program in c++ with opencv that converts the image to greyscale and rotates the image afterwards, but the output I get is all kinds of messed up.

I have been searching for solutions and looking for help everywhere, but I haven't been able to find out what the heck I have done wrong so if any of you could help me it'd be great

Code: http://pastebin.com/FSJKyaeU

Also, here's a picture of the output I get https://i.stack.imgur.com/uMGuJ.jpg

2 Answers2

2

Please replace:

Mat imRotate(im.cols * 2, im.rows * 2, im.type());

to use this one:

  int size = static_cast<int>(sqrt(im.cols * im.cols/4 + im.rows * im.rows/4) * 2) + 1;
  Mat imRotate(size, size , im.type());

Then update FuncRotate. I think you need to do something like this:

    void FuncRotate(Mat im, Mat imRotate, int q, int x, int y, int rows, int columns) 
    {
      double radians = (q * 3.1415)/180; // or try use M_PI instead of 3.1415
      double cosr = cos(radians);
      double sinr = sin(radians);

      for(int i=0; i<columns; i++) //columns of the original image
      {
         for(int j=0; j<rows; j++) //rows of the original image
         {
            int NewXPixel = imRotate.cols/2 + ((i-x) * cosr) - ((j-y) * sinr);
            int NewYPixel = imRotate.rows/2 + ((i-x) * sinr) + ((j-y) * cosr);
            if(NewXPixel < 0 || NewYPixel < 0 || NewXPixel >= imRotate.cols || NewYPixel >= imRotate.rows)
    continue;
  imRotate.at<unsigned char>(NewYPixel,NewXPixel) = im.at<unsigned char>(j,i);
         }
      }
    }
  • I think the idea was to rotate around the x and y that are given as arguments (see how they are read in main), so I believe you could use them instead of the center position. – Reunanen Oct 21 '12 at 19:03
  • I tried putting in your solution for `Mat imRotate(im.cols * 2, im.rows * 2, im.type());` and for some reason it makes my program crash – user1755692 Oct 21 '12 at 19:42
  • I updated code. Please apply this changes. Please note, src image rotates around x,y, but it places at center of your new image, and depending on x,y, and angle part of image can be trubcated. – Vladyslav Litunovsky Oct 21 '12 at 22:02
  • Can you explain what you did to FuncRotate? – user1755692 Oct 22 '12 at 01:16
  • Here is the topic that described rotation: [link](http://stackoverflow.com/questions/7953316/rotate-a-point-around-a-point-with-opencv?rq=1) I added only part that uses center of new image (imRotate.cols/2 and imRotate.rows/2) as rotate point (.. x,y ..) you pass in function. – Vladyslav Litunovsky Oct 22 '12 at 13:31
0

I think at least there is something wrong on this line:

imRotate.at<unsigned char>(i,j) = im.at<unsigned char>(NewXPixel,NewYPixel);

... because i and j are supposed to loop over the original image, right?

Perhaps you meant something like:

imRotate.at<unsigned char>(NewXPixel,NewYPixel) = im.at<unsigned char>(i,j);

Also, x and y are not used in FuncRotate.

Moreover, I believe you should initialize imRotate to zero.

Reunanen
  • 7,921
  • 2
  • 35
  • 57
  • Sorry, but i'm not so experienced in coding language. What exactly do you mean about initializing `imRotate` – user1755692 Oct 21 '12 at 18:12
  • For instance, you could loop i and j over rows and columns - as you do in FuncRotate - and then: imRotate.at(i,j) = 0; – Reunanen Oct 21 '12 at 19:00