Wednesday, November 21, 2012

MATLAB - CODING'S

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



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



Identifying the objects based on count(bwlabel) 

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.





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.



 

No comments:

Post a Comment