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

18 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. thnx bro...gud wrk keep it up..:)

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

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

    ReplyDelete
  5. sir,could u help us get matlab coding for image enhancement

    ReplyDelete
  6. can u send me time complexity of clustering

    ReplyDelete
  7. 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
  8. hello sir i need coding for homomorphic filtering, anisotropic filtering and bilateral filtering codings...

    ReplyDelete
  9. 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
  10. 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
  11. hi sir i need your help for my project of image processing using matlab i need some info about matlab

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

    ReplyDelete
  13. 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
  14. hello sir i need coding for remove particular areas from an image based on color in matlab. please help me sir....

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

    ReplyDelete
  16. 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
  17. In addition to these projects you can have a look at these Matlab projects also;

    RGB Image Compression using Discrete Cosine Transform: http://berksoysal.blogspot.com/2014/07/rgb-image-compression-using-discrete.html

    Pseudo Coloring
    http://berksoysal.blogspot.com/2014/05/ee431-introduction-to-image-video.html

    All source codes are included, keep visiting for more Image Processing Projects.

    ReplyDelete