How To Make Morphological Gradient Work With C#
Morphological gradient is a grayscale morphological operation in image processing, which emphasized boundaries and supresses homogenous areas
Filter by Category
Morphological gradient is a grayscale morphological operation in image processing, which emphasized boundaries and supresses homogenous areas
Morphological smoothing is an image processing technique, which includes grayscale erosion and dilation, and grayscale opening and closing
Automatic algorithm for filling holes is a sequence of morphological operations in reconstruction branch, which fills all holes in the image.
Opening by reconstruction is a morphological operation in image processing for removing small objects and recovering shape accurately after.
Geodesic dilation and erosion are fundamental morphological reconstruction algorithms which yield the same result if left to converge.
Pruning in image processing is a morphological operation for removing spurs. It serves mainly as a post processing technique for cleaning up.
Skeletonization is a morphological process in image processing, which extracts the center lines of all shapes, which look like their skeleton
Thickening is a morphological operation in image processing, which adds foreground or white pixels to objects in order to thicken them.
Thinning is a morphological operation in image processing, which eats away at the foregorund pixels and leaves only lines shaping objects.
Convex hull in image processing is a morphological operation, where we encapsulate a shape or and object in an image into a convex shape.
Morphological gradient is an operation in image processing, with which we can emphasize edges in grayscale images. Furthermore, it utilizes grayscale dilation and erosion to calculate the difference between the two.
When we described dilation and erosion processes, we were working with binary images. Therefore, we we’re talking about how we were taking away and adding foreground pixels.
But, in our case here, we work with grayscale images, which have more than two possible intensities. For this reason, grayscale dilation and erosion work a little differently. But we still have a structuring element to slide over the image, while comparing values at each step.
However, the difference in grayscale morphology is that we choose the resulting pixel value by taking all of the values in the neighborhood into account. For example, with erosion, we pick minimum intensity in neighborhood overlapped by structuring element.
As we mentioned before, we’re going to use grayscale dilation and erosion to get the result. Firstly, we need to get the resulting images from each of these two operations. And secondly, we need to subtract erosion image values from dilation image values.
In essence, dilation thickens regions and generally makes images a bit brighter. While on the other hand, erosion has the opposite effect. And if we get the difference between these two, we’ll get an image with emphasized boundaries and suppressed homogenous areas.
However, results do depend on the size of the structuring element we want to use.
We already talked about grayscale erosion and dilation in another post, where I already posted the code for these operations. Therefore, I won’t copy it again for this guide, but I will post the function for applying morphological gradient.
public static Bitmap MorphologicalGradient(this Bitmap image)
{
int w = image.Width;
int h = image.Height;
BitmapData image_data = image.LockBits(
new Rectangle(0, 0, w, h),
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
int bytes = image_data.Stride * image_data.Height;
byte[] buffer = new byte[bytes];
byte[] result = new byte[bytes];
Marshal.Copy(image_data.Scan0, buffer, 0, bytes);
image.UnlockBits(image_data);
result = buffer.Dilate(image_data);
buffer = buffer.Erode(image_data);
for (int i = 0; i < bytes; i++)
{
result[i] = (byte)(result[i] - buffer[i]);
}
Bitmap res_img = new Bitmap(w, h);
BitmapData res_data = res_img.LockBits(
new Rectangle(0, 0, w, h),
ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
Marshal.Copy(result, 0, res_data.Scan0, bytes);
res_img.UnlockBits(res_data);
return res_img;
}
I hope this tutorial was helpful in giving you a better understanding on how morphological gradient and grayscale morphological operations work in general.
In case you want to check out the demo project, I’m leaving a download link below.