OpenCV : convert the pointer in memory to image

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

I have a grabber which can get the images and show them on the screen with the following code

while((lastPicNr = Fg_getLastPicNumberBlockingEx(fg,lastPicNr+1,0,10,_memoryAllc))

but I want to use the pointer to the image data and display them with OpenCV, cause I need to do the processing on the pixels. my camera is a CCD mono camera and the depth of the pixels is 8bits. I am new to OpenCV, is there any option in opencv that can get the return of the (unsigned char*)Fg_getImagePtrEx(fg,lastPicNr,0,_memoryAllc); and disply it on the screen? or get the data from the iPtr pointer an allow me to use the image data?

回答1:

Creating an IplImage from unsigned char* raw_data takes 2 important instructions: cvCreateImageHeader() and cvSetData():

// 1 channel for mono camera, and for RGB would be 3 int channels = 1;  IplImage* cv_image = cvCreateImageHeader(cvSize(width,height), IPL_DEPTH_8U, channels); if (!cv_image) {     // print error, failed to allocate image! }  cvSetData(cv_image, raw_data, cv_image->widthStep);  cvNamedWindow("win1", CV_WINDOW_AUTOSIZE); cvShowImage("win1", cv_image); cvWaitKey(10);  // release resources cvReleaseImageHeader(&cv_image); cvDestroyWindow("win1"); 

I haven't tested the code, but the roadmap for the code you are looking for is there.



回答2:

If you are using C++, I don't understand why your are not doing it the simple way like this:

If your camera is supported, I would do it this way:

   cv::VideoCapture capture(0);     if(!capture.isOpened()) {      // print error      return -1;    }     cv::namedWindow("viewer");     cv::Mat frame;     while( true )    {      capture >> frame;       // ... processing here       cv::imshow("viewer", frame);      int c = cv::waitKey(10);      if( (char)c == 'c' ) { break; } // press c to quit    } 

I would recommend starting to read the docs and tutorials which you can find here.



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