Open Frameworks and OpenCV
Open Frameworks and OpenCV
Open Frameworks and OpenCV
See in-depth details in my book Mastering openFrameworks Books examples are free, see masteringof.wordpress.com
// OF_IMAGE_COLOR - 3-byte color. // OF_IMAGE_GRAYSCALE - 1-byte gray. // OF_IMAGE_COLOR_ALPHA - 4-byte, color + transparency.
Introduction to OpenCV
- What is OpenCV - The first project to OpenCV - Class Mat - Image processing functions
What is OpenCV
"Open Computer Vision Library" Open library with a set of functions for processing, analysis and image recognition, C / C + +.
What is OpenCV
2000 - First alpha version, support for Intel, C-interface 2006 - Version 1.0 2008 - Support Willow Garage (lab. Robotics) 2009 - version 2.0, classes in C + + 2010 - version 2.2, realized work with the GPU
2. Create a console project File - New - Project - Win32 Console Application, in the Name enter Project1, click OK.
3. Set up the path Alt + F7 - opens the project properties Configuration Properties - C / C + + - General - Additional Include Directories, where we put the value "C: \ Program Files \ OpenCV2.1 \ include \ opencv"; Linker - General - Additional Library Directories, where we put the value of C: \ Program Files \ OpenCV2.1 \ lib \
Linker - Input - Additional Dependencies cv210.lib cvaux210.lib cxcore210.lib cxts210.lib highgui210.lib for Release, cv210d.lib cvaux210d.lib cxcore210d.lib cxts210.lib highgui210d.lib for Debug
The first project to OpenCV 2. Reading the image and display it on screen
1. Preparing the input data: file http://www.fitseniors.org/wp-content/uploads/2008/04/green_apple.jpg write in C: \ green_apple.jpg 2. Writing in Project1.cpp: # Include "stdafx.h" # Include "cv.h" # Include "highgui.h" using namespace cv; int main (int argc, const char ** argv) { Mat image = imread ("C:\\green_apple.jpg");// Load image from disk imshow ("image", image); // Show image waitKey (0); // Wait for keystroke return 0; } 3. Press F7 - compilation, F5 - run. The program will show the image in the window and by pressing any key will complete its work.
Mat Class
Mat - Base class for storing images OpenCV.
// Show the channels in separate windows // Note that the red channel - 2, not 0.
imshow ("Red", channels [2]); imshow ("Green", channels [1]); imshow ("Blue", channels [0]); waitKey (0); return 0; }
Original image
http://www.innocentenglish.com/funny-pics/best-pics/stairs-sidewalk-art.jpg
http://www.svi.nl/wikiimg/SeedAndThreshold_02.png
FunctionfloodFillprovides a fill area, starting from a pixel (x, y), with specified boundaries shutdown using a 4 - or 8 - adjacency pixels. It is important: It spoils the original image - as it fills. Most often it is used to highlight areas identified by the threshold processing, for subsequent analysis.
http://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Square_4_connectivity.svg/300px-Square_4_connectivity.svg.png http://tunginobi.spheredev.org/images/flood_fill_ss_01.png
The contour of the object - this is the line representing the edge of the object's shape. Underline the contour points -Sobel, Leased-line circuit -Canny. Application 1. Recognition. Along the contour can often determine the type of object that we observe. 2. Dimension. With the circuit can accurately estimate the size of the object of their rotation, and location.
http://howto.nicubunu.ro/gears/gears_16.png http://cvpr.uni-muenster.de/research/rack/index.html
Problem
The image of billiard field to find the coordinates of the centers of billiard balls. Algorithm:
1. Threshold find bright pixels. 2. Analysis of areas. The method of casting we find connected regions, Among them we find such dimensions that allow it balls.
1. Threshold
Problem - the image of billiard field highlight the pixels that are not field (shooting conditions such that the field - dark) Mat image = imread ("C:\\billiard.png"); // load the input image imshow ("Input image", image); vector <Mat> planes; split (image, planes); Mat gray = 0.299 * planes [2] + 0.587 * planes [1] + 0.114 * planes [0]; double thresh = 50.0; // The threshold is chosen empirically threshold(Gray, gray, 50.0, 255.0, CV_THRESH_BINARY); imshow ("Threshold", gray);
Please note: We have identified just pixels "not" field. To find the coordinates of the centers of balls and cue position - requires further processing.
2. Analysis areas
floodFill- Allocation of connected regions
floodFill - description
Function floodFillprovides a fill area, starting from a pixel (x, y), with specified boundaries shutdown using a 4 - or 8 - adjacency pixels.
2. Analysis areas
Problem - the image of billiard glades find billiard balls - ie. compute their centers and sizes. The idea - using the example of the result threshold, through all connected regions with floodFill, and the found areas to consider those balls whose sizes lie in the predefined boundaries. const int minRectDim = 25; // Max and min size of the balls const int maxRectDim = 35; // Iterate over the image pixels for (int y = 0; y <gray.rows; y + +) { for (int x = 0; x <gray.cols; x + +) { int value = gray.at <uchar> (y, x); if (value == 255) {// If the value of - 255, fill it 200 Rect rect;// Here is written Bounding Box int count = floodFill(Gray, Point (x, y), Scalar (200), & rect);
Analysis areas
// Check size if (rect.width> = minRectDim && rect.width <= maxRectDim && Rect.height> = minRectDim && rect.height <= maxRectDim) { // Center int x = rect.x + rect.width / 2; int y = rect.y + rect.height / 2; // Radius int rad = (rect.width + rect.height) / 4; // Draw a circle the thickness of 2 pixels circle (image, Point (x, y), rad, Scalar (255, 0, 255), 2); } } } } imshow ("out", image);
Comments
In this example, we considered the simplest method for finding the ball in the picture - by analyzing the sizes of bounding boxes. Such an analysis works on the assumption that the image no other sites with similar bounding boxes. For a real application, a more detailed analysis of areas. This is primarily due to the fact that if the balls are near each other, then they can "stick together" in one connected region. Possible approaches to solving this problem: 1. To fill the interior area, select the path obtained by field and assess its areas of convexity and concavity for the selection of balls. 2. Use template "round", which is applied to the obtained area and look for the best of its location.
Debugging in OpenCV
To debug the project, which is being processed by OpenCV, very useful to display intermediate images by usingimshow imshow ("image", image); - It displays the image in the image window with the heading "image". Warning: 1. need # Include "highgui.h" 2. if you display images in the window with the same name, then only the last image will be visible.
Homework
Do a project on openFrameworks, which 1) receive a picture from the camera 2) then this picture is transmitted in OpenCV, where she - Smoothed - Is the threshold processing 3) The picture with the camera and the resulting image is displayed on the screen using openFrameworks.