Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Paint Application using bwlabel concept

 Fill the image with RGB colors based on user specified color value. Instead of Flood fill algorithm I am using bwlabel function in MATLAB to paint the image. 

This is very simple comparing to the previous application.


function color
global BW L A colorv rgb originalcolor undocolor point filename;
scz=get(0,'ScreenSize');
figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Paint Application','Resize','off');
originalcolor=[0;0;0;];
ax=axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);
directory=dir('*.png');
files={directory.name}';
uicontrol('Style','popupmenu','position',[535 430 160 30],'Value',1,'String',files,'Callback',@displayfile);
uicontrol('Style','text','position',[495 400 35 20],'String','RED');
 
R=uicontrol('Style','slider''position',[535 400 160 20],'min', 0.0, 'max',1.0,'Callback',@colorvalue);
 
uicontrol('Style','text','position',[495 360 35 20],'String','GREEN');
G=uicontrol('Style','slider''position',[535 360 160 20],'min', 0.0, 'max',1.0,'Callback',@colorvalue);
 
uicontrol('Style','text','position',[495 320 35 20],'String','BLUE');
B=uicontrol('Style','slider''position',[535 320 160 20],'min', 0.0, 'max',1.0,'Callback',@colorvalue);
 
uicontrol('Style','text','position',[495 280 35 20],'String','Color');
colorv(25,80,1:3)=0;
uicontrol('Style','pushbutton','position',[600, 470, 40,20],'String''UNDO','Callback',@options);
uicontrol('Style','pushbutton','position',[645, 470, 40,20],'String''SAVE','Callback',@options);
uicontrol('Style','pushbutton','position',[540 280 80 30]);



Function ‘colorvalue’ will show the color on the pushbutton.





function colorvalue(obj,event)
        R1=get(R,'value');
        G1=get(G,'value');
        B1=get(B,'value');
        rgb=[ceil(R1*255); ceil(G1*255); ceil(B1*255);];
        colorv(:,:,1)=R1;
        colorv(:,:,2)=G1;
        colorv(:,:,3)=B1;
uicontrol('Style','pushbutton','position',[540 280 80 30],'String','color','CDATA',colorv);
  originalcolor=rgb;
  try
 paintme;
  catch
      originalcolor='null';
  end
    end
    

%User selects the image

function displayfile(obj,event)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        A=imread(filename);
        [BW,L]=bwlabel(im2bw(A),8);
        imshow(BW);
end
    function paintme
        while(originalcolor==rgb)
            getpoints;
       end
    end

               
                     

                                                                                                                                                                 
                                                                                                                                         
                                                                                                                                    %To save the image or to go back to the previous point

function options(obj,event)
        fn=get(obj,'String');
        if(strcmp(fn,'SAVE')==1)
            fname=strcat('my',filename);
           msg=strcat('File name:',fname);
           msgbox(msg,'FILE SAVED');
        elseif(strcmp(fn,'UNDO')==1)
            rgb=undocolor;
            colorfill(point(1),point(2));
            rgb=originalcolor;
       end
    end
    

%Get the point in the image.

function getpoints
     try
         waitforbuttonpress;
         f=get(ax,'CurrentPoint');
         x=round(f(1,2));
         y=round(f(1,1));
        if(x< size(A,1) && y < size(A,2))
          %Check whether x and y are less than the image size
         point(1)=x;
         point(2)=y;
         undocolor=[A(x,y,1); A(x,y,2); A(x,y,3);];
         colorfill(x,y);
        end
     catch
        originalcolor='null';
     end
end



%Fill the image with the RGB value



function colorfill(x,y)
        label=BW(x,y);
        [r c]=find(BW==label);
         for i=1:size(r,1)
            A(r(i,1),c(i,1),:)=rgb;
        end
        imshow(A);
    end
end
Watch the video:                       Painting the image using MATLAB.
GET THE SAMPLE IMAGE AT:http://www.clker.com/clipart-jumping-horse-outline.html

like button Like "IMAGE PROCESSING" page

