Playing with images and MATLAB

Us presento el meu gat, Trasto, per jugar amb elements estructurants i operacions morfològiques utilitzant MATLAB. Primer utilitzant elements estructurants plans, després de no plans i, finalment, de formes diferents.

I know I said I wouldn’t use MATLAB again but Octave instead. I lied. This afternoon I was a little bit bored and began playing with images and tiny scripts (almost one-liners).

First of all, let me introduce you my cat. It’s called Trasto, a catalan word that means piece of junk, although in catalan it’s not as offensive as in English ;)

Original image of my cat

These are just a few quick experiments. The leit motiv of all is reducing colors of the image (although not much).

Playing with flat structuring elements

First of all I played with a disk structuring element. The intention was to make flat homogenous regions of color.

This is my first try:

cn=4096;
cs=colorcube(cn);
t=ind2rgb(imerode(rgb2ind(i,cs),strel('disk',3)),cs);

t looks like this:

Image with erosion using a disk structuring element

To smooth the image a little bit while presenving homogenous regions I used median filtering:

t2(:,:,1)=medfilt2(t(:,:,1),[5 5]);
t2(:,:,2)=medfilt2(t(:,:,2),[5 5]);
t2(:,:,3)=medfilt2(t(:,:,3),[5 5]);

This is how t2 looks like:

Image with erosion using a disk structuring element and median filtering

Now the same image using 1024 colors. Keep in mind that palette of 1024 colors is made using colorcube, which doesn’t use image data to choose the values, so the resulting image has much less than 1024 colors. We could use some adaptative process to choose them if we wanted to, and we’d probably see that adjacents colors in the cat would be more similar from each other than they are now (and probably would happen just the opposite in other places).

Image with erosion using a disk structuring element, median filtering and 1024 colors

And what about non-flat structuring elements?

Perhaps the image has too much detail to seek homogeneous colors. We could try to use non-flat structuring elements, like ball, and see what we get. For instance, let’s use ball with radius 2 and height 1.

cn=1024;
cs=colorcube(cn);
t=ind2rgb(imerode(rgb2ind(i,cs),strel('ball',2,1)),cs);
Image with erosion using a ball structuring element and 1024 colors

We apply median filtering, as before:

Image with erosion using a ball structuring element, median filtering and 1024 colors

Using greater height values we can get funny things (for instance radius 5 and height 5) due to saturation:

Image with erosion using a ball structuring element with radius 5 and height 5 and 1024 colors

And with median filtering:

Image with erosion using a ball structuring element with radius 5 and height 5, median filtering and 1024 colors

Texturing the image using shaped structuring elements

What happens if we use a cross as a structuring element?

cs=2048; t=ind2rgb(imerode(rgb2ind(i,colorcube(cs)), 
[1,0,0,0,1; 
 0,1,0,1,0; 
 0,0,1,0,0; 
 0,1,0,1,0; 
 1,0,0,0,1]), 
colorcube(cs));
Image with erosion using a cross-shaped structuring element

Reducing the number of colors the effect is much more visible:

Image with erosion using a cross-shaped structuring element and 256 colors

We obviously don’t use median filtering here because we would eliminate the texturing effect of the crosses.

If we use horizontal lines as structuring elements,

cs=256; t=ind2rgb(imerode(rgb2ind(i,colorcube(cs)), 
[1,1,1,1,1,1,1]), 
colorcube(cs));

, we get:

Image with erosion using a horizontal line structuring element and 256 colors

Since my cat is called Trasto I’ll use T-shaped structuring elements:

cs=256; t=ind2rgb(imerode(rgb2ind(i,colorcube(cs)), 
[0,0,1,1,1,0,0; 
 0,0,0,1,0,0,0; 
 0,0,0,1,0,0,0; 
 0,0,0,1,0,0,0; 
 0,0,0,1,0,0,0; 
 1,0,0,1,0,0,1; 
 1,1,1,1,1,1,1]), 
 colorcube(cs));
Image with erosion using a T-shaped structuring element and 256 colors

Note: I didn’t realize when I saved all the images to JPEG format using imwrite that MATLAB has the default quality setting for JPEG very low. So you’ll see the “water” effect of JPEG in images. This is specially true for last images, crosses, lines and T should be all homogeneous.

Leave a Reply