Access pixels with Mat OpenCV

2019-02-24 14:39发布

问题:

I would like to access pixels in RGB with OpenCV 2.3. I'm trying like this but it's like every pixels are equal frame after frame because I got no output. Images are from my webcam and I can see them. Btw RED = 0;

THX

Mat frame;
Mat oldFrame;

VideoCapture cap(0);
cap >> oldFrame;
sumFramePix = oldFrame.cols * oldFrame.rows;
nbChannels = oldFrame.channels();
cout << "NbcHANNELs : " << nbChannels << endl;
imshow("Video 1", oldFrame);

while(1)
{
    cap >> frame;
    imshow("Video 1", frame);

    for(int i=0; i<frame.rows; i++)
    {
        for(int j=0; j<frame.cols; j++)
        {
            if (frame.ptr<uchar>(i)[nbChannels*j+RED] < oldFrame.ptr<uchar>(i)[nbChannels*j+RED])
            {
                cout << "==============-";
            }
        }
    }
    oldFrame = frame;

    if(waitKey(300) >= 0) break;
}

回答1:

Change

oldFrame = frame;

to

oldFrame = frame.clone();

You are creating two Mat objects that point to the same data. clone() makes a deep copy.