===== Tutorial 1 ===== On this tutorial you will learn how to open an image , and change from RGB colored to gray scale and HSV.At the end you will learn the same thing but using the live webcam stream. I recommend you to type the code on your own to get familiarized with the program language. If you have trouble , the original code is attached bellow ( Running on Visual Studio 2015 + OpenCV 3.1 ) * Check the installation guide to make sure that you linked all the OpenCV modules to your Visual Studio. {{::basic_operations.rar|Basic operations using image}} {{::basic_operations_using_webcam.rar|Basic operations using webcam}} ---- ===== Basic Operations ===== {{ ::basic_op.jpg?direct |}} //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; } ---- ===== Understanding the Code ===== //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; **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). The "usingnamespace" is convenient to declare always because it avoid us to type cv::OpenCVFunction or std::StandardFunction always before using the function for the opencv scope or standard scope. ---- int main(int argc, char *argv[]) { //Declaring variables to store the colored,gray scale and HSV images Mat colorimage; Mat grayimage; Mat hsvimage; **Line 13 to 19** To Store images we will the Mat variable type. ---- //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); **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 gray scale (CV_BGR2GRAY) and convert to HSV (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; } **Line 20 to 37** To show the results we first open a window , using the function **"namedWindow"** , which the second argument defines the window properties ( if it can be sized , etc.. ) . Then we use the **"imshow"** function to display the Mat variable (second argument) on the specified window ( first argument ). The **"namedWindow"** function can be omitted if you want to use the default window properties (auto size). ---- ===== Using your Webcam Image ===== To use the webcam live video instead of an saved image, you just need to insert a infinite do loop , and your video will be a collection of frames from your webcam. The new code will be. //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; //Open the default camera VideoCapture capture(0); //Check for Failure if (!capture.isOpened()) { printf("Failed to open the webcam"); } //Set Capture device properties. capture.set(CV_CAP_PROP_FRAME_WIDTH, 640); capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480); char key=0; //Loop will stop if "q" is pressed in the keyboard while (key != 'q') { //Capture a frame of the webcam live video and store it on the image variable capture >> colorimage; //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 q to end the program key = waitKey(25); } return 0; } ** What is changed ? ** //Open the default camera VideoCapture capture(0); //Check for Failure if (!capture.isOpened()) { printf("Failed to open the webcam"); } //Set Capture device properties. capture.set(CV_CAP_PROP_FRAME_WIDTH, 640); capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480); We changed the **"imread"** for the **"VideoCapture"** which will capture from your default video source (capture(0)) and the **"resize"** for the **"capture.set"** function. ---- char key=0; //Loop will stop if "q" is pressed in the keyboard while (key != 'q') { //Capture a frame of the webcam live video and store it on the image variable capture >> colorimage; The loop will run until the "q" key on the keyboard is pressed (we need to declare the key variable first ). The video shown on the images will be a collection of frames took from your webcam ( using the assign **capture >> colorimage**) the color image will store the webcam frame . The rest of the code is the same thing as the last code , converting to gray scale and HSV. At the end we just need to assign the **"waitKey"** command to the key variable and close the loop. \\ The video below demonstrates the program as it is run in real time. \\ {{youtube>PqW0GsMVRd0?medium}}