How Change The Color of BGR pixels With The Same Value C++

Hello! Someone knows how to change the color of every pixel equal into another color? For example, change all the BGR pixels (130, 0, 0) to (0, 0, 0). Below my code, nevertheless it is not returning what I expected.

   for (int i = 0; i < dest.rows; i++) {
        for (int j = 0; j < dest.cols; j++) {
            if (dest.at<uchar>(i, j) == (130, 0, 0));
            dest.at<uchar>(i, j) = (0, 0, 0);
        }
    }
    imshow("result", dest);
    waitKey(0);

Solved.

    for (int y = 0; y < dest.rows; y++)
        {
            for (int x = 0; x < dest.cols; x++)
            {
                
                Vec3b& color = dest.at<Vec3b>(y, x);
                if (color[0] == 130 && color[1] == 0 && color[2] == 0) {
                   
                    color[0] = 0;
                    color[1] = 0;
                    color[2] = 0;

            
                    dest.at<Vec3b>(Point(x,y)) = color;
                    
                }
            }
        }
        imshow("result", dest);
        waitKey(0);

Don’t use for loop

    Mat mask;
    inRange(img, Vec3b(130, 0, 0), Vec3b(130, 0, 0), mask);
    img.setTo(Vec3b(0,0,0),mask);
1 Like

Good optimization, thank you Laurent Berger.

to add…

C and C++ do not have a tuple type. in C and C++, that expression evaluates to 0, the last element of the list.

that’s why OpenCV comes with various Vec types to fill the purpose, as demonstrated by laurent.berger.

1 Like

yes, I noticed. Thank you CrackWitz