More Morphology Transformations
2013-12-22 21:31
357 查看
Goal
In this tutorial you will learn how to:Use the OpenCV function
morphologyEx to apply Morphological Transformation such as:
Opening
Closing
Morphological Gradient
Top Hat
Black Hat
Theory
NoteThe explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.
In the previous tutorial we covered two basic Morphology operations:
Erosion
Dilation.
Based on these two we can effectuate more sophisticated transformations to our images. Here we discuss briefly 05 operations offered by OpenCV:
Opening
It is obtained by the erosion of an image followed by a dilation.Useful for removing small objects (it is assumed that the objects are bright on a dark foreground)
For instance, check out the example below. The image at the left is the original and the image at the right is the result after applying the opening transformation. We can observe that the small spaces in the corners of the letter tend to dissapear.
Closing
It is obtained by the dilation of an image followed by an erosion.Useful to remove small holes (dark regions).
Morphological Gradient
It is the difference between the dilation and the erosion of an image.It is useful for finding the outline of an object as can be seen below:
Top Hat
It is the difference between an input image and its opening.Black Hat
It is the difference between the closing and its input imageCode
This tutorial code’s is shown lines below. You can also download it fromhere
#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdlib.h> #include <stdio.h> using namespace cv; /// Global variables Mat src, dst; int morph_elem = 0; int morph_size = 0; int morph_operator = 0; int const max_operator = 4; int const max_elem = 2; int const max_kernel_size = 21; char* window_name = "Morphology Transformations Demo"; /** Function Headers */ void Morphology_Operations( int, void* ); /** @function main */ int main( int argc, char** argv ) { /// Load an image src = imread( argv[1] ); if( !src.data ) { return -1; } /// Create window namedWindow( window_name, CV_WINDOW_AUTOSIZE ); /// Create Trackbar to select Morphology operation 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; Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) ); /// Apply the specified morphology operation morphologyEx( src, dst, operation, element ); imshow( window_name, dst ); }
Explanation
Let’s check the general structure of the program:Load an image
Create a window to display results of the Morphological operations
Create 03 Trackbars for the user to enter parameters:
The first trackbar “Operator” returns the kind of morphology operation to use (morph_operator).
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 );
The second trackbar “Element” returns morph_elem, which indicates what kind of structure our kernel is:
createTrackbar( "Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name, &morph_elem, max_elem, Morphology_Operations );
The final trackbar “Kernel Size” returns the size of the kernel to be used (morph_size)
createTrackbar( "Kernel size:\n 2n +1", window_name, &morph_size, max_kernel_size, Morphology_Operations );
Every time we move any slider, the user’s function Morphology_Operations will be called to effectuate a new morphology operation and it will update the output image based on the current trackbar values.
/** * @function Morphology_Operations */ void Morphology_Operations( int, void* ) { // Since MORPH_X : 2,3,4,5 and 6 int operation = morph_operator + 2; Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) ); /// Apply the specified morphology operation morphologyEx( src, dst, operation, element ); imshow( window_name, dst ); }
We can observe that the key function to perform the morphology transformations is
morphologyEx. In this example we use four arguments (leaving the rest as defaults):
src : Source (input) image
dst: Output image
operation: The kind of morphology transformation to be performed. Note that we have 5 alternatives:
Opening: MORPH_OPEN : 2
Closing: MORPH_CLOSE: 3
Gradient: MORPH_GRADIENT: 4
Top Hat: MORPH_TOPHAT: 5
Black Hat: MORPH_BLACKHAT: 6
As you can see the values range from <2-6>, that is why we add (+2) to the values entered by the Trackbar:
int operation = morph_operator + 2;
element: The kernel to be used. We use the function
getStructuringElement to define our own structure.
Results
After compiling the code above we can execute it giving an image path as an argument. For this tutorial we use as input the image:baboon.png:
And here are two snapshots of the display window. The first picture shows the output after using the operator
Opening with a cross kernel. The second picture (right side, shows the result of using a
Blackhat operator with an ellipse kernel.
====================
Smoothing with MORPH_OPEN
TheMORPH_OPEN function applies the opening operation, which is erosion followed by dilation, to a binary or grayscale image. The opening operation removes noise from an image while maintaining the overall sizes of objects in the foreground. Opening is a useful
process for smoothing contours, removing pixel noise, eliminating narrow extensions, and breaking thin links between features. After using an opening operation to darken small objects and remove noise, thresholding or other morphological processes can be applied
to the image to further refine the display of the primary shapes within the image.
The following example applies the opening operation to an image of microscopic spherical organisms,
Rhinosporidium seeberi protozoans. After applying the opening operation and thresholding the image, only the largest elements of the image are retained, the mature
R.seeberi organisms. Complete the following steps for a detailed description of the process.
Example Code See morphopenexample.proin the examples/doc/imagesubdirectory of the IDL installation directory for code that duplicates this example. |
DEVICE, DECOMPOSED = 0, RETAIN = 2 LOADCT, 0
Select and open the image file:
file = FILEPATH('r_seeberi.jpg', $ SUBDIRECTORY = ['examples', 'data']) READ_JPEG, file, image, /GRAYSCALE
Get the image dimensions, prepare a window and display the image:
dims = SIZE(image, /DIMENSIONS) WINDOW, 0, XSIZE = 2*dims[0], YSIZE = 2*dims[1], $ TITLE = 'Defining Shapes with Opening Operation' TVSCL, image, 0
Define the radius of the structuring element and create a disk-shaped element to extract circular features:
radius = 7 strucElem = SHIFT(DIST(2*radius+1), radius, radius) LE radius
Compared to the previous example, a larger element is used in order to retain only the larger image elements, discarding all of the smaller background features. Further increases in the size of the structuring element would extract
even larger image features.
Tip Enter PRINT, strucElemto view the structure created by the previous statement. |
MORPH_OPEN function to the image, specifying the GRAY keyword for the grayscale image:
morphImg = MORPH_OPEN(image, strucElem, /GRAY)
Display the image:
TVSCL, morphImg, 1
The following figure shows the original image (left) and the application of the opening operation to the original image (right). The opening operation has enhanced and maintained the sizes of the large bright objects within the
image while blending the smaller background features.
Figure 9-6: Application of the Opening Operation to a Grayscale Image
Create a window and use HISTOGRAM in conjunction with PLOT, displaying an intensity histogram to help determine the threshold intensity value:
WINDOW, 1, XSIZE = 400, YSIZE = 300 PLOT, HISTOGRAM(img)
Note Using an intensity histogram as a guide for determining threshold values is described in the section, Determining Intensity Values for Threshold and Stretch. |
threshImg = image GE 160 WSET, 0 TVSCL, threshImg, 2
Apply the opening operation to the binary image to remove noise and smooth contours, and then display the image:
morphThresh = MORPH_OPEN(threshImg, strucElem) TVSCL, morphThresh, 3
The combination of thresholding and applying the opening operation has successfully extracted the primary foreground features as shown in the following figure.
Figure 9-7: Binary Image (left) and Application of the Opening Operator to the Binary Image (right)
相关文章推荐
- SQLServer的数据类型
- yum报错的解决方法
- window 该磁盘处于脱机状态,因为它与另一个联机磁盘发生签名冲突
- Linux编程实现一个简单的Shell
- java连接oracle
- 第三次软工
- fedora 20 软件
- 设置Android模拟器上网的方法
- Makefile系列之五 :函数
- 数字的正则表达式以及Demo测试页面
- 风电点一些基本知识
- 设计模式之策略模式
- 我的写作
- Thrift解读(一)——TNonblockingServer 连接管理
- Mybatis常见错误
- 手势
- 从基础开始,从一个SQLHelper开始
- Add Subtitle (srt) to Video by using Ffmpeg
- 机房收费系统—封装函数
- error LNK2001: unresolved external symbol __imp__PathFileExistsA@4