Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

First: your image is in RGB (CV_8UC3) and you treat it as a greyscale image. That's why it goes only to 1/3 of your row. RGB images contain 3xCols bytes (uchar values) per row.

So either you go to 3*cols in the second for loop, or use Vec3b variables.

With Vec3b variables it's also simple to set it in red:

Vec3b red=Vec3b(0,0,25);
...
    //in the second for loop
    Vec3b pixel=image.at<Vec3b>(j, i)
    if ( (pixel[0]+pixel[1]+pixel[2]) /3 < 100) //change color less than 100
    {
        image.at<Vec3b>(j, i) = red; // make it red
    }

Second: if you want to process greyscale images, be sure they are CV_8UC1 type. Convert it if needed.