Tuesday, November 3, 2009

Image Processing - Code to filter color in MATLAB

In this post we will learn to how filter color in Matlab. Matlab provide very good Image processing tool box with many ready made functions. Here I have illustrated a very basic method to filter color in Matlab. The concept is- in the 3D matrix of RGB color space, if the value at a particular pixel have more value in Red space than blue or Green, and if it is within a tolerance value then consider that pixel Red.
eg -


  elseif im(i,j,1)>200 & im(i,j,2)<50 & im(i,j,3)<50
            white(i,j)=0;
            green(i,j)=0;
            blue(i,j)=0;
            red(i,j)=1;


Copy this code an save as .m file. then run this file.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ------------------------@author:-  RITESH RANJAN
% -------- visit:-  http://programing-tutorial.blogspot.com/
% ------I have used a simple method. There are many more complex
%--------------- methods too that I will discuss Later
% ------------Just copy it save a .m file run in MATLAB 7 or higher
% ----------------Don't forget to change the name of Image file

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;
im=imread('image.jpg'); %put image name here
imtool(im);
sz=size(im);

for i=1:sz(1)
    for j=1:sz(2)
        if im(i,j,1)>200 & im(i,j,2)>200 & im(i,j,3)>200
            white(i,j)=1;
            green(i,j)=0;
            blue(i,j)=0;
            red(i,j)=0;
        elseif im(i,j,1)>200 & im(i,j,2)<50 & im(i,j,3)<50
            white(i,j)=0;
            green(i,j)=0;
            blue(i,j)=0;
            red(i,j)=1;
        elseif im(i,j,1)<50 & im(i,j,2)>200 & im(i,j,3)<50
            white(i,j)=0;
            green(i,j)=1;
            blue(i,j)=0;
            red(i,j)=0;
        elseif im(i,j,1)<50 & im(i,j,2)<50 & im(i,j,3)>200
            white(i,j)=0;
            green(i,j)=0;
            blue(i,j)=1;
            red(i,j)=0;
        else 
            white(i,j)=0;
            green(i,j)=0;
            blue(i,j)=0;
            red(i,j)=0;
       end
             end
end


imtool(white);
imtool(green);
imtool(blue);
imtool(red);

19 comments:

  1. Hello. Mr. Ritesh. I want to ask you about color segmentation in Matlab.
    Can you help me how to find a* value for green colour by converting RGB to CIElab colour space.

    ReplyDelete
  2. hello sir i want to find hidden object of image can u provide me code

    ReplyDelete
  3. sir i need matlab code for image processing for our b.tech project please help

    ReplyDelete
  4. can u send me time complexity of clustering

    ReplyDelete
  5. sir i am doing project inorder to differentitate good an bad tomato in image processing using matlab .i want matlab code for this project.

    ReplyDelete
  6. hello sir i need coding for homomorphic filtering, anisotropic filtering and bilateral filtering codings...

    ReplyDelete
  7. hello sir i want to know that after showing image the matrix is generated in matlab so how can i modify,control,change dat matrix numbers???plzzz suggest.....

    ReplyDelete
  8. Write an image processing algorithm to detect a linear scratch over an image as given in the figure 1a. For simplicity, here we can assume that the intensity of scratch is constant. Our output should show the scratch area in a separate image file like in the figure 1b. You should also show the length of scratch in the number of pixels at the command window.

    ReplyDelete
  9. hi sir i need your help for my project of image processing using matlab i need some info about matlab

    ReplyDelete
  10. I need to find the area covered by the green colour only(leaf); plaese send me the code; naveen.4skr@gmail.com

    ReplyDelete
  11. hello sir,
    im working on my final year project in which im sorting objects on the basis of colour using a conveyor belt and some actuators i.e. plungers,cylinders (to be decided) ...
    sir i need a code which can tell whether the object under the camera is green, blue or red ?? i will be thankfull if you have something to share in this regard ... kindly mail the code to the following email

    thankyou very much

    ahsankhalid.mecha09@yahoo.com

    ReplyDelete
  12. hello sir i need coding for remove particular areas from an image based on color in matlab. please help me sir....

    ReplyDelete
  13. please send me complete code algorithm & flow chart at rajatsharma.sharma143@gmail.com

    ReplyDelete
  14. I want to sort objects with 3 different shapes(circle,square,hexagon).....Pls send MATLAB codes for the above project @ s.sivanesan100@yahoo.com

    ReplyDelete
  15. removing pixels which are not related to the red
    colour using a CY-based colour filter..in image.any one know...help mee sir/mam..

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. :) So many FOR loops?? This is not matlab programming! Use vector approach.
    [Iw,Jw]=find( im(:,:1)>200 & im(:,:,2)>200 & im(:,:,3)>200)
    White=zeros(size(im(:,:,1))
    White(Iw,Jw)=1
    imtool(White)
    ..similarly for other channels...

    ReplyDelete