# 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

### Auto Cropping- Based on labeling the connected components

This post is about labeling the connected components in a binary image and crop the connected components based on the label. The main two functions used for this simple operation are ‘bwlabel’ and ‘regionprops’.
I used ‘bwlabel’ to label the connected components.  After labeling, I used ‘regionprops’ function to find the rectangle containing the region.  To find the rectangle points, I used ‘BoundingBox’ property.
Then the labeled components are automatically cropped and the image is displayed. To crop an image, ‘imcrop’ function is used.

MATLAB CODE:
``````

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

```

```

``````

%Convert the Image to binary
B=im2bw(A);

%Fill the holes
C=imfill(B,'holes');

%Label the connected components
[Label,Total]=bwlabel(C,8);
figure,imshow(C); title('Labelled Image');

```

```

``````

%Rectangle containing the region
Sdata=regionprops(Label,'BoundingBox');

%Crop all the Coins
for i=1:Total
Img=imcrop(A,Sdata(i).BoundingBox);
Name=strcat('Object Number:',num2str(i));
figure,imshow(Img); title(Name);
end

```

```

Another Example:

 'coins.png'

 Labeled Image

Like "IMAGE PROCESSING" page

### Identifying the objects based on count(bwlabel)

Identifying the objects based on labeling

Steps To Be Performed:

1. Convert the RGB image to binary image.
2. Fill the holes in the image.
3. Label the objects in the image based on connectivity 8
4. Display the images in a RGB format
4.1.  Store the co-ordinates(x,y) of the object 1 which is  labeled as 1.
4.2.  Calculate the image size for object one. The length can be found by subtracting the maximum and the minimum of y values. Similary for x find the width by subtracting the maximum and minimum of x values.
4.3.  Now map the pixel value to the new image.

 Original Image

figure,imshow(A);
title('Original image');
C=~im2bw(A);
B=imfill(C,'holes');
label=bwlabel(B,8);
for j=1:max(max(label))

[row, col] = find(label==j);

len=max(row)-min(row)+2;
sy=min(col)-1;
sx=min(row)-1;

for i=1:size(row,1)
x=row(i,1)-sx;
y=col(i,1)-sy;
target(x,y,:)=A(row(i,1),col(i,1),:);
end
mytitle=strcat('Object Number:',num2str(j));
figure,imshow(target);title(mytitle);
end

The objects that are displayed in a RGB format.

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');