Hello!
I've trying to set just a really simple program to make sure my OpenCV installation is working properly. The idea is simple get the frames from my webcam and show them.
I made a code that is able to load an image from a directory and i also have a code that uses the opencv to get the images from the webcam and show them with the opencv highgui. I already figured out that i need to convert the IplImage to the QImage so i also managed to find a function that does the conversion between formats. What i did was this:
QImage* IplImage2QImage
(IplImage
*iplImg
) {
int h = iplImg->height;
int w = iplImg->width;
int channels = iplImg->nChannels;
char *data = iplImg->imageData;
for (int y = 0; y < h; y++, data += iplImg->widthStep) {
for (int x = 0; x < w; x++) {
char r, g, b, a = 0;
if (channels == 1) {
r = data[x * channels];
g = data[x * channels];
b = data[x * channels]; }
else if (channels == 3 || channels == 4) {
r = data[x * channels + 2];
g = data[x * channels + 1];
b = data[x * channels]; }
if (channels == 4) {
a = data[x * channels + 3];
qimg->setPixel(x, y, qRgba(r, g, b, a)); }
else {
qimg->setPixel(x, y, qRgb(r, g, b)); }
}
}
return qimg;
}
int main(int argc, char *argv[])
{
IplImage* frame;
CvCapture* capture;
capture = cvCreateCameraCapture(1);
while(1) {
frame = cvQueryFrame(capture);
myImage = IplImage2QImage(frame);
myLabel.
setPixmap(QPixmap::fromImage(*myImage
));
myLabel.show(); }
cvReleaseCapture(&capture);
return a.exec();
}
QImage* IplImage2QImage(IplImage *iplImg)
{
int h = iplImg->height;
int w = iplImg->width;
int channels = iplImg->nChannels;
QImage *qimg = new QImage(w, h, QImage::Format_ARGB32);
char *data = iplImg->imageData;
for (int y = 0; y < h; y++, data += iplImg->widthStep) {
for (int x = 0; x < w; x++) {
char r, g, b, a = 0;
if (channels == 1) {
r = data[x * channels];
g = data[x * channels];
b = data[x * channels]; }
else if (channels == 3 || channels == 4) {
r = data[x * channels + 2];
g = data[x * channels + 1];
b = data[x * channels]; }
if (channels == 4) {
a = data[x * channels + 3];
qimg->setPixel(x, y, qRgba(r, g, b, a)); }
else {
qimg->setPixel(x, y, qRgb(r, g, b)); }
}
}
return qimg;
}
int main(int argc, char *argv[])
{
IplImage* frame;
CvCapture* capture;
QImage* myImage;
QLabel myLabel;
QApplication a(argc, argv);
capture = cvCreateCameraCapture(1);
while(1) {
frame = cvQueryFrame(capture);
myImage = IplImage2QImage(frame);
myLabel.setPixmap(QPixmap::fromImage(*myImage));
myLabel.show(); }
cvReleaseCapture(&capture);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
it builds without any error but when i run it i only get this:
result.jpg
i also checked THIS post and from it i assumed that only using openCV i should also be able to get the output of the webcam (i rewritten the code only for one web)
int main( int argc, char *argv[] )
{
CvCapture *capture = 0;
IplImage *frame = 0;
int key = 0;
/* initialize camera */
capture = cvCaptureFromCAM( 0 );
/* always check */
if ( !capture ) {
fprintf( stderr, "Cannot open initialize webcam!\n" );
return 1; }
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480);
cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, 8);
/* create a window for the video */
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
while( key != 'q' ) {
/* get a frame */
frame = cvQueryFrame( capture );
/* always check */
if( !frame ) break;
/* display current frame */
cvShowImage("Left", frame);
/* exit if user press 'q' */
key = cvWaitKey( 1 ); }
/* free memory */
cvDestroyWindow( "result" );
cvReleaseCapture( &capture );
return 0;
}
int main( int argc, char *argv[] )
{
CvCapture *capture = 0;
IplImage *frame = 0;
int key = 0;
/* initialize camera */
capture = cvCaptureFromCAM( 0 );
/* always check */
if ( !capture ) {
fprintf( stderr, "Cannot open initialize webcam!\n" );
return 1; }
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480);
cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, 8);
/* create a window for the video */
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
while( key != 'q' ) {
/* get a frame */
frame = cvQueryFrame( capture );
/* always check */
if( !frame ) break;
/* display current frame */
cvShowImage("Left", frame);
/* exit if user press 'q' */
key = cvWaitKey( 1 ); }
/* free memory */
cvDestroyWindow( "result" );
cvReleaseCapture( &capture );
return 0;
}
To copy to clipboard, switch view to plain text mode
and i get exactly the same results. no errors when built and the a similar cmd window as the previous picture...
can someone give me a hint on what is wrong in either of the codes and why i'm not getting the images from the webcam?
many thanks
Bookmarks