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

Simple GUI Calculator in MATLAB

Simple GUI calculator

 I first created a simple GUI for the calculator to perform +-*/.
 When the user click the button the function arithmetic is called.


Additional Information: To perform all the complex calculations use Texas Instruments.








function calculator
figure('Position',[200 200 220 200],'Name','Calculator','NumberTitle','off','MenuBar','None','Resize','off');
txt=uicontrol('Style','Text','Position',[10 165 200 30],'String','0');
global stack value top op tops opstack num1 num2 flag;
flag=0;
x=10;
y=130;
top=1;
tops=1;
name=['7','8','9','/','4','5','6','*','1','2','3','-','0','+','C','='];%k


for k=1:size(name,2)
   
   uicontrol('Style','Pushbutton','Position',[x y 50 30],'String' ,name(k),'Callback',@arithmetic) ;
   x=x+50;
   if(mod(k,4)==0)
   
       x=10;y=y-35;
      
   end
end




The String value of the pressed button is obtained using the get(object,’String’) function.

If the value is a number then it is placed in a stack.
If the value is a operator then the operator will be placed in the operator stack (opstack).
If the value is ‘C’ then the stacks top is set to 1.


    function arithmetic(object,~)
        num=str2double(get(object,'String'));
        if((num>=0)&&(num<=9))
            value=num;
            evaluate();
        else
            op=get(object,'String');
            if(op=='C')
                top=1;
                tops=1;
                set(txt,'String','0');
            else
            operator();
            end
        end
    end

     The function evaluate is called to add the number to the stack.
     If the stack (1) =0 and the stack (2) =9, then the stack (1) =9.
        This is because, when the user enters zero and then 1, the stack value will be ‘01’. To obtain the correct value, it is better to remove the zero and display as ‘1’.

      If the user enters numbers continuously without entering an operator then the values should be concatenated at each time.
For example, if the user enter 2, 4, 5 and then +, the values 245 should be displayed as a single number.
    


    function evaluate()
          stack(top)=value;
          if((stack(1)==0)&&(top==2))
                stack(1)=stack(top);
                top=top-1;
         end
       str=num2str(stack(1));
       for i=2:top
                str=strcat(str,num2str(stack(i)));
       end
            top=top+1;
            set(txt,'String',' ');
            set(txt,'String',str);
            flag=0;
        end

If the user enter an operator then the operator will be added to the operator stack and the first number to perform the arithmetic operation is stored in the variable ‘num1’.

   If the user enters an operator again then the operator will be added to the stack and the second number will be stored in the variable ‘num2’.

After getting the two numbers, the corresponding arithmetic operation is done based on the operator in the opstack(1).
The result will be stored in the variable ‘num1’.










   function operator()
        
     if((top~=1)||(flag==1))
      
        opstack(tops)=op;
        tops=tops+1;
       
        if((tops==2)&&(flag~=1))
           
            str=num2str(stack(1));
            for i=2:top-1
                str=strcat(str,num2str(stack(i)));
            end
            num1=str2double(str);
            flag=1;
            top=1;
        elseif(tops>=3)
                     
            if(flag==0)
            str=num2str(stack(1));
            for i=2:top-1
                str=strcat(str,num2str(stack(i)));
            end
            num2=str2double(str);
           
          
            top=1;
           
            if(opstack(tops-1)=='=')
                
                calculate();
             set(txt,'String','           ');
            set(txt,'String',num2str(num1));
           
            flag=1;
            tops=1;
           
           
           
            
            else  
               
            calculate();
            set(txt,'String','           ');
            set(txt,'String',num2str(num1));
           
            flag=1;
            tmp=opstack(tops-1);
            opstack(1)=tmp;
            tops=tops-1;
            end
            else
                tmp=opstack(tops-1);
                opstack(1)=tmp;
                tops=tops-1;
               
            end
        end
       
      end
    end


    function calculate()
        switch (opstack(1))
                case '+'                
                       num1=num1+num2;
                case '-'
                       num1=num1-num2;
                case '/'
                       num1=num1/num2;
                case '*'
                        num1=num1*num2;
                       
          
        end
    end
       
    end

 Did u find the material useful?




like button Like "IMAGE PROCESSING" page

Excel file to matlab


EXCEL2UITABLE:

Method: 1
The data from excel file can be imported to matlab using xlsread function.

[numericdata,stringdata]=xlsread(filename, sheet name);

 The numeric data in the sheet will be stored in the first variable ‘numericdata’.

The strings or characters in the sheet will be stored in the second variable ‘stringdata’.

Method : 2
  Using uiimport , the user can interactively select the rows and data.
Syntax: uiimport('Employee.xls')

Sample program:
Here, a excel file by name ‘Employee.xls’ with sheet name ‘Employee Details’ contains some employee details. The details are imported from excel to matlab.

To obtain the heading of each column:
%[data heading]=xlsread('Employee.xls','Employee Details','A1:B1:C1');


[ID Text]=xlsread('Employee.xls','Employee Details');

The numeric and string data are stored in the ID and Text respectively.



To display the sheet in a table format:
The employee id,employee name and employee date of join is stored.
To calculate the experience of each employee, the date of join obtained is converted to datenum and it is subtracted from the current date.
num=floor(now)-datenum(Text(i,3));
The number of years is obtained by using the function datestr(num,11);
Here the value 11 will return the years in ‘YY’ format.

f = figure('Position',[400 400 400 150],'Name','Employee Details','NumberTitle','off');
dat=cell(size(Text,1),size(Text,2)+1);
for i=2:size(Text,1)
    num=floor(now)-datenum(Text(i,3));
    exp=datestr(num,11);
   
    dat(i-1,1)={ID(i-1,1)};
    dat(i-1,2)=Text(i,2);
    dat(i-1,3)=Text(i,3);
    dat(i-1,4)={exp};
   
end


columnname={'Employee ID','Employee Name','Date Of Join','Experience'};

columnformat = {'numeric', 'char', 'char', 'numeric'};

uitable('Units','normalized','Position',...
            [0.1,0.1,.9,.9], 'Data', dat,...
            'ColumnName', columnname,...
            'ColumnFormat', columnformat,'RowName',[]);



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