# IMAGE PROCESSING

Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

## Recent Posts

### Mean, Median , Variance, Standard deviation and Mode

A=[10 10 20 40 60; 12 1 2 3 4; 7 8 9 11 4 ; 3 4 5 9 10];

A =

10    10    20    40    60
12     1     2     3     4
7     8     9    11     4
3     4     5     9    10

%MATLAB code to find the mean, median, variance, standard deviation and mode for an image
%Convert the image to type ‘double’.
%A=double(A);

[r,c]=size(A);

% MEAN:
The average of sum of all the values in the matrix.
%To find the mean for each column
colmean=sum(A)/r;
display(colmean);

%To calculate the mean of the matrix
totmean=sum(A(:))/(r*c);
display(totmean);

%To calculate the variance and standard-deviation column-wise
myvar=zeros([c,1]);
mystd=zeros([c,1]);
for i = 1: c
diff=A(:,i)-colmean(i);
myvar(i)=sum(diff.^2)/(r-1);
mystd(i)=sqrt(myvar(i));
end

display(myvar);
display(mystd);

%To calculate the variance and standard deviation

totdiff=(A-totmean).^2;
totsum=sum(totdiff(:));
nele=(r*c)-1;
totvar=totsum/nele;
display(totvar);
totstd=sqrt(totvar);
display(totstd);

%MEDIAN

%To find the median of the matrix row-wise
n=r/2;
B=sort(A,1);
if(mod(r,2)==0)
rowmedian=(B(round(n),:)+B(round(n)+1,:))/2;
else
rowmedian=B(round(n),:);
end
display(rowmedian);

%To find the median of the matrix column-wise
n=c/2;
B=sort(A,2);
if(mod(c,2)==0)

colmedian=(B(:,round(n))+B(:,round(n)+1))/2;
else
colmedian=B(:,round(n));
end
display(colmedian);

%To find the total median of the image
C=reshape(A,[],1);
min1=min(C);
max1=max(C);

E=sort(A(:));
num=round((r*c)/2);

if( (mod(r,2)==0) || (mod(c,2)==0) )
totmedian=(E(num)+E(num+1))/2;

else
totmedian=E(num);
end
display(totmedian);

%To find the mode of the matrix
D=hist(C,min1:max1);
D1=find(D==max(D));
mymode=D1(1)+min1-1;
display(mymode);

Like "IMAGE PROCESSING" page

### Reverse the words position in a string

This can be done in two ways.
First Method:
a.Find the length of the string
b. Use MATLAB built in function strtok to separate the first word and the remaining sentence.
For instance, the input string is ‘Lets Learn together... Happy Reading’.
[Token,remain]=strtok(input_string);
The variable token will contain ‘Lets
And the variable remain will contain ‘Learn together… Happy Reading’.
Use while loop to extract all the strings.

c. In the first iteration, the string in the token variable is concatenated with a empty variable ‘t’.
Now t has ‘Hello’.
In the second iteration, token has ‘Learn’ and remain has ‘together… Happy Reading’.
Now the string ‘Learn’ is concatenated with the string t.

d. Here to preserve the space ,the syntax [string2, ‘  ’,string1] is used.
The length of the input_string gets reduced during each iteration and finally when all the strings are extracted, the loop is terminated.

MATLAB CODE:
%To reverse the words position in a string
str='Lets Learn together... Happy Reading';
%str=fliplr(str);
len=length(str);
t='';

while(len>0)
[token,str]=strtok(str);
len=length(str);
%token=fliplr(token);
%t=[token,' ',t];
t=[' ',token,t];

end
display(t);

Output:

t =

Reading Happy together... Learn Lets

Second  Method:

This  differs  in the way of concatenation.
The input string position is reversed. And inside the loop after extracting each token, it is again reversed.
Then  the  string ‘t’ is concatenated with the token. The first method itself sufficient and just know the other way also.
Like "IMAGE PROCESSING" page

### How to draw in MATLAB

I used the function ‘getline’ to get the points from the user. Using plot function the lines are drawn.

MATLAB CODE:
scz=get(0,'ScreenSize');

fig=figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Draw Lines','Resize','off');

[x, y] = getline(fig);
plot(x,y);
axis([0 1 0 1]);

 Draw lines

To draw a rectangle:

x=zeros([1 5]);
y=zeros([1 5]);

r=getrect(fig);
x(:)=r(1);
y(:)=r(2);
x(2:3)=r(1)+r(3);
y(3:4)=r(2)+r(4);

