opencv_tutorials_t2
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
opencv_tutorials_t2 [2016/06/06 14:05] – created joaomatos | opencv_tutorials_t2 [2017/05/13 17:04] (current) – [Understanding the Code] acater | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | Tutorial 2) | + | ===== Tutorial 2 ===== |
+ | |||
+ | On this tutorial you will learn how to access a specific pixel in an image , and how to read the properties of this pixel ( RGB value and position | ||
+ | |||
+ | 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. | ||
+ | |||
+ | {{:: | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Pixel Operations ===== | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace cv; | ||
+ | using namespace std; | ||
+ | |||
+ | |||
+ | Mat image; | ||
+ | |||
+ | //function to get the pixel RGB value at the given y column and x row | ||
+ | static void onMouse(int event, int x, int y, int f, void*) { | ||
+ | |||
+ | Vec3b pix = image.at< | ||
+ | int B = pix.val[0]; | ||
+ | int G = pix.val[1]; | ||
+ | int R = pix.val[2]; | ||
+ | |||
+ | cout << " | ||
+ | cout << " | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | int main(int argc, char** argv) | ||
+ | { | ||
+ | |||
+ | //Read the image from your archive and resize | ||
+ | image = imread(" | ||
+ | resize(image, | ||
+ | |||
+ | //show the image in a window | ||
+ | imshow(" | ||
+ | |||
+ | //use function setMouseCallback with our onMouse function to get the pixel properties | ||
+ | //on the mouse pointer location | ||
+ | setMouseCallback(" | ||
+ | |||
+ | waitKey(0); | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Understanding the Code ===== | ||
+ | |||
+ | Now that you have followed tutorial 1 , we already know how to open an image and how the program works. Lets analyze the new things on this tutorial. | ||
+ | |||
+ | < | ||
+ | //function to get the pixel RGB value at the given y column and x row | ||
+ | static void onMouse(int event, int x, int y, int f, void*) { | ||
+ | |||
+ | Vec3b pix = image.at< | ||
+ | int B = pix.val[0]; | ||
+ | int G = pix.val[1]; | ||
+ | int R = pix.val[2]; | ||
+ | |||
+ | cout << " | ||
+ | cout << " | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | We can define a function to be called on the main program to get the RGB values (On OpenCV we always read BGR ) and the pixel' | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | //use function setMouseCallback with our onMouse function to get the piexel properties | ||
+ | //on the mouse pointer location | ||
+ | setMouseCallback(" | ||
+ | |||
+ | </ | ||
+ | |||
+ | On the main program we call our onMouse function using the setMouseCallback . It will set the mouse handler for the first argument window (which we opened using the imread function ). On this case we don't set any event (just call if a certain mouse button is pressed ). If you want to call the function just in specific cases , you can find an example [[http:// | ||
+ | \\ | ||
+ | \\ | ||
+ | The video below will demonstrate the program in real time. | ||
+ | {{youtube> |
opencv_tutorials_t2.1465247118.txt.gz · Last modified: by joaomatos