【问题标题】:OpenCV: output image is blueOpenCV:输出图像为蓝色
【发布时间】:2020-05-17 14:33:21
【问题描述】:

所以我正在制作这个项目,我在 OpenCV 上制作图像的反射(不使用翻转功能),完成它的唯一问题(我认为)是假设图像出来反映,出来是蓝色的。

我的代码(我拿出了常用的部分,问题应该在这里):

Mat imageReflectionFinal = Mat::zeros(Size(220,220),CV_8UC3);

for(unsigned int r=0; r<221; r++)
    for(unsigned int c=0; c<221; c++) {
       Vec3b intensity = image.at<Vec3b>(r,c);
       imageReflectionFinal.at<Vec3b>(r,c) = (uchar)(c, -r + (220)/2);
    }

    ///displays images
    imshow( "Original Image", image );
    imshow("Reflected Image", imageReflectionFinal);
    waitKey(0);
    return 0;
}

【问题讨论】:

  • 在两个 for 循环中也有一个错误,这将导致您访问超过像素缓冲区末尾的内存。 Size(220,220) 表示最大列(以及行)数是 219,而不是 220。
  • 你希望(uchar)(c, -r + (220)/2) 做什么?

标签: c++ image opencv opencv3.0


【解决方案1】:

您的代码存在一些问题。正如所指出的,您的迭代变量超出了实际的图像尺寸。不要使用硬编码的边界,您可以使用inputImage.colsinputImage.rows 来获取图像尺寸。

有一个已设置但未使用的变量(BGR Vec3b) - Vec3b intensity = image.at&lt;Vec3b&gt;(r,c);

最重要的是,目前尚不清楚您要达到的目标。 (uchar)(c, -r + (220)/2); 行没有提供太多信息。另外,您将原始图像翻转到哪个方向? X 轴还是 Y 轴?

以下是在 X 方向翻转图像的可能解决方案:

//get input image:
cv::Mat testMat = cv::imread( "lena.png" );

//Get the input image size:
int matCols = testMat.cols;
int matRows = testMat.rows;

//prepare the output image:
cv::Mat imageReflectionFinal = cv::Mat::zeros( testMat.size(), testMat.type() );

//the image will be flipped around the x axis, so the "target"
//row will start at the last row of the input image:
int targetRow = matRows-1;

//loop thru the original image, getting the current pixel value:
for( int r = 0; r < matRows; r++ ){
    for( int c = 0; c < matCols; c++ ) {
        //get the source pixel:
        cv::Vec3b sourcePixel = testMat.at<cv::Vec3b>( r , c );
        //source and target columns are the same:
        int targetCol = c;
        //set the target pixel
        imageReflectionFinal.at<cv::Vec3b>( targetRow , targetCol ) = sourcePixel;
    }
    //for every iterated source row, decrease the number of
    //target rows, as we are flipping the pixels in the x dimension:
    targetRow--;
}

结果:

【讨论】:

  • 只是为了澄清一下,我关于该语句的问题是让 OP 考虑该语句在 C++ 中的实际含义,因为它是编写的。 |很好的答案。 :)
  • 首先,非常感谢您的完整回答!我不知道我可以得到这样的行和列,谢谢你,你发布的准备输出图像的方式似乎更好。我在(uchar)(c, -r + (220)/2) 尝试做的是应用 (y,−x+ 2x0) 的公式来使图像翻转,我的目标是然后尝试使其水平翻转应用 (−y+ 2y0,x) . (更多关于新评论,点击字符限制)
  • 此时我试图调整你的一些代码以适应我的,如果我离开它Vec3b sourcePixel = image.at&lt;Vec3b&gt;(r,c); imageReflectionFinal.at&lt;Vec3b&gt;(r, c) = (uchar)(c, -r + (220)/2);,我会得到我之前得到的蓝色图像,但如果我将它更改为imageReflectionFinal.at&lt;Vec3b&gt;(rows, cols) = (uchar)(cols, -rows + (220)/2)我得到一个空白图像(全黑)。我之前得到的蓝色图像,有一条水平线。
猜你喜欢
  • 2016-08-20
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 2020-04-12
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多