The OpenCV homepage is opencv.org; you’ll want to look at their reference manual and tutorials, in particular tutorials about installation.
Handling images is quite simple. Full code: images.cpp
// load the image
Mat img = imread("lena.jpg");
// show it in a window
imshow("Image", img);
// image window will immediately disappear if the program ends, so
// we'll wait for a keypress, indefinitely
waitKey();
// do a simple transformation: convert to grayscale
// first copy the image
Mat img_gray = img.clone();
// now convert to grayscale
cvtColor(img, img_gray, CV_RGB2GRAY);
// save the image
bool ret = imwrite("lena.jpg", img_gray);
if(ret) {
cout << "Save worked." << endl;
} else {
cout << "Save failed." << endl;
}
Amazingly, handling video files is also very simple, and is virtually the same as handling cameras! Full code: videos.cpp
VideoCapture capture(-1); // use webcam
//VideoCapture capture("output.mpg"); // use input file
Mat frame;
if(capture.isOpened()) {
// if using webcam:
int fps = 30;
// if using input file:
//int fps = capture.get(CV_CAP_PROP_FPS);
int width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
int height = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "FPS: " << fps << ", width: " << width << ", height: " << height << endl;
VideoWriter writer("input.mpg", CV_FOURCC('P','I','M','1'),
fps, cvSize(width, height), 0); // 0 means gray, 1 means color
if(writer.isOpened()) {
while(true) {
capture >> frame;
if(!frame.empty()) {
imshow("Video", frame);
}
Mat frame_gray = frame.clone();
cvtColor(frame, frame_gray, CV_RGB2GRAY);
writer << frame_gray;
int key = waitKey(10);
if((char)key == 'q') { break; }
}
}
}
Mat img = imread("lena.jpg");
imshow("Image", img);
// convert to grayscale
Mat img_gray = img.clone();
cvtColor(img, img_gray, CV_RGB2GRAY);
// convert to binary with a gray threshold
Mat img_binary = img_gray.clone();
threshold(img_gray, img_binary, 128.0, 255.0, THRESH_BINARY);
imshow("Binary", img_binary);
// make a kernel of 4x4, all 1's
Mat kernel = Mat::ones(Size(4, 4), CV_8U);
Mat img_binary_erode4x4 = img_binary.clone();
// do the erosion
erode(img_binary, img_binary_erode4x4, kernel);
imshow("Erode binary 4x4", img_binary_erode4x4);
Same code as erosion, even same kernel, except for this minor change:
dilate(img_binary, img_binary_dilate4x4, kernel);
static image, or updating image; using differences
building MOG model
(not face recognition)