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




 
No comments:
Post a Comment