【发布时间】:2014-10-28 10:26:10
【问题描述】:
我是 openCV 和 C++ 的新手。我想更改我加载的图像的像素值并在另一个窗口中显示该新图像以比较结果(只是视觉上)。但是,当我运行代码时,我得到了两个原始图像。这意味着要么我的 for 循环没有做它应该做的事情(我怀疑这对我有意义)或者像素值丢失并且没有被保存以显示新图像。我读过之前的一篇文章,说我应该在处理每个像素以设置到更改后的图像后包含此语句。语句为:img.at(Point(x,y)) = color.
有人可以告诉我我做错了什么吗?
谢谢
cv::Mat img = cv::imread("12.jpg", CV_LOAD_IMAGE_COLOR);
// start of pixel navigation
Mat navigateImage(Mat) {
for(int x = 0; x > img.rows; x++)
{
for(int y = 0; y > img.cols; y++){
Vec3b color = img.at<Vec3b>(Point(x,y));
if ( color[0] > 10 && color [1] > 10 && color[2]> 10 )
{
color[0] = 0 ;
color[1] = 0;
color[2] = 0;
img.at<Vec3b>(Point(x,y)) = color;
}
else
{
color.val[0] = 255 ;
color.val[1] = 255;
color.val[2] = 255;
img.at<Vec3b>(Point(x,y)) = color;
}
}
}
return img;
}
// end of pixel navigation
int main( int argc, char** argv )
{
if(! img.data){
cout << "could not open or find the image" << endl;
return -1;}
Mat newImage = navigateImage(img);
cv::imshow( " Original", img);
cv::imshow( " Altered ", newImage);
cv::waitKey(0);
return 0;
}
【问题讨论】: