繁体   English   中英

OpenCV - 从相机设备获取像素数据

[英]OpenCV - getting pixel data from camera device

我正在使用OpenCV 2.4.6。 我通过互联网找到了从相机获取帧的一些例子。 效果很好(它将丑陋的面孔显示在屏幕上)。 但是,我绝对无法从帧中获取像素数据。 我在这里找到了一些话题: http//answers.opencv.org/question/1934/reading-pixel-values-from-a-frame-of-a-video/但它对我不起作用。

这是代码 - 在评论的部分我指出了什么是错的。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main() {
    int c;
    IplImage* img;
    CvCapture* capture = cvCaptureFromCAM(1);
    cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
    while(1) {
        img = cvQueryFrame(capture);

        uchar* data = (uchar*)img->imageData; // access violation

        // this does not work either
        //Mat m(img);
        //uchar a = m.data[0]; // access violation

        cvShowImage("mainWin", img);
        c = cvWaitKey(10);
        if(c == 27)
            break;
    }
}

你能给我一些建议吗?

我建议使用较新的Mat结构而不是IplImage因为你的问题用C ++标签标记。 对于您的任务,您可以使用Matdata成员 - 它指向内部Mat存储。 比如Mat img; uchar* data = img.data; Mat img; uchar* data = img.data; 这是一个完整的例子

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main() {
    int c;
    Mat img;
    VideoCapture capture(0);
    namedWindow("mainWin", CV_WINDOW_AUTOSIZE);
    bool readOk = true;

    while(capture.isOpened()) {

        readOk = capture.read(img);

        // make sure we grabbed the frame successfully 
        if (!readOk) {
            std::cout << "No frame" << std::endl;
            break;
        }

        uchar* data = img.data; // this should work

        imshow("mainWin", img);
        c = waitKey(10);
        if(c == 27)
            break;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM