# Some hints for the mandatory assignment one

I have received some feedback that it is hard to understand what you are supposed to do in the first mandatory assignment. So here are some hints on what you are supposed to do. I'm not going to solve the exercises but I'll try to provide some hints for you.

You have to keep in mind that the overall task in this exercise is to be able to autmatically segment each texture from the other textures. This means that the answer to the question what is a good direction/good feature is a direction/feature that will help you segment the textures.

## Part A

This part is just to look at the 8 different textures and by your own judgement describe how the features differ. Relevant keywords are listed in the assignment. So forexample saying that the frequency in texture X is higher than texture Z is okay. But if you are able to figure out the actual quantitative frequency you are welcome to say so, but this is not necessary.

## Part B

Here you should devide the images into subimages of each texture. You may also do different histogram transforms as a pre-prosessing step as I used the histogram equalization as a preprossing in the first weekly exercise.

You also should, based on you analysis in A, choose some paramteres you believe should help separating the textures.

Then you are to normalise the image (thus use fewer grayleves). How many graylevels do you need? And calculate the GLCM from each texture.

For example like this:

% Here I have chosen some other textures from http://www.cb.uu.se/~gustaf/texture/
clear all
close all

% My chosen number of graylevels
G = 16;
% My chosen direction
dx = 2;
dy = 0;
%Normalizing the images
texture1 = uint8(round(double(texture1) * (G-1) / double(max(texture1(:)))));
texture2 = uint8(round(double(texture2) * (G-1) / double(max(texture2(:)))));

%Lets make a mock texture3
texture3 = texture2;
lim1 = 6;
lim2 = 8;
texture3(texture2>lim2) = 8;
texture3((texture2>lim1)&(texture2<=lim2)) = 15;
texture3(texture2<lim1) = 0;

% Calculate the GLCM from each texture. Here I use the symmetric normalized
% GLCM. The isotropic GLCM menitioned in the lecture is a GLCM where you
% merge many GLCMs calulated for different directions. See the lecture
% slides for more detail.
P1 = GLCM(texture1,G,dx,dy,1,1);
P2 = GLCM(texture2,G,dx,dy,1,1);
P3 = GLCM(texture3,G,dx,dy,1,1);

% Plot the images and resulting GLCMs
figure(1);clf
imagesc(texture1)
colormap gray
title('Texture 1');

figure(2);clf
imagesc(P1)
colormap jet
title('GLCM of texture 1');

figure(3);clf
imagesc(texture2)
colormap gray
title('Texture 2');

figure(4);clf
imagesc(P2)
colormap jet
title('GLCM of texture 3');

figure(5);clf
imagesc(texture3)
colormap gray
title('Texture 3');

figure(6);clf
imagesc(P3)
colormap jet
title('GLCM of texture 3');      ## Part C pre

In part C you are asked to use three different features. Let's investigate them a bit.

% Creating two matrices with the corresponding index.
i = repmat((0:(G-1))', 1, G);
j = repmat( 0:(G-1)  , G, 1);

%To understand the three different GLCM features, lets look at each of the
%weighting
%Intertia weighting
inertia_weighting = (i-j).^2;
%Homogenity (Inverse Difference Moment)
idm_weighting = 1./(1+(i-j).^2);
%Cluster shade weighting using mean from GLCM from texture 1
u_x_1 = sum(sum(P1 .* (i + 1)));
u_y_1 = sum(sum(P1 .* (j + 1)));
cs_weighting_1 = (i+j-u_x_1-u_y_1).^3;
%Cluster shade weighting using mean from GLCM from texture 2
u_x_2 = sum(sum(P2 .* (i + 1)));
u_y_2 = sum(sum(P2 .* (j + 1)));
cs_weighting_2 = (i+j-u_x_2-u_y_2).^3;

figure(7)
imagesc(inertia_weighting)
colorbar
title('GLCM intertia weighting')

%Lets calculate some values. What to they mean?
text1_intertia = sum(sum(inertia_weighting.*P1))
text2_intertia = sum(sum(inertia_weighting.*P2))
text3_intertia = sum(sum(inertia_weighting.*P3))

figure(8)
imagesc(idm_weighting)
colorbar
title('Homogenity weighting');

%Lets calculate some values. What to they mean?
text1_idm = sum(sum(idm_weighting.*P1))
text2_idm = sum(sum(idm_weighting.*P2))
text3_idm = sum(sum(idm_weighting.*P3))

figure(9)
imagesc(cs_weighting_1)
colorbar
title('Cluster shade weighting using means from P1');

figure(10)
imagesc(cs_weighting_2)
colorbar
title('Cluster shade weighting using means from P2');

% I'll leave it for you to calculate some example values for the GLCM

% So when looking at these weights, what will each of these features favour?
% What kind of structure in the GLCM matrix will get what values??

text1_intertia =

5.1397

text2_intertia =

1.3622

text3_intertia =

24.7920

text1_idm =

0.4102

text2_idm =

0.6406

text3_idm =

0.6602    ## Part C

In part C you are supposed to calculate feature images. You do this by having a local window gliding over the entire image of the four textures and for each window you get one GLCM matrix which will give you one value for each feature.

% One feature image for one chosen GLCM direction can for example look like
% this:
figure(11)
imagesc(feature_img);
colormap jet
colorbar
title('Example of a feature image'); ## Part D

Part D is to treshold the different feature images to try to segment each texture. post_id = 529; %delete this line to force new post; permaLink = http://inf4300.olemarius.net/2015/09/29/oblig1-m/;

1. lxq says:

Can any one tell me, why graycomatrix(img, 'numlevels', 16) this method only scale my gray level for each pixel to either 16 or 1 only (almost all are 16 actually)? If I set the GrayLimits parameter to [1, 16], most of the values are still 16, but just very few become 5....

It works fine for the entire image, but it only happens to my cropped image (inside the sliding windows).

1. omrindal says:

The graycomatrix does the normalization for you. So you have to make sure that you do not send an image to graycomatrix that is allready normalized. Then it would normalize it again and set all pixels to for example zero. Which would give you a GLCM matrix with only values at 16 or 1. Are you sure this is not what is going on? I believe we looked at this during the group lecture. You could stop by my office, and we can see if we can solve it.

1. lxq says:

Hi,

I have found out why.

I need to call uint8() to convert the img when calling graycomatrix() this function. It is very weird but now everything works fine 2. omrindal says:

Hello all,

I have gotten some comments on why i use
 u_x_1 = sum(sum(P1 .* (i + 1))); u_y_1 = sum(sum(P1 .* (j + 1))); 

and not

 u_x_1 = sum(sum(P1 .* (i ))); u_y_1 = sum(sum(P1 .* (j ))); 

To be honest I don't really know, but I believe the book (page 832) defines it like this. But I believe that the last implementation is the most correct.

3. Paul Magnus says:

I don't understand the definition of cluster shading.
CHD = \sum_{i=0}^{G-1} \sum_{j=0}^{G-1} (i + j - \mu_x - \mu_y)^3 * P(i,j)
What is \mu_x and \mu_y? I thought \mu = mean(P), but how do you define the mean of something in x- or y-direction?

1. Paul Magnus says:

I realised now that mu_x=mu_i and mu_y=mu_j.

1. omrindal says:

Yep, so I believe you figured it out 😉