Reputation: 25
I'm writing a simple algorithm to convert a RGB image, from my webcam, to HSV, it's compiling fine, but crashes when the .exe is executed.
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv)
{
// Cria uma janela.
cvNamedWindow("imagem", CV_WINDOW_AUTOSIZE);
// Cria a conexão com a webcam.
CvCapture *capture = cvCreateCameraCapture(0);
// Variável que armazena o frame.
IplImage *frame;
IplImage* imghsv = cvCreateImage(cvGetSize(frame),8,3);
while(1)
{
// Variável recebe o frame.
frame = cvQueryFrame(capture);
if(!frame) break;
cvCvtColor(frame, imghsv, CV_BGR2HSV);
// Exibe o frame na janela.
cvShowImage("imagem", frame);
cvShowImage("hsv", imghsv);
// Encerra o loop com uma tecla.
if( cvWaitKey(100) == 27 ) break;
}
// Libera a memória utiliazada.
cvReleaseImage(&frame);
cvReleaseImage(&imghsv);
cvReleaseCapture(&capture);
// Fecha a janela.
cvDestroyWindow("imagem");
cvDestroyWindow("hsv");
}
Without the line "IplImage* imghsv = cvCreateImage(cvGetSize(frame),8,3);" the .exe works, but the algorithm only shows my image.
I'm using Dev C++ 4.9.9.2 and OpenCV 2.1 in Windows XP SP3. Sorry about the english. Thanks
The problem was solved replacing IplImage *frame;
by IplImage *frame=cvQueryFrame(capture);
.
Upvotes: 1
Views: 903
Reputation: 5023
This code works perfectly... Some lines missing in your code... Have a look.
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv)
{
// Cria uma janela.
cvNamedWindow("imagem", CV_WINDOW_AUTOSIZE);
cvNamedWindow("hsv", CV_WINDOW_AUTOSIZE); // added in orig code
// Cria a conexão com a webcam.
CvCapture *capture = cvCreateCameraCapture(0);
// Variável que armazena o frame.
IplImage *frame;
IplImage* imghsv;
// IplImage* imghsv = cvCreateImage(cvGetSize(frame),8,3);
while(1)
{
// Variável recebe o frame.
frame = cvQueryFrame(capture);
imghsv = cvCreateImage(cvGetSize(frame),8,3); /// changed from orig code.
if(!frame) break;
cvCvtColor(frame, imghsv, CV_BGR2HSV);
// Exibe o frame na janela.
cvShowImage("imagem", frame);
cvShowImage("hsv", imghsv);
// Encerra o loop com uma tecla.
if( cvWaitKey(100) == 27 ) break;
}
// Libera a memória utiliazada.
cvReleaseImage(&frame);
cvReleaseImage(&imghsv);
cvReleaseCapture(&capture);
// Fecha a janela.
cvDestroyWindow("imagem");
cvDestroyWindow("hsv");
}
Upvotes: 0
Reputation: 10698
IplImage *frame;
IplImage* imghsv = cvCreateImage(cvGetSize(frame),8,3);
Your frame
image has not been yet allocated, you shouldn't create your hsv image before actually grabbing a frame.
Also note :
cvReleaseImage(&frame);
You should not release an image grabbed from cvQueryFrame()
, OpenCV will take care of it.
From the OpenCV documentation :
The function cvQueryFrame grabs a frame from a camera or video file, decompresses it and returns it. This function is just a combination of GrabFrame and RetrieveFrame , but in one call. The returned image should not be released or modified by the user. In the event of an error, the return value may be NULL.
CvCapture *capture = cvCreateCameraCapture(0);
You should also check the return value of cvCreateCameraCapture
, which could be NULL
.
IplImage* imghsv = cvCreateImage(cvGetSize(frame),8,3);
You should use IPL_DEPTH_8U
instead of 8
.
Upvotes: 2