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:

A=imread('shapes.bmp');

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

SEE ALSO :Identifying objects based on label

## 1 comments:

A(i,j,1)<=0 shows an error saying expression to the left of the operator is not valid..please help

## Enjoyed Reading? Share Your Views