Bug with pixel access in OpenCV 2.x

前端 未结 2 700
栀梦
栀梦 2021-01-21 16:30

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.

2条回答
  •  温柔的废话
    2021-01-21 17:32

    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):

    original img

    Result using OP's code:

    OP's code

    Result using the revised code (with j * img.cols * 3):

    New code

提交回复
热议问题