Subscribe:

Ads 468x60px

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

17 comments:

Syafiq O. said...

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.

manoj batra said...

thnx bro...gud wrk keep it up..:)

rajesh kumar said...

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

arun kumar said...

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

Madhu said...

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

lipika priyadarshini said...

can u send me time complexity of clustering

R kalaivani said...

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

TEDDY said...

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

Niraj said...

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.....

ASR said...

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.

asdf said...

thank you very much ...

Mani Mathes said...

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

naWIN kumar B L said...

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

Ahsan Khalid said...

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

Bala Subramaniam said...

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

rajatsharma said...

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

Sivanesan S said...

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

Post a Comment