Paint application in MATLAB

Let’s Paint


This paint application will fill the closed boundary with the specified color.
First, Create image with the well defined boundary value.
                                 

In my application, I created images with boundary/border with black pixel value ie [R,G,B]=[0,0,0] and the rest of the area with white [R,G,B]=[255,255,255].




How to use the application:
1.     Select one image file from the pop up menu.
2.     Select a color and click the image portion to fill the clicked area.
3.     If you need to fill the image portion with another color then click another color button
4.     Click undo button to come back to the previous point.
5.     Click save button to save the file.







This application is based on floodfill algorithm and the same application can be done using bwlabel concept. Check: http://angeljohnsy.blogspot.com/2011/06/paint-application-using-bwlabel-concept.html

Code explanation:

    First create a figure with color buttons, axis and the pop-up menu for selecting the image.
 I am showing only .png(Portable Network Graphics) files in the pop-up menu.
directory=dir('*.png');

When the user selects the image file then the function ‘displayfile’ is called.





function paintapp
scz=get(0,'ScreenSize');
figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Paint Application','Resize','off');
inputs=['RED   ';'GREEN ';'BLUE  ';'YELLOW';'BLACK ';'WHITE ';'ORANGE';'PURPLE';'BROWN ';'PINK  ';'GRAY  ';'LGREEN';'Save  ';'undo  '];
global top targetcolor replacecolor x y A color undocolor originalcolor point filename;
top=1;
targetcolor=[255 255 255];
xaxis=500;
yaxis=400;
for k=1:size(inputs,1)
uicontrol('Style','pushbutton','position',[xaxis yaxis 80 30],'String',inputs(k,:),'Callback',@paintme);
xaxis=xaxis+90;
if(mod(k,2)==0)
    yaxis=yaxis-50;
    xaxis=500;
end
end
ax=axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);
directory=dir('*.png');
files={directory.name}';
uicontrol('Style','popupmenu','position',[500 450 160 30],'Value',1,'String',files,'Callback',@displayfile);


The file is obtained from the string array ‘files’ and the image is displayed.

function displayfile(obj,~)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        A=imread(filename);
        image(A);
    end
    

When the user clicks the color button the function ‘paintme’ is called.
Here, targetcolor is the color value at the point where the user clicks.


The replacecolor is the color the user selects from the colors displayed.


Based on the color selected the color value to be filled in the area is stored in ‘replacecolor’. If the user selects the undo button the color to be replaced will the replacecolor and the target color will be filled at the place.
                                                                 

                                                                                                                      






function paintme(object,~)
   color=get(object,'String');
   if(~isempty(A))
   switch(color(1,:))
       case 'RED   '
           while(strcmp(color,'RED   ')==1)
           replacecolor=[254 1 1];
            getpoints;
           end
       case 'GREEN '
           while(strcmp(color,'GREEN ')==1)
           replacecolor=[1 60 21];
          getpoints;
           end
       case 'BLUE  '
           while(strcmp(color,'BLUE  ')==1)
               replacecolor=[20 120 120];
           %replacecolor=[1 1 254];
            getpoints;
           end
       case 'YELLOW'
           while(strcmp(color,'YELLOW')==1)
           replacecolor=[240 240 1];
           getpoints;
           end
       case 'BLACK '
           while(strcmp(color,'BLACK ')==1)
           replacecolor=[1 1 1];
            getpoints;
           end
       case 'WHITE '
           while(strcmp(color,'WHITE ')==1)
           replacecolor=[254 254 254];
            getpoints;
           end
       case 'ORANGE'
          while(strcmp(color,'ORANGE')==1)
          replacecolor=[250 90 1];
         getpoints;
          end
       case 'PURPLE'
           while(strcmp(color,'PURPLE')==1)
           replacecolor=[90 20 60];
            getpoints;
           end
       case 'BROWN '
           replacecolor=[90 20 10];
           while(strcmp(color,'BROWN ')==1)
            getpoints;
           end
       case 'PINK  '
           while(strcmp(color,'PINK  ')==1)
           replacecolor=[220 90 90];
            getpoints;
           end
          
            case 'LGREEN'
           while(strcmp(color,'LGREEN')==1)
            replacecolor=[20 240 10];
            getpoints;
           end
       case 'GRAY  '
      
           while(strcmp(color,'GRAY  ')==1)
           replacecolor=[20 20 20];
            getpoints;
           end
          
        case 'undo  '
                floodfill(point(1),point(2),undocolor,originalcolor);
       case 'Save  '
           fname=strcat('my',filename);
           msg=strcat('File name:',fname);
           msgbox(msg,'FILE SAVED');
          
   end
   end
   
