Ask Your Question
0

having trouble to access to pixels in ROI

asked 2020-05-28 01:49:52 -0600

suu gravatar image
    Rect roi(0, 0, W / 3, H);

    Mat Roi_1 = image_gray(roi);

    int h_1 = Roi_1.rows;
    int w_1 = Roi_1.cols;

    int sum1 = 0;

    int mean1  = 0;

    int x, y;

    uchar* Data_1 = Roi_1.data;

    for (y = 0; y < h_1; y++) {
        for (x = 0; x < w_1; x++) {
            sum1 += Data_1[y * w_1, +x];
        }
    }

    mean1 = sum1 / (h_1 * w_1);

I want to get mean value of pixels on the left side of the image

so I set the ROI and read the pixels value of ROI

but it returns the upside mean values not left side

I think my code read the original image("image_gray"), not the ROI

I don't know what can I do

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-05-28 04:23:21 -0600

berak gravatar image

a roi does not have continuous memory, it is more a "view" into the original pixel array. (so your code gets the row offsets wrong)

please do not write for ... loops for this, instead use builtin opencv functions,

see it all boils down to a single (and safe !!) line:

Scalar mean = cv::mean(image_gray(roi));
edit flag offensive delete link more

Comments

it works, you save my life.

suu gravatar imagesuu ( 2020-05-28 05:33:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-05-28 01:49:52 -0600

Seen: 161 times

Last updated: May 28 '20