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

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:

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

like button Like "IMAGE PROCESSING" page

3 comments:

LIFE IS WORTH HARD WITHOUT HER.... said... Reply to comment

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

Unknown said... Reply to comment

@LIFE IS WORTH HARD WITHOUT HER....
Remove the space between < and = .

Unknown said... Reply to comment

hi , may ask you what is the threshold of black color ? i want to detect black color

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com