Friday, 11 December 2015

Colour Detection Code

clc;
close all;
clear all;
for index=1:2
    capture
I=imread('input1.jpg');
figure,imshow(I);

Red_component=I(:,:,1);
Green_component=I(:,:,2);
Blue_component=I(:,:,3);

% figure,imshow(Red_component)
% figure,imshow(Green_component)
% figure,imshow(Blue_component)

[Count_R Pixel_val]=imhist(Red_component);
[Count_G Pixel_val]=imhist(Green_component);
[Count_B Pixel_val]=imhist(Blue_component);
% figure,
% bar(Count_R, 'r');
% grid on;
% xlabel('Gray Levels');
% ylabel('Pixel Count');
% title('Histogram of Red Band', 'FontSize', 20);
% figure,
% bar(Count_G, 'g');
% grid on;
% xlabel('Gray Levels');
% ylabel('Pixel Count');
% title('Histogram of Green Band', 'FontSize', 20);
%
% figure,
% bar(Count_B, 'b');
% grid on;
% xlabel('Gray Levels');
% ylabel('Pixel Count');
% title('Histogram of Blue Band', 'FontSize', 20);
   
redThresholdLow = 85;
redThresholdHigh = 255;
greenThresholdLow = 0;
greenThresholdHigh = 70;
blueThresholdLow = 0;
blueThresholdHigh = 90;

redMask=(Red_component >= redThresholdLow) & (Red_component <= redThresholdHigh);

greenMask = (Green_component >= greenThresholdLow) & (Green_component <= greenThresholdHigh);
blueMask = (Blue_component >= blueThresholdLow) & (Blue_component <= blueThresholdHigh);

redObjectsMask = uint8(redMask & greenMask & blueMask);
% figure,imshow(redObjectsMask,[])
   
redObjectsMask = uint8(bwareaopen(redObjectsMask, 100));
structuringElement = strel('disk', 4);
redObjectsMask = imclose(redObjectsMask, structuringElement);
% figure,imshow(redObjectsMask, []);
     
       
maskedImageR = redObjectsMask .* Red_component;
maskedImageG = redObjectsMask .* Green_component;
maskedImageB = redObjectsMask .* Blue_component;
   
maskedRGBImage = cat(3, maskedImageR, maskedImageG, maskedImageB);
gray_im=rgb2gray(maskedRGBImage);
% figure,imshow(maskedRGBImage,[])
edge_im=edge(gray_im,'sobel');
% hold on
% image(edge_im)
% hold off
blue_c=255;
red_c=120;
green_c=100;
ed_mask1=edge_im*255;
ed_mask2=edge_im*255;
ed_mask3=edge_im*100;
maskedImage = uint8(cat(3, ed_mask1, ed_mask2, ed_mask3));
maskedRGBImage=double(maskedRGBImage);
maskedImage=double(maskedImage);
output_im=uint8(maskedRGBImage + maskedImage) / 2;
figure,imshow(output_im)
end
% ncuts=otsu(maskedRGBImage);
%   second=1;once=1;
%   for i=1:240
%       for j=1:320
%           if gray_im(i,j)==0
%               var(i,j)=1;
%           end
%       end
%   end
%   stats2 = regionprops(ncuts, {'Area', 'BoundingBox'});
%  
%   Iprops=regionprops(ncuts,'BoundingBox','Image');
% %   for i=1:158
% %   area=stats1(i,1).Area
% %   end
%   for j=1:size(stats2,1)
%  hold on
%  area2 = stats2(j).Area;
% %  area3=sort(area2)
%  if((area2>0) )
%  rectangle('Position',[stats2(j).BoundingBox(1),stats2(j).BoundingBox(2),stats2(j).BoundingBox(3),stats2(j).BoundingBox(4)],'EdgeColor','r','LineWidth',2 );
%  end


% end

No comments:

Post a Comment