Access pixels with Mat OpenCV

十年热恋 提交于 2019-12-31 02:14:10

问题


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.



来源:https://stackoverflow.com/questions/7183359/access-pixels-with-mat-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!