2

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. 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?

karlphillip
  • 92,053
  • 36
  • 243
  • 426
user261002
  • 2,182
  • 10
  • 46
  • 73
  • Is your Camera not supported or why are you trying to mess with pointers? – Stephan Dollberg Apr 12 '12 at 14:40
  • hi bamboon, im not sure about this, does it mean that if it supports my camera then I dont need to use the framegrabber interface, and I can use the OpenCV directly to get the images? – user261002 Apr 12 '12 at 15:06
  • Thank you so much. Im going to try it now. – user261002 Apr 12 '12 at 15:17
  • im trying to use the sample from this link : http://opencv.willowgarage.com/wiki/CameraCapture but it keep giving me this message : Error:capture is NULL, do you know how can I find out if the opencv is supporting my camera. ?? – user261002 Apr 12 '12 at 15:33
  • there is a list here which I assume is probably not complete though. http://opencv.willowgarage.com/wiki/FullOpenCVWiki#Welcome.2FOS.OS_Specific_Stuff – Stephan Dollberg Apr 12 '12 at 15:38
  • I am using Mikrotron CMOS High Speed Camera - Eosens (MC1362) , which it wasnt in that list. – user261002 Apr 12 '12 at 15:41
  • @bamboon my good old crystal ball never fails to surprise me. – karlphillip Apr 12 '12 at 17:24

2 Answers2

5

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.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • hi, thank you so much for your code, Im trying your code, its doeant show any error, but on the screen it only shows a gray image and quickly close the window. do you know why it doesnt show my images? – user261002 Apr 12 '12 at 17:03
  • 1
    Try changing the time in `cvWaitKey()`. It's the amount of miliseconds it will wait for a key press. I think you can try passing `0` to force the function to wait for a keypress. Also, do you mean that it shows your image in gray shades , or just a full gray rectangle inside the window? Are your images colored?!? – karlphillip Apr 12 '12 at 17:10
  • Thank you. well my camera is a monochrome camera. but the image is only a full gray rectangle. one more question, I put the whole code in the while LOOP except ReleaseImageHeader and the DestroyWinodw. hope this is correct – user261002 Apr 12 '12 at 17:17
  • 1
    In that case, leave the `cvNamedWindow()` call out of the loop. Else it will create a new window on every iteration and this can cause problems, including the one you are seeing. – karlphillip Apr 12 '12 at 17:21
  • 1
    WOW, you are STAR, it did work and its showing the images from the memory. one more question as I am new to openCV, I was trying to use the openCV directly without a need of using the framegrabber, I am using Mikrotron CMOS High Speed Camera - Eosens (MC1362) camera, but none of the simple examples, work, do you have any Idea how can I use OpenCV connected to camera? – user261002 Apr 12 '12 at 17:23
  • Thanks, you can click on the checkbox near my answer to select it as the official answer to your question. If your camera is not on [this list](http://opencv.willowgarage.com/wiki/Welcome/OS) then you are already doing the way it should be done. – karlphillip Apr 12 '12 at 17:27
  • 2
    Just a heads up, since we ran into issues with this example in our robotics project - `cv_image->widthStep` is an aligned row size (look it up in OpenCV documentation) while `cvSetData` expects actual row size, so in some cases where the aligned value does not match the actual row size, you'll get your pixels shifted. – Rudolfs Bundulis Oct 16 '12 at 06:29
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.

Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
  • Simple: the OP might not be reading a file from the disk. It seems he is grabbing the frames directly from a device. – karlphillip Apr 12 '12 at 14:30
  • @karlphillip why isn't he using the proper way then? I simply assume the problem is of a total different nature. – Stephan Dollberg Apr 12 '12 at 14:30
  • Maybe his device is not supported? ;) – karlphillip Apr 12 '12 at 14:31
  • @karlphillip sounds reasonable, we'll have to ask him that. – Stephan Dollberg Apr 12 '12 at 14:32
  • hi all, thank you so much for contributing to solve my problem. as I told you I am new to OpenCV, and Im not sure of it supports my camera or not. the only thing that I understand is that I have a framegrabber, and I'm using the framegrabber interface to get the data from camera and store them in the memory, now instead of using the grabber interface, which doesnt give me that much options for image processing I need to read the raw data from the memory by OpenCV then I can prepare them for processing – user261002 Apr 12 '12 at 15:03