【问题标题】:Access pixels with Mat OpenCV使用 Mat OpenCV 访问像素
【发布时间】:2011-11-03 05:52:19
【问题描述】:

我想使用 OpenCV 2.3 访问 RGB 中的像素。 我正在尝试这样,但就像每个像素在帧后都是相等的,因为我没有输出。图片来自我的网络摄像头,我可以看到它们。 顺便说一句,红色 = 0;

谢谢

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;
}

【问题讨论】:

    标签: c++ image opencv rgb pixel


    【解决方案1】:

    改变

    oldFrame = frame;
    

    到

    oldFrame = frame.clone();
    

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

    【讨论】:

    • 最好使用frame.copyTo(oldFrame) 以避免为每个帧重新分配内存。
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    相关资源
    最近更新 更多