Opencv erode binary image edge
Apply two very common morphology operators i. Dilation and Erosionwith the creation of custom kernels, in order to extract straight lines on the horizontal and vertical axes. For this purpose, you will opencv erode binary image edge the following OpenCV functions:. Morphology is a set of image processing operations that process images based on predefined structuring elements known also as kernels. The value of each pixel in the output image is based on a comparison of the corresponding pixel in the input image with its neighbors.
By choosing the size and shape of the kernel, you can construct a morphological operation that is sensitive to specific shapes regarding the input image. Two of the most basic morphological operations are dilation and erosion. Opencv erode binary image edge adds pixels to the boundaries opencv erode binary image edge the object in an image, while erosion does exactly the opposite.
The amount of pixels added or removed, respectively depends on the size and shape of the structuring element used to process the image. In general the rules followed from these two operations have as follows:. The value of the output pixel is the maximum value of all the pixels that fall within opencv erode binary image edge structuring element's size and shape. For example in a binary image, if any of the pixels of the input image falling within the range of the kernel is set to the value 1, the corresponding pixel of the output image will be set to 1 as well.
The latter applies to any type of image e. The vise versa applies for the erosion operation. The value of the output pixel is the minimum value of all the pixels that fall within the structuring element's size and shape.
Look the at the example figures below:. As it can be seen above and in general in any opencv erode binary image edge operation the structuring element used to probe the input image, is the most important part. A structuring element is a matrix consisting of only 0's and 1's that can have any arbitrary shape and size. Typically are much smaller than the image being processed, while the pixels with values of 1 define the neighborhood.
The center pixel of the structuring element, called the origin, identifies the pixel of interest — the pixel being processed. A structuring element can have many common shapes, such as lines, diamonds, disks, periodic lines, and circles and sizes. For example, to find lines in an image, create a linear structuring element as you will see later. This tutorial code's is shown lines below. You can also download it from here. Extract horizontal and vertical lines by using morphological operations.
Goal In this tutorial you will learn how to: For this purpose, you will use the following OpenCV functions: Theory Morphology Operations Morphology is a set of image processing operations that process images based on predefined structuring elements known also as kernels. In general the rules followed from these two operations have as follows: Dilation on a Binary Image.
Dilation on a Grayscale Image. Erosion on a Binary Image. Erosion on a Grayscale Image.
Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original image, second one is called structuring opencv erode binary image edge or kernel which decides the nature of operation.
Two basic morphological operators are Erosion and Dilation. Then its variant forms like Opening, Closing, Gradient etc also comes into play. We will see them one-by-one with help of following image:. The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of foreground object Always try to keep foreground in white.
So what it does? The kernel slides through the image as in 2D convolution. A pixel in the original image either 1 or 0 will be considered 1 only if all the pixels under the kernel is 1, otherwise it is eroded opencv erode binary image edge to zero.
So what happends is that, all the pixels near boundary will be discarded opencv erode binary image edge upon the size of kernel. So the thickness or size of the foreground object decreases or simply white region decreases in the image. It is useful for removing small white noises as we have seen in colorspace chapterdetach two connected objects etc. It is just opposite of erosion. Here, a pixel element is '1' if atleast one pixel under the kernel is '1'.
So it increases the white region in the image or size of foreground object increases. Normally, in cases like opencv erode binary image edge removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our object. So we dilate it.
Since noise is gone, they won't come back, but our object area increases. It is also useful in joining broken parts of an object. Opening is just another name of erosion followed by dilation. It is useful in removing noise, as we explained above.
Here we use the function, cv. Closing is reverse of Opening, Dilation followed by Erosion. It is useful in closing small holes inside the foreground objects, or small black points on the object. It is the difference between input image and Opening of the image. Below example is done for a 9x9 kernel. We manually created a structuring elements in the previous examples with help of Numpy.
It is rectangular shape. So for this purpose, OpenCV has a function, cv. You just pass the shape and size of the kernel, you get the desired kernel. We will see different functions like: Theory Morphological transformations are some simple operations based on the image opencv erode binary image edge. We will see them one-by-one with help of following image:
It defines a series of operators that transform an image by probing it with a predefined shape element. Opencv erode binary image edge way this shape element intersects the neighborhood of a pixel determines the result of the operation. This chapter presents the most important morphological operators. It also explores the problems of image segmentation and feature detection using algorithms based on morphological operators. As morphological filters often work on binary images, we will use the binary image that was created through thresholding in the first recipe of the previous chapter.
Opencv erode binary image edge, since the convention is to have the foreground objects represented by high white pixel values and the background objects by low black pixel values in morphology, we have negated the image. In morphological terms, the following image is said to be the complement of the image that was created in the previous chapter:. The first screenshot shows erosion:. Since the input binary image contains only black 0 and white pixels, each pixel is replaced by either a white or black pixel.
A good way to picturize the effect of these two operators is to think in terms of background black and foreground white objects. With erosion, if the structuring element when opencv erode binary image edge at a given pixel location touches the background that is, one of the pixels in the intersecting set is blackthen opencv erode binary image edge pixel will be sent to the background.
In the case of dilation, if the structuring element on a background pixel touches a foreground object, then this pixel will be assigned opencv erode binary image edge white value. This explains why the size of the objects has been reduced the shape has been eroded in the eroded image. Similarly, the dilated objects are now larger, and some of the "holes" inside them have opencv erode binary image edge filled.
By default, OpenCV uses a 3 x 3 square structuring opencv erode binary image edge. Mat is specified as the third argument in the function call, as it was done in the preceding example. In the following example, a 7 x 7 structuring element is applied:. Another way to opencv erode binary image edge the same result is to repetitively apply the same structuring element on an image. The two functions have an optional parameter to specify the number of repetitions:.
Indeed, eroding an image twice is similar to eroding an image with a structuring element dilated with itself. This also applies to dilation. Eroding the foreground objects with a structuring element can be seen as a dilation of the background part of the image. In other words, opencv erode binary image edge can make the following observations:. Note that even though we applied our morphological filters on binary images here, these filters can be applied on gray-level or even color images with the same definitions.
Also, note that the OpenCV morphological functions support in-place processing. This means that you can use the input image as the destination image, as follows:. The opening and closing operators are presented in this recipe. For example, the following call will apply the closing operator:. Note that we used a 5 x 5 structuring element to make the effect of the filter more apparent.
If we use the binary image of the preceding recipe as input, we will obtain an image similar to what's shown in the following screenshot:. Opencv erode binary image edge opening filter can be obtained by reverting these opencv erode binary image edge function calls.
While examining the result of the closing filter, it can be seen that the small holes of the white foreground objects have been filled. Basically, any holes or gaps that are too small to completely contain the structuring element will be eliminated by the filter. All the objects that were too small to contain the structuring element have been removed.
Therefore, it is advantageous to use them in a sequence. If our test binary image is successively closed and opened, we obtain an image that shows only the main objects in the scene, as shown in the following screenshot. You can also apply the opening filter before the closing filter if you wish to prioritize noise filtering, but this will be at the price of eliminating some fragmented objects.
Note that applying the same opening and similarly the closing operator on an image several times has no effect. Indeed, as the holes have been filled by the first opening filter an additional application of the same filter will not produce any other changes to the image.
In mathematical terms, these operators are said to be idempotent. Refer to the following code:. Indeed, this requires opencv erode binary image edge different structuring opencv erode binary image edge shaped as a square, diamond, cross, and X-shape to be defined in the constructor all these structuring elements have a fixed 5 x 5 dimension for simplicity:.
In the detection of corner opencv erode binary image edge, all these structuring elements are applied in a cascade to obtain the resulting corner map:. Also, since edges correspond to a rapid transition between the dark and bright pixels, these can be pictured as abrupt cliffs. If an erosion operator is applied on such a terrain, the net result will be to replace each pixel by the lowest opencv erode binary image edge in a certain neighborhood, thus reducing its height.
As a result, cliffs will be "eroded" as the valleys expand. Dilation has the exact opposite effect; that is, cliffs will gain terrain over the valleys.
However, in both cases, the plateaux that is, the area of constant intensity will remain relatively unchanged. These observations lead to a simple way to detect the edges or cliffs of an image. This can be done by computing the difference between the dilated and eroded images. Since these two transformed images differ mostly at the edge locations, the image edges will be emphasized by the subtraction. Obviously, the larger the structuring element is, the thicker the detected edges will be.
Note that similar results can also be obtained by simply subtracting the original image from the dilated one or the eroded image from the original. The resulting edges would be thinner.
This operator is not implemented in OpenCV, but we present it here to demonstrate how the structuring elements of various shapes can be defined and combined. The idea is to close the image by dilating and eroding it with two different structuring elements. These elements are chosen such that they leave straight edges unchanged, but because of their respective effects, the edges at corner points will be affected.
Let's use the simple following image made of a single white opencv erode binary image edge to better understand the effect of this asymmetrical closing operation:. The first square is the original image.
When dilated with a cross-shaped structuring element, the square edges expand, except at the corner opencv erode binary image edge where the cross shape does not opencv erode binary image edge the square. The rightmost square is then obtained, which as it can be seen has lost its sharp corners.
These two elements are the rotated versions of the previous elements and will consequently capture the corners at a degree orientation. Finally, differencing the two results will extract the corner features. Gaddipati, Pattern Recognition Letters, volume 26, issue 7, Maygives more information on morphological corner detection. As a result of its simplicity, the original version of this algorithm tends to over-segment the image, which produces multiple small regions.
This is why OpenCV proposes a variant of this algorithm that uses a set of predefined markers that guide the definition of the image segments. The input for this function is a bit signed integer-marker image in which each nonzero pixel represents a label. The idea is to mark some pixels of the image that are known to belong to a given region. From this initial labeling, the watershed algorithm will determine the regions to which the other pixels belong.
The way these markers are obtained depends on the application. For example, some preprocessing steps might have resulted in the identification of some pixels that belong to an object of interest.
The watershed would then be used to delimitate the complete object from that initial detection. Therefore, from our binary image, we need to identify the pixels that belong to the foreground the animals and the pixels that belong to the background mainly the grass. As of now, the binary image includes too many white pixels that belong to the various parts of the image. We will then severely erode this image in order to retain only the pixels that belong to the important objects:.
Similarly, we also select a few pixels of the background by a large dilation of the original binary image:. The resulting black pixels correspond to the background pixels. The following image is obtained:. The following image will be used as the input to the watershed algorithm:. The segmentation is then obtained as follows:. The resulting image of the labels is as follows:. In order to create a watershed segmentation, the idea is to opencv erode binary image edge flood the image starting at level 0.
As the level of "water" progressively increases to levels 1, 2, 3, and so oncatchment basins are formed. The size of these basins also gradually increases and, consequently, the water of two different basins will eventually merge. When this happens, a watershed is created in order to keep the two basins separate.
Once the level of water has reached its maximal level, the sets of these created basins and watersheds form the watershed segmentation. When all of these are opencv erode binary image edge, many watershed lines are created, which results in an over-segmented image. To overcome this problem, a modification to this algorithm has been proposed in which the flooding process starts from a predefined set of marked pixels.
The basins created from these markers are labeled in accordance with the values assigned to the initial marks. When two basins having the same label merge, no watersheds are opencv erode binary image edge, thus preventing over-segmentation. The input marker image is updated to produce the final watershed segmentation.
To facilitate the display of the result, we have introduced two special methods. This is easily done through thresholding, as follows:. This is due to the saturation operation that is applied when signed integers are converted into unsigned characters. Obviously, the marker image can be obtained in many different ways. Alternatively, in an attempt to identify an opencv erode binary image edge located at the center of an image, one can also simply input an image with the central area marked with a certain label and the border of the image where the background is assumed to be present marked with another label.
This marker image can be created as follows:. Scalar ,3. Meyer, Journal of Mathematical Imaging and Vision, volume 22, issueMaygives more information on the watershed transform.