opencv_tutorials_t1
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
opencv_tutorials_t1 [2016/06/06 14:03] – created joaomatos | opencv_tutorials_t1 [2017/05/13 16:55] (current) – [Using your Webcam Image] acater | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | Tutorial 1 - | + | ===== 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 ===== | ||
+ | |||
+ | {{ :: | ||
+ | |||
+ | < | ||
+ | |||
+ | //Blocks that we will need on this program | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | |||
+ | // declaring the namespaces | ||
+ | using namespace std; | ||
+ | using namespace cv; | ||
+ | |||
+ | |||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | // | ||
+ | Mat colorimage; | ||
+ | Mat grayimage; | ||
+ | Mat hsvimage; | ||
+ | |||
+ | //Read the color image from your PC. | ||
+ | //Change the argument of the imread function to your image' | ||
+ | colorimage = imread(" | ||
+ | |||
+ | //Resize function to resize your image | ||
+ | resize(colorimage, | ||
+ | |||
+ | //Convert Color function. | ||
+ | // | ||
+ | //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, | ||
+ | |||
+ | //COLOR TO HSV ( CV_BGR2HSV) | ||
+ | cvtColor(colorimage, | ||
+ | |||
+ | //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(" | ||
+ | namedWindow(" | ||
+ | namedWindow(" | ||
+ | |||
+ | //function to show the image on the specified window (first argument) | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | |||
+ | //Press any key to end the program | ||
+ | waitKey(0); | ||
+ | return 0; | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Understanding the Code ===== | ||
+ | |||
+ | < | ||
+ | |||
+ | //Blocks that we will need on this program | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | |||
+ | // 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 " | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | < | ||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | // | ||
+ | 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' | ||
+ | colorimage = imread(" | ||
+ | |||
+ | //Resize function to resize your image | ||
+ | resize(colorimage, | ||
+ | |||
+ | //Convert Color function. | ||
+ | // | ||
+ | //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, | ||
+ | |||
+ | //COLOR TO HSV ( CV_BGR2HSV) | ||
+ | cvtColor(colorimage, | ||
+ | |||
+ | </ | ||
+ | |||
+ | **Line 20 to 37** | ||
+ | |||
+ | First we have to open the image using the **" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | //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(" | ||
+ | namedWindow(" | ||
+ | namedWindow(" | ||
+ | |||
+ | //function to show the image on the specified window (first argument) | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | |||
+ | //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 **" | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | ===== 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 " | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | |||
+ | // declaring the namespaces | ||
+ | using namespace std; | ||
+ | using namespace cv; | ||
+ | |||
+ | |||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | // | ||
+ | Mat colorimage; | ||
+ | Mat grayimage; | ||
+ | Mat hsvimage; | ||
+ | |||
+ | //Open the default camera | ||
+ | VideoCapture capture(0); | ||
+ | |||
+ | //Check for Failure | ||
+ | if (!capture.isOpened()) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | //Set Capture device properties. | ||
+ | |||
+ | capture.set(CV_CAP_PROP_FRAME_WIDTH, | ||
+ | capture.set(CV_CAP_PROP_FRAME_HEIGHT, | ||
+ | |||
+ | char key=0; | ||
+ | //Loop will stop if " | ||
+ | while (key != ' | ||
+ | { | ||
+ | |||
+ | // | ||
+ | capture >> colorimage; | ||
+ | //Convert Color function. | ||
+ | // | ||
+ | //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, | ||
+ | |||
+ | //COLOR TO HSV ( CV_BGR2HSV) | ||
+ | cvtColor(colorimage, | ||
+ | |||
+ | // | ||
+ | //can be defined on the second argument | ||
+ | //Can be omitted ( just use imshow ) if you want to use the default window properties. | ||
+ | namedWindow(" | ||
+ | namedWindow(" | ||
+ | namedWindow(" | ||
+ | |||
+ | // | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | |||
+ | //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(" | ||
+ | } | ||
+ | |||
+ | //Set Capture device properties. | ||
+ | |||
+ | capture.set(CV_CAP_PROP_FRAME_WIDTH, | ||
+ | capture.set(CV_CAP_PROP_FRAME_HEIGHT, | ||
+ | |||
+ | </ | ||
+ | |||
+ | We changed the **" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | char key=0; | ||
+ | //Loop will stop if " | ||
+ | while (key != ' | ||
+ | { | ||
+ | //Capture a frame of the webcam live video and store it on the image variable | ||
+ | capture >> colorimage; | ||
+ | </ | ||
+ | The loop will run until the " | ||
+ | |||
+ | \\ | ||
+ | The video below demonstrates the program as it is run in real time. | ||
+ | \\ | ||
+ | {{youtube> |
opencv_tutorials_t1.1465247033.txt.gz · Last modified: by joaomatos