In the tutorial where I have explained how to
write your first program, we already used some kind of image processing.
In this post I want to try out some other image processing possibilities.
Before we start we have to include some headers (from now on I will only use the newer headers from OpenCV 2):
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
After this I want to remind of the standard input and output operations that we will use.
Input and Output of images
Reading an image
We already did this in the tutorial. The command is:
Mat image = imread( "PATH_TO_IMAGE", 1);
So let's split this up:
Mat: this is the class for storing images, like int is for numbers
image: this is the name of the variable where the image is stored in
imread: command to read image
PATH_TO_IMAGE: this specifies the path to the image like
"/users/christian/pictures/img.jpg"
1: specifies a color image with the 3 channels RGB. Change it to 0 for a grayscale image with just one channel.
Showing an image in a window
To do this we need the following command:
imshow("Name of Window", image);
imshow: command to show image
Name of Window: this will be the name of the window where the image is shown in
image: variable of the image you want to display
Image Processing
I will just give you some examples. You can find all the possible commands in the
OpenCV documentation. Let's start:
Allocation of channels
vector<Mat>channels;
split(image, channels);
Here we first create a variable called
channels. With the command 'split' we split the RGB channels of the image and store them in
channels. After that we can access each channel separately.
imshow("Red", channels[2]);
imshow("Green", channels[1]);
imshow("Blue", channels[0]);
Thresholding
Remember that every pixel of a image can be an integer in a range from 0 to 255. This means that a grayscale image can have 256 different scales of gray. The Picture below shows this
threshold(image, image, 100, 255, CV_THRESH_BINARY);
image: the input and here as well the output image
100: this is the threshold; every pixel lower then 100 will be set to 0 (black)
255: all pixels over 100 will here be set to 255 (white)
CV_THRESH_BINARY: type of the threshold (
doc)
For this I am using a grayscale image.
Blurring
blur(image, image, Size (10, 10));
image: the input and here as well the output image
Size: bluring kernel size, the higher the number - the more blur you'll get
Cutting
Rect rect = Rect(100, 100, 200, 200); // Rectangle size 200x200, location x=100 y=100
image(rect).copyTo(imgaecut); // Copy of the image
image(rect) *= 2; // Highlighting of the cut part in the original
Here we are creating a rectangle called
rect with the size of 200x200 pixles and we locate it at x=y=100 pixels starting from the upper left corner. This part will be copied to a new image called
imagecut. After this we highlight the cut part in the original image.
Don't forget to check the
documentation for other processing commands!
Program versions:
OS: Mac OS X 10.9.1
Xcode: 5.0.2
OpenCV: 2.4.8.0
Reference: whydomath.org