end

 If the user clicks the save button the file is saved.




The user should click on the image to fill with the replace color.
The ‘waitforbuttonpress’ function is used to get the user input graphically by a mouse click.
When user clicks a particular point, the x and y co-ordinate value is obtained using ‘get(ax,'CurrentPoint')’.
   
function getpoints
      
     try
         waitforbuttonpress;
         f=get(ax,'CurrentPoint');
         x=round(f(1,2));
         y=round(f(1,1));
        
         % check whether x and y is less than the size of the image
         targetcolor(1)=A(x,y,1);
         targetcolor(2)=A(x,y,2);
         targetcolor(3)=A(x,y,3);
        
         point(1)=x;
         point(2)=y;
         undocolor=replacecolor;
         originalcolor=targetcolor;
         floodfill(x,y,targetcolor,replacecolor);
         color='null';
    
     catch
         color='null';
     end
        
       
    end
    
Floodfill or bucketfill algorithm is used to fill the image portion.

function floodfill(x,y,targetcolor,replacecolor)
    top=1;
    queue=zeros(1);
   
    if((A(x,y,1)==targetcolor(1))&&(A(x,y,2)==targetcolor(2))&&(A(x,y,3)==targetcolor(3)))
        queue(top,1)=x;
        queue(top,2)=y;
        top=top+1;
        i=1;
     while(i~=top)
          l= queue(i,1);
          m=queue(i,2);
        
             
         if((A(l,m,1)==targetcolor(1))&&(A(l,m,2)==targetcolor(2))&&(A(l,m,3)==targetcolor(3)))
            
             w=m;
             e=m;
             wnode=1;
             enode=1;
             while((A(l,w,1)==targetcolor(1))&&(A(l,w,2)==targetcolor(2))&&(A(l,w,3)==targetcolor(3)))
                 wnode=wnode+1;
                 w=w-1;
             end
             while((A(l,e,1)==targetcolor(1))&&(A(l,e,2)==targetcolor(2))&&(A(l,e,3)==targetcolor(3)))
                 enode=enode+1;
                 e=e+1;
             end
             w=m+1;
             e=m-1;
             for j=1:wnode-1
                 A(l,w-j,1)=replacecolor(1);
                 A(l,w-j,2)=replacecolor(2);
                A(l,w-j,3)=replacecolor(3);
             end
             
             for j=1:enode-1
                A(l,e+j,1)=replacecolor(1);
                A(l,e+j,2)=replacecolor(2);
                A(l,e+j,3)=replacecolor(3);
             end
             snode=m-wnode+2;
             for j=snode:(snode+enode+wnode-4)
                if((A(l+1,j,1)==targetcolor(1))&&(A(l+1,j,2)==targetcolor(2))&&(A(l+1,j,3)==targetcolor(3)))
                   queue(top,1)=l+1;
                   queue(top,2)=j;
                   top=top+1;
               end
                if((A(l-1,j,1)==targetcolor(1))&&(A(l-1,j,2)==targetcolor(2))&&(A(l-1,j,3)==targetcolor(3)))
               queue(top,1)=l-1;
               queue(top,2)=j;
               top=top+1;
                end
                end
        
         end
        i=i+1;
    end
    end
    image(A);
end
end


References:
-------------------------------------------------------------------------------------------------------------
GET FREE IMAGES AT:http://www.clker.com/





like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com