OpenCV修改图像像素

时间:2017-09-26 12:13:35

标签: c++ image opencv image-processing

我想修改图像的每个像素。但结果并不像我预期的那样。

原始图片:

enter image description here

结果:

enter image description here

用于修改每个像素的循环:

image = imread("image.png", CV_LOAD_IMAGE_COLOR)
unsigned char value;

for (int col = 0; col < image.cols; col++){
   for (int row = 0; row < image.rows; row++){
        value = image.at<unsigned char>(row,col);
        value *= 0;
        image.at<unsigned char>(row, col) = value;
   }
}

所以我不知道错误在哪里,我想问你是否知道答案。

1 个答案:

答案 0 :(得分:6)

对于3通道无符号字符图像,在循环遍历行和列时,需要使用Vec3b访问所有3个通道。

image = imread("image.png", CV_LOAD_IMAGE_COLOR)
Vec3b value;

for (int row = 0; row < image.rows; row++){
   for (int col = 0; col < image.cols; col++){
        //Vec3b as typename
        value = image.at<Vec3b>(row,col);
        value *= 0;
        image.at<Vec3b>(row, col) = value;
   }
}