繁体   English   中英

用于转换图像和旋转的程序

[英]Program to convert image and rotate

我一直在尝试使用opencv在c ++中创建这个程序,将图像转换为灰度并随后旋转图像,但我得到的输出是各种混乱。

我一直在寻找解决方案,并在各处寻求帮助,但我无法找出我做错了什么,所以如果你们中的任何人能帮助我,那就太棒了

代码: http//pastebin.com/FSJKyaeU

另外,这是我得到的输出图片http://i.imgur.com/qpYm1.jpg

请替换:

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

使用这个:

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

然后更新FuncRotate。 我想你需要做这样的事情:

    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);
         }
      }
    }

我认为至少这一行有问题:

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

...因为ij应该在原始图像上循环,对吗?

也许你的意思是:

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

此外,在FuncRotate中不使用xy

而且,我相信你应该将imRotate初始化为零。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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