I have recently installed OpenCV on my Mac. I have been coding OpenCV on a linux machine for a few months now so I have made many programs..
I tried running a program in Mac today.. I installed OpenCV using Homebrew and loaded it on an xCode project.
The program is supposed to load images from the webcam, and filter them to a threshold value, and then add all the filtered images to get one image. This is updated real-time so you should see two windows, one the webcam and the other the filtered webcam (sum of all previous filtered images). The program takes images every 50ms. This program WORKS PERFECTLY ON LINUX.
But on Mac, it loads the webcam, but freezes after that, doesn't show the filtered images, doesn't update the real-time webcam feed.
What's wrong in my code? I haven't been programming in Mac long so I don't know much about the differences of OpenCV on Linux and OpenCV on Mac.
Here's the code:
I tried running a program in Mac today.. I installed OpenCV using Homebrew and loaded it on an xCode project.
The program is supposed to load images from the webcam, and filter them to a threshold value, and then add all the filtered images to get one image. This is updated real-time so you should see two windows, one the webcam and the other the filtered webcam (sum of all previous filtered images). The program takes images every 50ms. This program WORKS PERFECTLY ON LINUX.
But on Mac, it loads the webcam, but freezes after that, doesn't show the filtered images, doesn't update the real-time webcam feed.
What's wrong in my code? I haven't been programming in Mac long so I don't know much about the differences of OpenCV on Linux and OpenCV on Mac.
Here's the code:
Code:
#include <cstdio>
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <math.h>
using namespace cv;
int main(int, char**)
{
Mat frame;
double threshvalue=248, maxthresh=255; // min and max threshold values
IplImage *img, *result, *thresh, *gray;
//CvSize size;
char buf[100];
char buf2[100];
VideoCapture cap(0); // open the default camera, store in cap
if(!cap.isOpened()) // check if we succeeded
return -1;
int key = -1;
//capture webcam into frame
cap >> frame;
// show webcam frame
imshow("webcam", frame);
// store a temporary img location string into buf2
sprintf(buf2, "test/temp.jpg");
//capture webcam and store it as temp.jpg
imwrite(buf2, frame);
//load temp.jpg into img
img = cvLoadImage(buf2, 1);
//create blank image called 'result' based on size of img
result = cvCreateImage(Size(img->width, img->height), IPL_DEPTH_8U, 1);
cvNamedWindow("result", CV_WINDOW_AUTOSIZE);
cvShowImage("result", result);
sprintf(buf, "test/testimage.jpg");
// while loop to take images until a key is pressed
while(1)
{
key = cvWaitKey(50); //image taken every 50 miliseconds
if (key < 0)
{
cap >> frame;
imshow("webcam", frame);
imwrite(buf, frame);
img = cvLoadImage(buf, 1);
//create blank images gray and thresh
gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
thresh = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvCvtColor(img, gray, CV_RGB2GRAY); //convert image to grayscale
// apply threshold filter
cvThreshold(gray, thresh, threshvalue, maxthresh, CV_THRESH_BINARY);
cvErode(thresh, thresh, 0, 6); // erode 6 times to reduce noise
//cvSaveImage(buf2, thresh);// save image
cvAdd(result, thresh, result, NULL); // result = result + thresh
//show result
cvShowImage("result", result); // display result
cvSaveImage("test/result.jpg", result);// save result
remove(buf); // delete the original image
}
else
break;
}
remove("test/temp.jpg");
return 0;
}