Ask Your Question

Revision history [back]

Camera snap under OS X

Hi,

I've written the following C program, able to take a snap with the camera. It works well under GNU/Linux, but I got OpenCV with Macports, and it compiles and runs, but doesn't produce any image ... What's the problem with OS X ?

/* required standards includes */

include <stdio.h>

include <stdlib.h>

include <string.h>

include <opencv cv.h="">

include <opencv highgui.h="">

/* * Main program / int main(argc,argv) int argc; char *argv; { int f,g,p; /* Indexes / char name[255]; / string for input image / int width,height; / size of the image / FILE *fp1; / pointers to files for reading and writing */

IplImage *ITmp=NULL;
IplImage *framePtr=NULL;
CvCapture *capture=NULL;
CvSize frameSize;

if(argc!=2)
    {
    printf("¥nusage : %s image(.ppm)¥n",argv[0]);
    printf("Acquire an image file from IEEE1394 camera¥n");
    exit(1);
    }

while(capture==NULL) capture=cvCreateCameraCapture(CV_CAP_ANY);
printf("Camera OK¥n");
while(framePtr==NULL) framePtr=cvQueryFrame(capture);
printf("Capture OK¥n");
    frameSize=cvGetSize(framePtr);
width=frameSize.width;
height=frameSize.height;
ITmp=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
if(ITmp!=NULL)
    {
    cvConvertImage(framePtr,ITmp,0);

/* form the name of the input image / for(f=0;argv[1][f]!='¥0'&&argv[1][f]!='.';f++); argv[1][f]='¥0'; strcpy(name,argv[1]); strcat(name,".ppm"); fp1=fopen(name,"wb"); if(fp1==NULL) { printf("Can't open %s(.pgm|.ppm) for writing¥n",argv[1]); exit(1); } / write the output image / fprintf(fp1,"P6¥n#acquired image¥n#from %d %d¥n%d %d¥n255¥n",0,0,width,height); for(g=0;g<height;g++) for(f="0;f&lt;width;f++)" for(p="0;p&lt;3;p++)" putc(itmp-="">imageData[3(g*width+f)+2-p],fp1); fclose(fp1); } cvReleaseCapture(&capture); if(ITmp!=NULL) cvReleaseImage(&ITmp); exit(0); }

Can someone help me ?

Thanks,

Best regards,

-- François LE COAT

click to hide/show revision 2
retagged

updated 2018-12-28 07:31:10 -0500

berak gravatar image

Camera snap under OS X

Hi,

I've written the following C program, able to take a snap with the camera. It works well under GNU/Linux, but I got OpenCV with Macports, and it compiles and runs, but doesn't produce any image ... What's the problem with OS X ?

/* required standards includes */

include <stdio.h>

include <stdlib.h>

include <string.h>

include <opencv cv.h="">

include <opencv highgui.h="">

/* * Main program / int main(argc,argv) int argc; char *argv; { int f,g,p; /* Indexes / char name[255]; / string for input image / int width,height; / size of the image / FILE *fp1; / pointers to files for reading and writing */

IplImage *ITmp=NULL;
IplImage *framePtr=NULL;
CvCapture *capture=NULL;
CvSize frameSize;

if(argc!=2)
    {
    printf("¥nusage : %s image(.ppm)¥n",argv[0]);
    printf("Acquire an image file from IEEE1394 camera¥n");
    exit(1);
    }

while(capture==NULL) capture=cvCreateCameraCapture(CV_CAP_ANY);
printf("Camera OK¥n");
while(framePtr==NULL) framePtr=cvQueryFrame(capture);
printf("Capture OK¥n");
    frameSize=cvGetSize(framePtr);
width=frameSize.width;
height=frameSize.height;
ITmp=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
if(ITmp!=NULL)
    {
    cvConvertImage(framePtr,ITmp,0);

/* form the name of the input image / for(f=0;argv[1][f]!='¥0'&&argv[1][f]!='.';f++); argv[1][f]='¥0'; strcpy(name,argv[1]); strcat(name,".ppm"); fp1=fopen(name,"wb"); if(fp1==NULL) { printf("Can't open %s(.pgm|.ppm) for writing¥n",argv[1]); exit(1); } / write the output image / fprintf(fp1,"P6¥n#acquired image¥n#from %d %d¥n%d %d¥n255¥n",0,0,width,height); for(g=0;g<height;g++) for(f="0;f&lt;width;f++)" for(p="0;p&lt;3;p++)" putc(itmp-="">imageData[3(g*width+f)+2-p],fp1); fclose(fp1); } cvReleaseCapture(&capture); if(ITmp!=NULL) cvReleaseImage(&ITmp); exit(0); }

Can someone help me ?

Thanks,

Best regards,

-- François LE COAT