我想要将某些像素的颜色值设置为一个新值,这些像素在cv::Mat图像中。以下是我的代码:
Mat image = img;
for(int y=0;y<img.rows;y++)
{
for(int x=0;x<img.cols;x++)
{
Vec3b color = image.at<Vec3b>(Point(x,y));
if(color[0] > 150 && color[1] > 150 && color[2] > 150)
{
color[0] = 0;
color[1] = 0;
color[2] = 0;
cout << "Pixel >200 :" << x << "," << y << endl;
}
else
{
color.val[0] = 255;
color.val[1] = 255;
color.val[2] = 255;
}
}
imwrite("../images/imgopti"+to_string(i)+".tiff",image);
使用cout输出时,好像可以得到正确的像素,但使用imwrite输出图像时,受影响的像素未被修改。我已经尝试过color.val[0]...,但仍然无法弄清楚为什么输出图像中的像素颜色没有改变。
uchar& c = image.at<uchar>(y,x)
。 - berak