简体   繁体   中英

Program to convert image and rotate

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 http://i.imgur.com/qpYm1.jpg

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM