使用Mat OpenCV访问像素

Access pixels with Mat OpenCV

本文关键字:像素 访问 OpenCV Mat 使用      更新时间:2023-10-16

我想用OpenCV 2.3访问RGB中的像素。我试着这样做,但每一帧的像素都是相等的,因为我没有输出。图片来自我的网络摄像头,我可以看到它们。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;
}

更改

oldFrame = frame;

oldFrame = frame.clone();

您正在创建两个指向相同数据的Mat对象。clone()进行深度复制。