Working with real-time data, bypassing CvloadImage

醉酒当歌 提交于 2019-12-23 06:10:45

问题


This is how we display and process an Image using Opencv:

int _tmain(int argc, _TCHAR* argv[]) {
    IplImage* img = cvLoadImage( "MGC.jpg" );
    cvThreshold( img, img,  200, 255, CV_THRESH_BINARY );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
    return 0;
}

Now can you help me to achieve my goal as outlined below:

int _tmain(int argc, _TCHAR* argv[]) {
    Some code here (using some API other than OpenCv).... // Captures a frame and  
    //streams the data into a buffer Buf
    Buffer B= Buf; // B contain binary image data i.e 16 bit pixel values
    cvThreshold( B, B,  200, 255, CV_THRESH_BINARY );//Notice I am putting B and not an 
                                                     //Image(legal?)
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", B); //Notice I am putting B and not an Image(legal?)   
    cvWaitKey(0);
    cvReleaseImage( &B );
    cvDestroyWindow( "Example1" );
    return 0;
}

Notice that I have not used any hard-disk read/write operation in the second code snippet, such as the following:

  IplImage* img = cvLoadImage( "MGC.jpg" );

Basically I am working with real-time scenario thus I am bypassing the time consuming cvLoadImage. Obviously not saving the data into hard-disk, my entire data is still in the Buffer B. This save my time which is critical for my application.

Note: The fact is I will be doing more processing on the Buffer B, such as applying cvminmaxloc etc. But all these functions require an Image loaded from disk, not a buffer such in my case.

Can some point me to right direction to achieve my goal of working on Buffers instead of Images stored in the hard-disk? Am I committing some mistake in understanding the openCV functions?

Update:

A suggested below I can use imdecode for reading images from buffer. As explained above actually I will be doing processing on the image using OpenCv functions, for example CVMinMaxLoc and

 cvThreshold( src, src,  200, 255, CV_THRESH_BINARY );   

Now can I put Buffer as the first argument instead of Image in cvThreshold( )? If not then what is the alternative to dealing with Buffers (containing pixel values) with openCv functions? If there is no direct method to work with buffers, then what is the indirect method to achieve this goal? What is the work around?

In short I do not want to bring the data to hard disk before doing any processing.


回答1:


Understand that most of the C interface of OpenCV deals with IplImage, not Buffer nor any other custom data type. In other words, doing:

Buffer B = Buf; 
cvThreshold( B, B,  200, 255, CV_THRESH_BINARY );

will issue a compiling error because cvThreshold() requires the data to be encapsulated in an IplImage.

I don't want to run the risk of being Captain Obvious, but you need to convert Buffer to IplImage. So how do you do that, one might ask. Well, IplImage is simple data type that holds these important pieces of information together:

  • The size of the image (width/height);
  • The bit depth of the image;
  • The number of channels;
  • And the data (pixels) of the image;

And how do you create a IplImage from scratch? Call cvCreateImageHeader() followed by cvSetData().

Note: for the depth parameter, you might want to use either IPL_DEPTH_16U or IPL_DEPTH_16S. If your image is RGB, the number of channels is 3. After you are done using the IplImage, don't forget to call cvReleaseImage() to free it's resources.

I believe the real challenge is: how will you extract all these information from Buffer. Good luck!




回答2:


There is imdecode function in C++ OpenCV API which is able to decode image stored in memory buffer. But there are no analogs in legacy C API.




回答3:


I don't think you can put buffer (as you said having pixel values )in place of src i.e an Image loaded from disk in the following

  cvThreshold( src, src,  200, 255, CV_THRESH_BINARY );   

One way might be to convert your buffer in MAT and then use cvThreshold.

May be somebody having more experience might comment on this.




回答4:


You should convert your buffer to the openCV type Mat

Something similar is done here



来源:https://stackoverflow.com/questions/11272005/working-with-real-time-data-bypassing-cvloadimage

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