plot(x,y);
axis([0 1 0 1]);

 Draw Rectangle

The function ‘getrect’ returns the [xmin ymin width height].
We need 4 points to draw a rectangle.
The first point is (x,y) , here it is (xmin,ymin).
The second point is(x+width,y)
Third point is (x+width, y+height)
Fourth point is (x,y+height)
Since I am using the plot function I need to connect the 1st and the fourth point.
So I need a fifth point i.e (x,y) which completes the rectangle.
Like "IMAGE PROCESSING" page

### Histogram of an image

I was trying to calculate the histogram of a 2d array and ended up in finding a method to calculate the histogram of an image.
Consider a 2d matrix:
B=[0 1 2 4 4 5; 0 0 2 2 4 5; 1 1 2 0 5 5; 4 4 2 0 1 5;];

B =
0     1     2     4     4     5
0     0     2     2     4     5
1     1     2     0     5     5
4     4     2     0     1     5

I used reshape function to convert the 2d matrix to 1 dimensional matrix.

C=reshape(B,[],1);

size of the matrix 24x1
This value 24 represents  no.of rows of B x no.of cols of B

Output of C:
C =

0
0
1
4
1
0
1
4
2
2
2
2
4
2
0
0
4
4
5
1
5
5
5
5

If you use C=reshape(B,1,[]), the size of the matrix will be 1 x 24

C =

Columns 1 through 16

0     0     1     4     1     0     1     4     2     2     2     2     4     2     0     0

Columns 17 through 24

4     4     5     1     5     5     5     5

I typecasted the variable ‘C’ to double.

C=double(C);

To find the histogram of the values, I specified the range 0 to n values.

D=hist(C,0:5)
D =
5     4     5     0     5     5

The output shows the value 0 occurs 5 times.

B =
0     1     2     4     4     5
0     0     2     2     4     5
1     1     2     0     5     5
4     4     2     0     1     5
And 1 occurs 4 times, 2 occurs 5 times and so on.

Now, the code to find the histogram for an image. This will be handy when we try to calculate the histogram of the image, thresholding the image and histogram equalization.
MATLAB CODE:
B=rgb2gray(A);
C=reshape(B,[],1);
C=double(C);
D=hist(C,0:255);
Like "IMAGE PROCESSING" page

### Cartesian to Polar co-ordinates:

To find the angle of a point in the Cartesian co-ordinates, convert the co-ordinates to polar co-ordinates using the function cart2pol(x,y) . The output will be radius and the angle. The radius is calculated from the origin to the given point. The theta value will be in radians. The function rad2deg(theta) can be used to convert the radians to degree.

MATLAB CODE:
I am using this plot function to display the points in the Cartesian co-ordinates.
plot([-10 -3 10 0 0 0 0 0 2 ] ,[0 0  0 0 6 10 -10 0 3],'--rs','LineWidth',2,'MarkerFaceColor','k');
%180 degrees

%display the radius , angle and the co-ordinates in the axes.

degree=strcat('deg:',num2str(deg));
text(-3,-2,degree);

%90 degrees

degree=strcat('deg:',num2str(deg));
text(-3,6,degree);

%45 degrees

degree=strcat('deg:',num2str(deg));
text(4,3,degree);

Like "IMAGE PROCESSING" page

### MATLAB OUTPUT FUNCTION

The output to the screen using ‘displaymatlab function.

To display the result with new line and more than 1 argument or variable, I used the cell format to display the variables.
In the first example, I used cell format to display the variables.  The special character ‘,’ is used as a concatenation operator and the ‘;’ is used as a new line character.

%Simple Example to demonstrate display function
length=10;
shape=' rectangle';

result={'Length =',length; 'Breadth =',breadth;'Shape:',shape; 'Area of the rectangle =',area};
display(result);

Here I converted the single line into a string and then used ‘;’ for a new line character.

%Lets make use strings here

result={strcat('length of the',shape,'= ',num2str(length)); strcat('Breadth of the',shape,'=',num2str(breadth)); strcat('Area of the',shape,'=',num2str(area))};
display(result);

%sprintf

result=sprintf('Length of the %s = %.2f \nBreadth of the %s = %.2f \nArea of the %s = %.2f',shape,length,shape,breadth,shape,area);
display(result);

Sprintf is used to format the text. It is similar to the ‘C’ function ‘printf’. For strings, ‘%s’ is used. For decimals,’%d’ is used.
And for new line same as in ‘C’ we use ‘\n’ here.

Like "IMAGE PROCESSING" page