c++ - OpenCV:输出图像为蓝色
问题描述
所以我正在制作这个项目,我在 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;
}
解决方案
您的代码存在一些问题。正如所指出的,您的迭代变量超出了实际的图像尺寸。不要使用硬编码边界,您可以使用inputImage.cols
andinputImage.rows
来获取图像尺寸。
有一个已设置但未使用的变量(BGR Vec3b) -Vec3b intensity = image.at<Vec3b>(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--;
}
结果:
推荐阅读
- html - 如何在 CSS 中创建点划线和点划线和矩形
- python - 在 python 中使用 Pandas 合并后如何查找丢弃的数据?
- reactjs - 如何启动 iframe 的 designMode?
- c# - 图像处理:如何使用锁定位执行此功能
- java - 在 JavaFX 中处理一种节点动画的最佳方法
- javascript - 如何在科尔多瓦实现天气 API
- javascript - 如何在 JavaScript 中的数组上动态创建对象内的对象
- python - 我无法在我的代码中获得预期的输出(注册登录系统)
- android - 从 Play 商店安装的应用程序的绘制叠加权限
- ios - 如何在按钮单击时打开 UIPageViewController