【问题标题】:Program to convert image and rotate转换图像和旋转的程序
【发布时间】:2012-10-11 14:07:29
【问题描述】:

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

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

代码: http://pastebin.com/FSJKyaeU

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

【问题讨论】:

标签: c++ opencv rotation grayscale


【解决方案1】:

请替换:

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

【讨论】:

  • 我认为这个想法是围绕作为参数给出的 x 和 y 旋转(看看它们是如何在 main 中读取的),所以我相信你可以使用它们而不是中心位置。跨度>
  • 我尝试为Mat imRotate(im.cols * 2, im.rows * 2, im.type()); 加入你的解决方案,但由于某种原因它使我的程序崩溃
  • 我更新了代码。请应用此更改。请注意,src 图像围绕 x,y 旋转,但它位于新图像的中心,并且根据 x,y 和角度部分图像可以被截断。
  • 你能解释一下你对 FuncRotate 做了什么吗?
  • 这里是描述旋转的主题:link 我只添加了使用新图像中心(imRotate.cols/2 和 imRotate.rows/2)作为旋转点的部分(..x, y ..) 你传入函数。
【解决方案2】:

我认为这行至少有问题:

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

...因为i 和j 应该循环遍历原始图像,对吧?

也许你的意思是这样的:

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

另外,x 和 y 未在 FuncRotate 中使用。

此外,我认为您应该将imRotate 初始化为零。

【讨论】:

  • 对不起,我在编码语言方面不是很有经验。你到底是什么意思初始化imRotate
  • 例如,您可以在行和列上循环 i 和 j - 就像在 FuncRotate 中一样 - 然后: imRotate.at(i,j) = 0;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多