Professional Documents
Culture Documents
Tuan 5 Morphological Operation Principle
Tuan 5 Morphological Operation Principle
Disc
A B z | ( B) Z Ac
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 0
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 0 0
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 0 0 0
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 0 0 0 0
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 0 0 0 0 1
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 0 0 0 0 1 0
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 0 0 0 0 1 0 0
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 0 0 0 0 1 0 0 0
Objects
Set threshold
here
9/19/2019 Introduction to Machine Vision
BINARY IMAGE
Problem here
Hàm getStructuringElement():
cv::getStructuringElement(int shape, Size ksize, Point anchor = Point(-1,-1));
Với:
shape: Là hình dạng của phần tử cấu trúc. Ví dụ: MORPH_RECT. Bạn có thể tham
khảo các loại hình dạng của phần tử cấu trúc trong cv::MorphShapes.
ksize: Là kích thước của ma tận phần tử cấu trúc.
anchor: Là điểm neo của phần tử cấu trúc. Giá trị mặc định là (-1, -1).
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 1
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 1 0
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 1 0 1
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 1 0 1 1
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 1 0 1 1 1
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 1 0 1 1 1 1
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 1 0 1 1 1 1 1
Input image 1 0 0 0 1 1 1 0 1 1
Structuring Element 1 1 1
Output Image 1 0 1 1 1 1 1 1
33
Robotic & Vision Lab RoVis
Exercise
• erosion
• removal of structures of certain shape and size, given by
SE
• Dilation
• filling of holes of certain shape and size, given by SE
36
Robotic & Vision Lab RoVis
Exercise
• A) erode by hand
• B) erode by programing do not use erode function
• C) using erode function to compare
• D) dilate by hand
• E) dilate by programing do not use dilate function
• F) using dilate function to compare
A B ( A B ) B
• First – erode A by B, and then dilate the result by B
First – erode A by B, and then dilate the result by B
• eliminates protrusions
• breaks necks
• smoothes contour
In other words, opening is the unification of all B objects
Entirely Contained in A
38
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Opening
3*9
9*3
A B ( A B) B
First – dilate A by B, and then erode the result by B
• smooth contour
• fuse narrow breaks and long thin gulfs
• eliminate small holes
• fill gaps in the contour
In other words, closing is the group of points, which the
intersection of object B around them with object A – is not
empty
47
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Closing
1. Threshold
2. Closing with disc of size 20
19-Sep-19
Thresholded closed
52
Robotic & Vision Lab RoVis
A) open by hand
B) open by programing do not use morphologyex function
C) using morphologyex function to compare
D) close by hand
E) close by programing do not use morphologyex function
F) using close function to compare
Blackhat
Robotic & Vision Lab RoVis
createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat",
window_name, &morph_operator, max_operator, Morphology_Operations);
/// Create Trackbar to select kernel type
createTrackbar("Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name, &morph_elem, max_elem,
Morphology_Operations);
/// Create Trackbar to choose kernel size
createTrackbar("Kernel size:\n 2n +1", window_name, &morph_size, max_kernel_size,
Morphology_Operations);
/// Default start
Morphology_Operations(0, 0);
waitKey(0);
return 0;
}
/**
* @function Morphology_Operations
*/
void Morphology_Operations(int, void*)
{
// Since MORPH_X : 2,3,4,5 and 6
int operation = morph_operator + 2;
60
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Some Basic Morphological Algorithms (1)
• Boundary Extraction
The boundary of a set A, can be obtained by first eroding A
by B and then performing the set difference between A and
its erosion.
( A) A A B
• Hole Filling
A hole may be defined as a background region surrounded
by a connected border of foreground pixels.
• Hole Filling
1. Forming an array X0 of 0s (the same size as the array
containing A), except the locations in X0 corresponding to
the given point in each hole, which we set to 1.
c
2. Xk = (Xk-1 + B) A k=1,2,3,…
cv::Mat mask;
image_thresh.copyTo(mask);
for (int i = 0; i < mask.cols; i++) {
if (mask.at<char>(0, i) == 0) {
cv::floodFill(mask, cv::Point(i, 0), 255, 0, 10, 10);
}
if (mask.at<char>(mask.rows - 1, i) == 0) {
cv::floodFill(mask, cv::Point(i, mask.rows - 1), 255, 0, 10, 10);
}
}
for (int i = 0; i < mask.rows; i++) {
if (mask.at<char>(i, 0) == 0) {
cv::floodFill(mask, cv::Point(0, i), 255, 0, 10, 10);
}
if (mask.at<char>(i, mask.cols - 1) == 0) {
cv::floodFill(mask, cv::Point(mask.cols - 1, i), 255, 0, 10, 10);
}
}
B B1 , B2
B1 : object
B2 : background
A B A B1 ( Ac B2 )
with 0-1=0!!
• If foreground and background fit the structuring
element exactly, then the pixel at the origin of the
SE is set to 0
B B1
, B 2
, B 3
,..., B n
where B i is a rotated version of B i -1
Kernel:
• Thickening:
K max{k | A kB };
Với
Sk ( A) ( A kB) ( A kB) B
original skeleton
with 1+1=1
• If foreground and background match exactly the SE,
then set the pixel at its origin to 1!
• Note that the value of the SE at the origin is 0 or
don’t care!
Kernel: