Identifying Objects based on color (RGB)
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:
A=imread('shapes.bmp');
figure,imshow(A);
title('Original image');
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgzQ9XUQ0jfkvwxC9ZRNyZnVGvtQpLo1T5_t6JJChd2PKc-3JWVMyWviN4a16PtciVnKz44yIeuzGN-QJ5uL7UfpfNCipOlAZKaSv0mgw7Rzl0dEw_eLnShBhhyphenhyphenQaftzaQSV0ibgArZwQ/s320/shapes.bmp)
%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');
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMIIi1_Nl2nlGc2gjIyfph9P4NOLYATseSaCmeXFJLrvoCmLU3_g0Op59qBEm7Hxr8GESWQ_SrHXLKnt9YAh-I8vplld_aEj5sFjme4vf2thAQg4sewA8i3Ovm9NTDJBvlnXPKhOn7Un4/s320/red.bmp)
Blue=uint8(Blue);
figure,imshow(Blue);
title('Blue color objects');
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdTFHxqglrUAi1Kwd4Be-qV-tbz-Np12RQLuF2EZb9aut0eaCxI8Nb11Z7YmtCd3tWpaGELuR6VFR9Xe9m6lA8M1sVMFgF5XvmPr0eKMfjHQgutnjsiHqVnA_hRyY0sK_H3B2SGR1NjZE/s320/blue.bmp)
Green=uint8(Green);
figure,imshow(Green);
title('Green color objects');
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RJvdANo3PZYni7T-r3ZFdgnapHAE6oQY4Rl9UY-f5dAI1UHPct4sjzhh6Lq5gnsFi1Aw68uuWtA054g01HdMmtrlxZtraht9xfA2TUO8kmJOo0XREHHMz8YAao85viCLMCRwvGzDk6w/s320/green.bmp)
Identifying the objects based on count(bwlabel)
Steps To Be Performed:
- Convert the RGB image to binary image.
- Fill the holes in the image.
- Label the objects in the image based on connectivity 8
- 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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxC1DQhPFLqhSEteIN4otzHwtJ6tvw5T2jX7vapFP3dFDKdRzDJVzpL3W6FtXwzEFgeWjw7gOB94sw-kjQWZ-Q1nDdo9T1nPQ1leJEiikq_rz4VInq0bl-VddsY90zx7SJ0JT2CLMcn9o/s320/num2.jpg)
A=imread('num2.jpg');
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;
breadth=max(col)-min(col)+2;
target=uint8(zeros([len breadth 3] ));
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCTaulJPO7bykbC4fLjZJ1DL4hG6xwPYZjBl9Tk1U5cbSPPGzFBMRdN3GugA-rmwqcGrc_47eA_N7yYYNRW1TSeYOS7W6jquVzPXu574CuOn3UvF7EiF8zHX8pRk2UNnek1xr7CKuKGls/s200/obj9.bmp)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7llWq9SyMGu4us-32lnDmhxCITMUjUAugbQ657-Q1Fchyphenhyphen1rGumx5OGDfPwcP2wYtwRy3IYwh9PKDQqGOsQRYczBVdiBmfJ1_T1RIGrrALIcblN8H9uLvREp4u08VF5UI3admgY_-NrVU/s200/obj5.bmp)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7llmwtJNSvymsl-mxTKaEC6epsgoM6WFHNpFAhxern2j0OmC33kzprJiLbdgfaBUIeQ86gn8KWSVpjWccm4f2reaum536DAd5veYp5gzzm5Pvg74UJKd3XGril8vMQIN4X1RlB9GGRno/s200/obj1.bmp)
No comments:
Post a Comment