Quick Reference for OpenCV Fundamentals

Guides | Links | Tutorial By 5 years ago

OpenCV is a massive computer vision library, just have a look at its index. It might be a good idea to bookmark that link too as I am sure you will want refer to it later on in your OpenCV projects. Getting started with such a big library can seem daunting to the uninitiated so I am going to present a couple of snippets and concepts to help you get going.

There are also plenty of good tutorials around on the web, here are a couple that I like, AI Shack and OpenCV.

Okay, on with the snippets!

Mat src;                                         // Mat is the image type for OpenCV
src = imread("nameOfImage.jpg");                 // Read an image from file
if (!src.data)                                   // Check if the image was read correctly
{                           
    cerr << "Image could not be loaded" << endl;
    exit(-1);
}

imwrite("write.jpg", src);                       // Write src to an image
imshow("A source image", src);                   // Display image to screen, don't forget the following waitKey call.
waitKey();                                       // Wait for any key press before continuing.
Mat src = Mat::zeros(src.size(), CV_8UC1);       // Create a new single channel image, all black the same size as src

I strongly recommend reading a little about the different colour spaces. The main colour spaces are HSV (best to use if you want to detect colour), BGR and grayscale. Images displayed in BGR or Blue-Green-Red format will resemble a regular picture, however an image in the HSV colour space will have entirely different colours. If you use imshow to display this image, as it will treat it as BGR, thereby rendering the Hue channel as Blue, the Saturation channel as Green and the Value channel as Red.

cvtColor(src, out, CV_BGR2GRAY);        // Used to convert images to different colour spaces

An image that just has black and white pixels (and no gray) is known as a binary image. An easy way to create a binary image is to convert the original image to grayscale and then apply a threshold operation. All the threshold does is look at the values of the pixels and adjusts the value based on the threshold value. Other options for the cvtColor function are CV_BGR2HSV, CV_HSV2BGR, and so on.

threshold(out, out, 150, 255, CV_THRESH_BINARY);    // Threshold value of 150 with a maximum value of 255, white

The threshold operation above will take our grayscaled src Mat, and for each pixel it will check whether the value is above or below 150. If the value is below 150, it will change the value to 0 (Black) if it is above 150 it will change it to 255 (white).

Listed below are several functions that operate on binary images and can act as a way to remove either black or white noise depending on the problem at hand.

Mat src, out;
erode(src, out, Mat());               // Make the black areas bigger at the expense of the white areas
Mat mask(Size(13,13), CV_8UC1);       // Create a single channel mask of size 13 * 13
dilate(out, out, mask);               // Make the white areas much bigger than before

We can use Pyramid image scaling to half the image size or double the image size, with the functions pyrUp and pyrDown.

Mat src = imread("image.jpg");
pyrDown(src, src); // Now src will be half the width and height it was before
pyrUp(src, src);   // Now it will be back to normal
pyrUp(src, src);   // Now it will be double the size it started at.

Mats have a number of attributes we can access.

src.size();          // Get the struct describing the size of the Mat src
src.size().width;    // Get the width of the Mat
src.size().height;   // Get the height of the Mat
src.cols;            // Get the number of columns in the Mat (equal to the width)
src.rows;            // Get the number of rows in the Mat (equal to the height)
src.at(x, y); // Get a vector of 3 values for the pixel at X, Y (For BGR this will be Blue, Green, Red)
src.at(x, y); // Get the float value at pixel X, Y in the Mat (For grayscale, this will be a value indicating how white the pixel is)

That’s all for now, check out those tutorials on OpenCV mentioned above. They’ve got some really useful stuff in there like Contour detection.

Credit also to ScottB, as we did this blog post as a dynamic duo.