# IMAGE PROCESSING

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

### Converting RGB image to Binary Image without using im2bw function

In the first example, image is filled with primary colors (RGB). So I am finding the sum of the values in the pixel position. If the sum is greater than zero then the value will be 1(white) otherwise zero (black).

In the second example, the following steps are needed to convert a RGB image to binary image.

1. Convert the RGB image into grayscale image.
2. Find the threshold value. If the value at the pixel position is greater than the threshold value then the value will be 1(white) else zero (black).

```
function mybinary

global GIm T1;

figure,imshow(A);

title('Original image');

B=zeros(size(A,1),size(A,2));

for l=1:size(A,1)

for m=1:size(A,2)

if(sum(A(l,m,:))>0)

B(l,m)=1;

end

end

end

B=logical(B);

figure,imshow(B);

```

```

figure,imshow(Im);

title('Original Image');

%0.2989 * R + 0.5870 * G + 0.1140 * B

GIm=uint8(zeros(size(Im,1),size(Im,2)));

for m=1:size(Im,1)

for n=1:size(Im,2)

GIm(m,n)=0.2989*Im(m,n,1)+0.5870*Im(m,n,2)+0.1140*Im(m,n,3);

end

end

```
we can perform the grayscale conversion without using the for loop:

%GIm=0.2989*Im(:,:,1)+0.5870*Im(:,:,2)+0.1140*Im(:,:,3);

```

ssz = get(0,'ScreenSize');

T.fg=figure('Visible','on','Name','IMAGE THRESHOLDING','NumberTitle','off','Position', ssz);

T.holder=axes('units','pixels','Position',[ssz(3)/35 ssz(4)/4 ssz(3)-(ssz(3)/3) ssz(4)-(ssz(4)/3)]);

imshow(GIm);

set(T.holder,'xtick',[],'ytick',[])

T.slid=uicontrol('Style','Slider','Visible','on','Value',1,'Max',255,'Min',0,'Sliderstep',[1 1],'Position',[ssz(3)/35 ssz(4)/5 ssz(3)-(ssz(3)/3) 20],'Callback', @tresher);

T.ent=uicontrol('Style','pushbutton','Visible','on','String','THRESHOLD VALUE','Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/8) 105 30]);

T.ed=uicontrol('Style','edit','Visible','on','String','0','Value',1,'Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/6) 90 20]);

function tresher(object,~)

val=get(object,'value');

in=GIm;

T1=Imthreshold1(in,val);

T.view1=imshow(T1);

set(T.holder,'xtick',[],'ytick',[])

set(T.ed,'String',val);

end

function Im=Imthreshold1(Image,Tvalue)

sz=size(Image);

mybin=zeros(size(Image));

for i=1:sz(1)

for j=1:sz(2)

if(Image(i,j)>Tvalue)

mybin(i,j)=1;

end

end

end

```

Instead of this for loop, the equivalent one line code is:

%mybin(find(Image>Tvalue))=1;

Explanation:
The output of find(Image>Tvalue) will be the values that are greater than Tvalue.

For instance,
consider a matrix,

>> A=[1,2,3,4;2,4,6,8;3,6,9,12];
>> A

A =

1     2     3     4
2     4     6     8
3     6     9    12

>> find(mod(A,2)==0)

ans =

2
4
5
6
8
10
11
12

``````
```
Im=logical(mybin);

end

end
```

Like "IMAGE PROCESSING" page

### Identifying Objects based on color (RGB)

Here I  used a bitmap image with different shapes filled with primary colors Red, Blue and Green.

The objects in the image are separated based on the colors. The image is a RGB image which is a 3 dimensional matrix.

Lets use (i,j) for getting the pixel position of the image A.
In the image, A (i, j, 1) represents the value of red color.
A (i, j, 2) represents the green color.
A (i, j, 3) represents the blue color.

