Skip to content

Latest commit

 

History

History
138 lines (93 loc) · 3.31 KB

opencv.org

File metadata and controls

138 lines (93 loc) · 3.31 KB

OpenCV examples

The OpenCV homepage is opencv.org; you’ll want to look at their reference manual and tutorials, in particular tutorials about installation.

Loading, saving, viewing: Images, videos, cameras

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; }
        }
    }
}

Image transformations and filters

Eroding

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);

Dilating

Same code as erosion, even same kernel, except for this minor change:

dilate(img_binary, img_binary_dilate4x4, kernel);

Object tracking

Background modeling

static image, or updating image; using differences

building MOG model

Background subtraction

Finding contours

Bounding boxes

Face detection

(not face recognition)