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.
Understand that most of the C interface of OpenCV deals with
IplImage, notBuffernor any other custom data type. In other words, doing:will issue a compiling error because
cvThreshold()requires the data to be encapsulated in anIplImage.I don’t want to run the risk of being Captain Obvious, but you need to convert
BuffertoIplImage. So how do you do that, one might ask. Well,IplImageis simple data type that holds these important pieces of information together:And how do you create a
IplImagefrom scratch? CallcvCreateImageHeader()followed bycvSetData().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 callcvReleaseImage()to free it’s resources.I believe the real challenge is: how will you extract all these information from
Buffer. Good luck!