You are here:Home » MATLAB » Image Processing - Code to filter color in MATLAB

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

0 comments:

Post a Comment