Time for action – ROI refinement using strel
In this example, we shall see how to use the disk structuring element from strel
, to have a better masking result for the middle rock of our holiday picture. To focus on our task, we will first crop the area we are mostly interested in. Assuming we have cleared our workspace using clear all
(MATLAB's command to clear all the variables), we follow these steps:
- Read our colored image, convert it to grayscale, and crop the area containing the middle rock:
>> img = imread('3rocks.jpg'); >> rock = imcrop(rgb2gray(img));
- Threshold the cropped image using the same threshold as before (
30
) and show the result side-by-side with the original:>> mask1 = rock < 30; >> subplot(1,2,1),imshow(rock),title('Original image') >> subplot(1,2,2),imshow(mask1),title('Initial mask')
- Perform image cleaning, using the
imerode
function:>> mask2 = imerode(mask1,ones(2));
- Make the structuring element for this rock, which will be the top half part of a disk. We will use a disk of radius
26
(you can experiment with other values):>> se = strel('disk',26); % Make a disk with a radius of 26px >> se_mat = getnhood(se); % Convert structuring element to matrix >> se_mat(27:end,:) = 0; % Make the bottom half equal to zero
- Perform dilation with the processed structuring element:
>> mask3 = imdilate(mask2,se_mat);
- Erase the rock from the original image using
mask3
:>> no_rock = rock; >> no_rock(mask3) = 200; % Use brightness value 200
- Demonstrate the results:
>> subplot(2,2,1),imshow(rock),title('Original image') >> subplot(2,2,2),imshow(no_rock),title('Masked image') >> subplot(2,2,3),imshow(mask2),title('Mask before dilation') >> subplot(2,2,4),imshow(imsubtract(mask3,mask2)), title('mask3-mask2')
What just happened?
In the example we just did, we finished tweaking our ROI selection and masking example. After cropping our image to include only one rock, we followed the same procedure as before to get to our first mask. Then, in the most important steps of this example (highlighted code in step 4) we chose the disk
structuring element from MATLAB, with a radius of 26
pixels, converted it to matrix form and set its bottom half to 0. Finally, we applied dilation using the structuring element we created, used the generated mask to alter the brightness of the pixels under it in the original image to 200, and displayed our results.
So, by now, you should be starting to get a good idea of how to tweak binary masks using dilation and erosion, and how these two operations actually affect your images. In a nutshell, binary masks can be used to focus your pixel processing tasks on specific areas of the image; dilation and erosion are tools used to expand or shrink your areas of interest respectively. A better structuring element selection for these operations leads to a better result.
Have a go hero – write a function to for local dilation/erosion
In the previous chapter, we saw how to write a function that performs enhancement of a rectangular area specified by the user. Can you do the same for dilation and erosion? The function should get three inputs; the original binary image, the structuring element and the selection of operation (one for erosion and two for dilation).
Well, the implementation shouldn't seem so hard now. We will more or less base our function on what we did in the previous chapter. The first step is to let the user crop the part of the image to be processed and save its coordinates. Then, we should switch to the specified operation based on the user's input. The selected operation will then be performed on the original binary image using the structuring element provided as input. The final step is to place the cropped region back on the image and return the output.
The function you should write, named CroppedDilationErosion.m
, is defined as follows:
function [output] = CroppedDilationErosion(input,se,method) % Function that performs area-based dilation or erosion with = % a user-defined structuring element. % Inputs: % input - Input image % se – Structuring element % method – Morphology operation (1: dilation, 2: erosion) % Output: % output - Output image (dilated or eroded)
To check if your function works as expected, you can use the mask from the previous example:
>> img = imread('3rocks.jpg'); >> rock = rgb2gray(img); >> mask = rock < 30; >> mask2 = CroppedDilationErosion(mask,ones(10),2); % Erode mask >> mask3 = CroppedDilationErosion(mask,ones(10),1); % Dilate mask
By selecting the following ROI in both operations:
The results would be:
>> subplot(1,3,1),imshow(mask),title('Original mask') >> subplot(1,3,2),imshow(mask2),title('Mask after erosion') >> subplot(1,3,3),imshow(mask3),title('Mask after dilation')
We can see that, by selecting an ROI including only the middle rock, the erosion result almost makes it disappear and the dilation result makes it grow. All the other parts of the image remain unaffected.
Tip
You may be surprised to learn that dilation and erosion are not limited to binary images, but can also work on grayscale images generating interesting results. To have a taste, try to call the function we just made using a grayscale input instead of a binary one.
More morphological operations
Until now, we have focused extensively on the erosion and dilation operations. It would be logical for you to start thinking whether is this all that is there? Aren't there any more morphological operations? The answer is; there are plenty, but you will not be using them half as much as the two aforementioned operations, at least for everyday tasks. Also, many of the other morphological operations are based on combinations of dilation and erosion. An analytical list of morphological operations supported by MATLAB can be found at http://www.mathworks.com/help/images/morphological-filtering.html.
However, describing all the morphological operations lies beyond the scope of this book. From here on, we will use those we need and describe them at the same time, so that you can comprehend the importance of their usage by example.