This is an old revision of the document!
Tutorial 1 -
<Code:c++ linenums:1>
Blocks that we will need on this program #include “opencv2/highgui/highgui.hpp” #include “opencv2/core/core.hpp” #include “opencv2/imgproc/imgproc.hpp” declaring the namespaces using namespace std; using namespace cv;
int main(int argc, char *argv[]) {
//Declaring variables to store the colored,gray scale and HSV images Mat colorimage; Mat grayimage; Mat hsvimage;
//Read the color image from your PC. //Change the argument of the imread function to your image's path. colorimage = imread("C:/Users/jcunhamatos/Desktop/s63.jpg");
//Resize function to resize your image resize(colorimage, colorimage, Size(640, 480));
//Convert Color function. //cvtColor(source image , converted image , type of conversion ) //You can check a list of possible convertions on the opencv website // The third argument will define the conversion.
// COLOR TO GRAY (CV_BGR2GRAY) cvtColor(colorimage, grayimage, CV_BGR2GRAY);
//COLOR TO HSV ( CV_BGR2HSV) cvtColor(colorimage, hsvimage, CV_BGR2HSV);
//Function to create windows to display your image. The type of the window //can be defined on the second argument //Can be omitted ( just use imshow ) if you want to use the default window properties. namedWindow("Color Image", CV_WINDOW_NORMAL); namedWindow("GrayScale Image", CV_WINDOW_NORMAL); namedWindow("HSV Image", CV_WINDOW_NORMAL);
//function to show the image on the specified window (first argument) imshow("Color Image", colorimage); imshow("GrayScale Image", grayimage); imshow("HSV image", hsvimage);
//Press any key to end the program waitKey(0); return 0;
}
</Code>
Understanding the Code
<Code:c++ linenums:1>
Blocks that we will need on this program #include “opencv2/highgui/highgui.hpp” #include “opencv2/core/core.hpp” #include “opencv2/imgproc/imgproc.hpp” declaring the namespaces using namespace std; using namespace cv;
</Code>
Line 1 to 11:
First we have to include the modules from OpenCV that will be used on the program. Each modules has its own functions and Variables type. (For example: the Mat variable type , the cvtColor function , the imshow function , etc… - each one is inside of one OpenCV module , that needs to be included in order to make the program compile).
<Code:c++ linenums:13> int main(int argc, char *argv[]) {
//Declaring variables to store the colored,gray scale and HSV images Mat colorimage; Mat grayimage; Mat hsvimage;
</Code>
Line 13 to 19
To Store images we will the Mat variable type.
<Code:c++ linenums:20> Read the color image from your PC. Change the argument of the imread function to your image's path.
colorimage = imread("C:/Users/jcunhamatos/Desktop/s63.jpg");
Resize function to resize your image resize(colorimage, colorimage, Size(640, 480)); Convert Color function.
//cvtColor(source image , converted image , type of conversion ) //You can check a list of possible convertions on the opencv website // The third argument will define the conversion.
COLOR TO GRAY (CV_BGR2GRAY) cvtColor(colorimage, grayimage, CV_BGR2GRAY); COLOR TO HSV ( CV_BGR2HSV)
cvtColor(colorimage, hsvimage, CV_BGR2HSV);
</Code>
Line 20 to 37
First we have to open the image using the “imread” function , that takes the path of your image on your computer as argument. We can use the “resize” function to set the width and height of the picture (avoid to open big files) . A very useful function from OpenCV is the cvtColor , which convert the image “color type”. The type of the conversion is set on the third argument - on this case , the color from the original picture is RGB , and we want to convert to grayscale (CV_BGR2GRAY) and convert to HSV (CV_BGR2HSV).