0% found this document useful (0 votes)
7K views26 pages

OpenCV Lections: 4. Working With The Channels, The Threshold Processing, Flood Fill

The document discusses image processing techniques including working with channels, threshold processing, and flood fill. It provides examples of using the split and merge functions to separate and recombine image channels. It also demonstrates thresholding to highlight pixels and flood fill to identify connected regions in an image, giving examples of detecting billiard balls. The techniques discussed include splitting and recombining color channels, thresholding, and flood fill analysis to find objects in an image.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7K views26 pages

OpenCV Lections: 4. Working With The Channels, The Threshold Processing, Flood Fill

The document discusses image processing techniques including working with channels, threshold processing, and flood fill. It provides examples of using the split and merge functions to separate and recombine image channels. It also demonstrates thresholding to highlight pixels and flood fill to identify connected regions in an image, giving examples of detecting billiard balls. The techniques discussed include splitting and recombining color channels, thresholding, and flood fill analysis to find objects in an image.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Lections on Image analysis, OpenCV

4. Working with the channels,


the threshold processing, flood fill.
Example: Billiard balls detection

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

www.uralvision.blogspot.com [email protected]
USU / IMM Fall 2010
The format of the ABC
1. Function Name
2. What it does
3. For what it's used
4. Announcement and description of the parameter list
5. Example D:
- Working code cpp, which reads the image, which carries
processing and displays the picture on the screen or writes to a file using
function imwrite
- Input image (png or jpg)
- The result of (png or jpg)
General operations
over images
Common operations on images

split- Partitioning into channels

merge - Merging of channels


resize - change the size of (*)
cvtColor - convert color spaces(*)

* - Functions for self-study


inPractical Problem 1,
see the distribution of surnames in uralvision.blogspot.com
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.
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 image


mv - the result set is 1-channel image

void merge (Const vector <Mat> & mv, Mat & dst )
mv - the original set of 1-channel image
dst - the resulting color image
split and merge - an example
Task - 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 image


imshow ("Input image", image);

vector <Mat> planes;


split(Image, planes);// Partition image into three channel planes
imshow ("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);
split and merge - an example of
application
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 is


Practical, objective 2.
Per-pixel operations
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(*)
threshold - a description

Function thresholdperforms threshold processing of the image.

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


image.
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 - Threshold
maxVal - The new maximum value (used for THRESH_BINARY,
THRESH_BINARY_INV)
thresholdType - Type of the function of the threshold processing:
THRESH_BINARY
THRESH_BINARY_INV
THRESH_TRUNC
THRESH_TOZERO
THRESH_TOZERO_INV
THRESH_OTSU (- there is a threshold automatically, and it's return value)
threshold - a list of options
thresholdType - 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)


threshold - an example of application
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);


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.
Working with Regions
Working with Regions
floodFill- Allocation of connected regions

morphological operations
dilate - dilation(*)
erode - Erosion(*)
floodFill - description

Function floodFillprovides a fill area, starting from a pixel (x, y), with specified
boundaries shutdown
using 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.
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 pouring
rect - Bounding box found by the field
loDiff, UpDiff - allowable difference with its neighbors
(Or - with embryonic pixel, if
flags | = FLOODFILL_FIXED_RANGE)
that is, a new pixel must satisfy valueNew
value - loDiff <= valueNew <= value + upDiff.
flags = 4 or 8 - connectivity.

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


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 fields
int x, y, width, height;
Scalar - The representation of color,
For example, Scalar (255) - 1-channel color
Scalar (255, 255, 255) - 3-channel color
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 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
// Of 200
Rect rect;// Here is written Bounding Box
int count = floodFill(Gray, Point (x, y), Scalar (200), & rect);
floodFill - example of application
// 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);


floodFill - example of application
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.
Homework

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

Practical task 2
- See text of split and merge.

You might also like