[英]OpenCV : convert the pointer in memory to image
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))<200) {
iPtr=(unsigned char*)Fg_getImagePtrEx(fg,lastPicNr,0,_memoryAllc);
::DrawBuffer(nId,iPtr,lastPicNr,"testing"); }
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. 但是我想使用指向图像数据的指针并通过OpenCV显示它们,因为我需要对像素进行处理。 my camera is a CCD mono camera and the depth of the pixels is 8bits. 我的相机是CCD单反相机,像素深度为8位。 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); 我是OpenCV的新手,opencv中是否有任何选项可以返回(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? 还是从iPtr指针获取数据,允许我使用图像数据?
Creating an IplImage
from unsigned char* raw_data
takes 2 important instructions: cvCreateImageHeader()
and cvSetData()
: IplImage
unsigned char* raw_data
创建IplImage
需要2条重要指令: cvCreateImageHeader()
和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. 我没有测试代码,但是您正在寻找的代码路线图已经存在。
If you are using C++, I don't understand why your are not doing it the simple way like this: 如果您使用的是C ++,我不明白为什么您不使用这种简单的方式来做到这一点:
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 . 我建议开始阅读可在此处找到的文档和教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.