I\'m having trouble trying to find out how to access rgb pixel in the new version (2.x) of OpenCV. I tried using a mix of the old and the new method but without success.
Inside your loop, you can do:
img.at<Vec3b>(j,i)[0] = 0; // Blue Channel
img.at<Vec3b>(j,i)[1] = 0; // Green Channel
img.at<Vec3b>(j,i)[2] = 0; // Red Channel
Is this what you wanted or I understood incorrectly?
I tested out your code, and I found the bug. You multiplied the column index by 3 (i * 3
), but it's also necessary to multiply the row index by 3 (j * img.cols * 3
).
I replaced j * img.cols
with j * img.cols * 3
:
for (int j = 0; j < img.rows; j++)
{
for (int i = 0; i < img.cols; i++)
{
img.data[j * img.cols * 3 + i*3 + 0] = (uchar)0; //B
//img.data[j * img.cols * 3 + i*3 + 1] = (uchar)0; //G
//img.data[j * img.cols * 3 + i*3 + 2] = (uchar)0; //R
}
}
Let's try an example.
Example image (from MIT pedestrian dataset):
Result using OP's code:
Result using the revised code (with j * img.cols * 3
):