+ cvReleaseMat(&g_encoded);
+ cvReleaseMat(&g_src);
+}
+
+ bool Camera_GetImage(int num, int width, int height, CvMat * image)
+ {
+ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Need to use a mutex to ensure 2 captures are not open at once
+ pthread_mutex_lock(&mutex);
+ bool result = false;
+
+ capture = cvCreateCameraCapture(num);
+
+ cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
+ cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);
+
+ IplImage * frame = cvQueryFrame(capture);
+ if( frame == NULL)
+ return result;
+
+ // Convert the IplImage pointer to CvMat
+ CvMat stub;
+ image = cvGetMat(frame, &stub, 0, 0);
+ if( image == NULL)
+ return result;
+
+ // Release the capture and IplImage pointers
+ cvReleaseImageHeader(&frame);
+ cvReleaseCapture(&capture);
+
+ pthread_mutex_unlock(&mutex); //Close the mutex
+ return true;