OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

26
Lections on Image analysis, OpenCV 4. Working with the channels, the threshold processing, flood fill. Example: Billiard balls detection USU / IMM Fall 2010 www.uralvision.blogspot.com [email protected] http://www.svi.nl/wikiimg/SeedAndThreshold_02.png

Transcript of OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

Page 1: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

Lections on Image analysis, OpenCV

4. Working with the channels, the threshold processing, flood fill. Example: Billiard balls detection

USU / IMM Fall 2010www.uralvision.blogspot.com [email protected]

http://www.svi.nl/wikiimg/SeedAndThreshold_02.png

Page 2: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

The format of the ABC

1. Function Name2. What it does3. For what it's used4. Announcement and description of the parameter list5. Example D:- Working code cpp, which reads the image, which carriesprocessing and displays the picture on the screen or writes to a file usingfunction imwrite- Input image (png or jpg)- The result of (png or jpg)

Page 3: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

General operations over images

Page 4: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

Common operations on images

split- Partitioning into channels

merge - Merging of channelsresize - change the size of (*)cvtColor - convert color spaces(*)

* - Functions for self-study inPractical Problem 1,see the distribution of surnames in uralvision.blogspot.com

Page 5: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

split and merge - description

Function split divides the multi-channel image into channels.

Function merge stitches together a single-image multi-channel.

Most often they are used to separately to each color image processing, as well as for various manipulations of the channels.

Page 6: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

split and merge - a list of options

Announcement and description of the parameter list:

void split (Const Mat & mtx, Vector <Mat> & mv )

mtx - the original color imagemv - the result set is 1-channel image

void merge (Const vector <Mat> & mv, Mat & dst )

mv - the original set of 1-channel imagedst - the resulting color image

Page 7: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

split and merge - an exampleTask - the input color image swap places red and blue channel, and to calculate the 1-channel image that represents the brightness.

Mat image = imread ("C:\\abc-blocks.png"); // load the input imageimshow ("Input image", image);

vector <Mat> planes;split(Image, planes);// Partition image into three channel planesimshow ("Blue", planes [0]); imshow ("Green", planes [1]); imshow ("Red", planes [2]);

vector <Mat> planesIzm (3);// Changes the Red and Blue sites:planesIzm [0] = planes [2]; planesIzm [1] = planes [1]; planesIzm [2] = planes [0];Mat imageIzm;merge(PlanesIzm, imageIzm);imshow ("Result", imageIzm);

// Calculating the brightness according to the formula 0.299 * R + 0.587 * G + 0.114 * B// (But, in fact, it's right to do with cvtColor)Mat gray = 0.299 * planes [2] + 0.587 * planes [1] + 0.114 * planes [0];imshow ("Gray", gray);

Page 8: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

split and merge - an example of application

Page 9: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

split and merge - with memory

1. Is your data when working with split and merge?

2. In the above example - if you change imageIzm, things will change whether the image?

- The answer to these questions isPractical, objective 2.

Page 10: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

Per-pixel operations

Page 11: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

Per-pixel operations

threshold- Threshold

adaptiveThreshold - Adaptive Threshold(*)min, max - min, max(*)abs - absolute value of taking (*)pow - exponentiation(*)sqrt - square root(*)randu - filled with random values(*)

Page 12: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

threshold - a description

Function thresholdperforms threshold processing of the image.

Most often it is used to highlight objects of interest pixels in the image.

Page 13: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

threshold - a list of options

Announcement and description of the parameter list:double threshold(Const Mat & src, Mat & dst,double thresh, Double maxVal,int thresholdType)

src anddst - Input and output 1-channel image.Allowed to be equal to dst src.thresh - ThresholdmaxVal - The new maximum value (used for THRESH_BINARY,THRESH_BINARY_INV)thresholdType - Type of the function of the threshold processing:

THRESH_BINARYTHRESH_BINARY_INVTHRESH_TRUNCTHRESH_TOZEROTHRESH_TOZERO_INVTHRESH_OTSU (- there is a threshold automatically, and it's return value)

Page 14: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

threshold - a list of optionsthresholdType - type thresholding function:

THRESH_BINARY

THRESH_BINARY_INV

THRESH_TRUNC

THRESH_TOZERO

THRESH_TOZERO_INV

THRESH_OTSU (- there is a threshold automatically, and it's return value)

Page 15: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

threshold - an example of applicationProblem - 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 imageimshow ("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 empiricallythreshold(Gray, gray, 50.0, 255.0, CV_THRESH_BINARY);

imshow ("Threshold", gray);

Page 16: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

threshold - an example of application

Please note: We have identified just pixels "not" field. To find the coordinates of the centers of balls and cue position - requires further processing.

Page 17: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

Working with Regions

Page 18: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

Working with Regions

floodFill- Allocation of connected regions

morphological operationsdilate - dilation(*)erode - Erosion(*)

Page 19: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

floodFill - description

Function floodFillprovides a fill area, starting from a pixel (x, y), with specified boundaries shutdownusing a 4 - or 8 - adjacency pixels.

Important: it spoils the original image - as it fills.

1. Most often it is used to highlight areas identified by the threshold processing, for subsequent analysis.

2. It can also be used to remove small noise on the binary image (in contrast to the "erosion + dilation" - do not spoil the boundaries of larger areas).

3.If enhance overall box found in the area of 1 pixel on all sides and make the fill, the way you can eliminate the internal hole in the area.

Page 20: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

floodFill - a list of options

Announcement and description of the parameter list:

int floodFill(Mat & image, Point seed, Scalar newVal, Rect * rect= 0 Scalar loDiff= Scalar (), Scalar upDiff= Scalar (),int flags= 4)image - The input image, 1 - or 3-channel, 8 or 32-bit.seed - Pixel, from which to start pouringrect - Bounding box found by the fieldloDiff, UpDiff - allowable difference with its neighbors(Or - with embryonic pixel, if flags | = FLOODFILL_FIXED_RANGE)that is, a new pixel must satisfy valueNewvalue - loDiff <= valueNew <= value + upDiff.flags = 4 or 8 - connectivity.

The resulting value - the number of pixels in the flooded area.

Page 21: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

floodFill - a list of options

Note about the types of OpenCV:

Point - Integer point to the fields int x, y;Rect - A rectangle with integer fieldsint x, y, width, height;Scalar - The representation of color,For example, Scalar (255) - 1-channel colorScalar (255, 255, 255) - 3-channel color

Page 22: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

floodFill - example of application

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 pre-defined boundaries.

const int minRectDim = 25; // Max and min size of the ballsconst int maxRectDim = 35;

// Iterate over the image pixelsfor (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// Of 200Rect rect;// Here is written Bounding Boxint count = floodFill(Gray, Point (x, y), Scalar (200), & rect);

Page 23: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

floodFill - example of application

// Check sizeif (rect.width> = minRectDim & & rect.width <= maxRectDim & & Rect.height> = minRectDim & & rect.height <= maxRectDim){ // Centerint x = rect.x + rect.width / 2;int y = rect.y + rect.height / 2;// Radiusint rad = (rect.width + rect.height) / 4;// Draw a circle the thickness of 2 pixelscircle (image, Point (x, y), rad, Scalar (255, 0, 255), 2);}} }}

imshow ("out", image);

Page 24: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

floodFill - example of application

Page 25: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

floodFill - example of application

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.

Page 26: OpenCV Lections: 4. Working with the channels, the threshold processing, flood fill

Homework

Practical task 1- See the names on uralvision.blogspot.com

Practical task 2- See text of split and merge.