To separate the objects of color red:
Check if A (i, j, 1) is positive. [In most cases the value will be 255];
A (i, j, 2) and A (i, j, 3) will be zero.

Similarly, other colors can be separated.

MATLAB CODE:

figure,imshow(A);
title('Original image');

%Preallocate the matrix with the size of A
Red=zeros(size(A));
Blue=zeros(size(A));
Green=zeros(size(A));

for i=1:size(A,1)
for j=1:size(A,2)

%The Objects with Red color
if(A(i,j,1) < = 0)
Red(i,j,1)=A(i,j,1);
Red(i,j,2)=A(i,j,2);
Red(i,j,3)=A(i,j,3);
end

%The Objects with Green color
if(A(i,j,2) < = 0)
Green(i,j,1)=A(i,j,1);
Green(i,j,2)=A(i,j,2);
Green(i,j,3)=A(i,j,3);
end

%The Objects with Blue color
if(A(i,j,3) < = 0)
Blue(i,j,1)=A(i,j,1);
Blue(i,j,2)=A(i,j,2);
Blue(i,j,3)=A(i,j,3);
end

end
end

Red=uint8(Red);
figure,imshow(Red);
title('Red color objects');

Blue=uint8(Blue);
figure,imshow(Blue);
title('Blue color objects');

Green=uint8(Green);
figure,imshow(Green);
title('Green color objects');

Like "IMAGE PROCESSING" page

### MATLAB code for Linear filtering without using imfilter function

Linear Filter :
Linear filtering technique is used for reducing random noise, sharpening the edges and correcting unequal illuminations.

The procedure is carried out by filtering the image by correlation with an appropriate filter kernel.  The value of output pixel is calculated as a weighted sum of neighboring pixels.

MATLAB CODE:

``````

figure,imshow(A);

title('Original Image');
``````

```
corr=[0 0.5 0.5;-1 0.5 0.2; 0.4 0.2 0;];

%corr=[0.5

%    0.4

%    0.1];

%corr=ones(5,5)/25;
```

%To pad the input image with zeros based on the kernel size.

Example:

Let M=[4 5 6; 1 1 4; 7 8 8;];

M= [ 4     5     6
1     1     4
7     8     8]

%Pad with zeros on all sides
M1 =

0     0     0     0     0
0     4     5     6     0
0     1     1     4     0
0     7     8     8     0
0     0     0     0     0

%pad with two rows and columns of zeros on all sides

M2 =

0     0     0     0     0     0     0
0     0     0     0     0     0     0
0     0     4     5     6     0     0
0     0     1     1     4     0     0
0     0     7     8     8     0     0
0     0     0     0     0     0     0
0     0     0     0     0     0     0

%Pad 1 row and 2 columns with zeros on all sides
M3 =

0     0     0     0     0     0     0
0     0     4     5     6     0     0
0     0     1     1     4     0     0
0     0     7     8     8     0     0
0     0     0     0     0     0     0

```

output=uint8(zeros(size(A)));

if(size(corr,1)==1)

m=0;

n=floor(size(corr,2)/2);

sz1=size(B,1);

elseif(size(corr,2)==1)

m=floor(size(corr,1)/2);

n=0;

sz2=size(B,2);

else

m=floor(size(corr,1)/2);

n=floor(size(corr,2)/2);

end

for i=1:size(A,1)

for j=1:size(A,2)

B(i+m,j+n)=A(i,j);

end

end

szcorr1=size(corr,1);

szcorr2=size(corr,2);

for i=1:sz1

for j=1:sz2

sum=0;

m=i;

n=j;

for x=1:szcorr1

for y=1:szcorr2

%The weighted sum of the neighborhood pixels is calculated.

sum=sum+(B(m,n)*corr(x,y));

n =n+1;

end

n=j;

m=m+1;

end

output(i,j)= sum;

end

end

figure,imshow(output);

title('After linear filtering');
```

%For the correlation kernel ones(5,5)/25;

Like "IMAGE PROCESSING" page