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

Array Search

Let's search the cell array and find the index. This simple example will help to find the index in cell array of large size.

names={'Hilda' 'Helen' 'Ada'; 'Matthew' 'Jenny' 'Justin'; 'Agnes' 'Merlin' 'Mark';}

names = 

    'Hilda'      'Helen'     'Ada'   
    'Matthew'    'Jenny'     'Justin'
    'Agnes'      'Merlin'    'Mark'  

I need to know whether the name 'Justin' exists in this cell array

find(strcmp(names,'Justin')==1)

ans =

     8

Let me retrieve the name 'Justin'  , using the index

names{8}

ans =

Justin


like button Like "IMAGE PROCESSING" page

Adding elements to a 2d array

A=[100 230 11; 22 10 15; 2 3 90;]

A =

   100   230    11
    22    10    15
     2     3    90

To add a row at the beginning:

A=[[10 20 30]; A]

A =

    10    20    30
   100   230    11
    22    10    15
     2     3    90

To add a row at the end:

A=[A; [10 20 30];]

A =

   100   230    11
    22    10    15
     2     3    90
    10    20    30


To add a column at the beginning:

A=[[10; 20 ;30] A]

A =

    10   100   230    11
    20    22    10    15
    30     2     3    90


To add a column at the end:

A=[A [10; 20 ;30]]

A =

   100   230    11    10
    22    10      15    20
     2     3        90    30


To add a row in the middle:


A=[10 12 14 16; 3 6 9 12; 4 8 12 16; 5 10 15 20;]

A =

    10    12    14    16
     3     6     9    12
     4     8    12    16
     5    10    15    20


A=[A(1:2,:); [8 16 24 32]; A(3:4,:);]

A =

    10    12    14    16
     3     6     9    12
     8    16    24    32
     4     8    12    16
     5    10    15    20

To add a column in the middle:


A=[A(:,1:2) [100; 200; 300; 400;] A(:,3:4)]

A =

    10    12   100    14    16
     3     6     200     9    12
     4     8     300    12    16
     5    10    400    15    20



like button Like "IMAGE PROCESSING" page

Tiling effect in MATLAB


Tiling effect

original Image
          Let’s make some random rectangles from the image. 

MATLAB CODE:

A=imread('tulip2.jpg');
C=uint8(zeros(size(A)));
i=1; j=1;
col=0;
row=0;
m=1;n=1;
%change the rsz and csz value to obtain pieces in different sizes
rsz=15;
csz=36;

%change the tsz_row and tsz_col values to make some gaps between the pieces.

tsz_row=3;
tsz_col=5;


while ( i < size(A,1))
%check whether i is less than size of (A,1)

while(m+i+row < size(A,1)&& n+j+col < size(A,2))

    
        %size of the tiles varies depending upon the random number
        C(m+i:m+i+row,n+j:n+j+col,:)=A(i:i+row,j:j+col,:);
      
        m=ceil(rand(1)*tsz_row);
        n=ceil(rand(1)*tsz_col);
        col=ceil(rand(1)*csz);
        row=ceil(rand(1)*rsz);
        j=j+col;
       
   
    end
     i=i+row;
     j=1;
end
figure,imshow(C);
With small gaps


With Large gaps
Subscribe to 'Image processing' to get the complete code.


like button Like "IMAGE PROCESSING" page

Array sorting in MATLAB

                 Sorting an array and retrieving the minimum and maximum values with the index may seem simpler but this helped me in many ways especially when working with arrays. So this is just a refresh on how to sort a 1 and 2d arrays.

Single dimensional array sorting

To sort the array and display the position (index):

A = [5 8 9 10 100 7 50 8 88 6;];

Before sorting:
A =   5     8     9    10   100     7    50     8    88     6

[sorted_array,pos]= sort(A)

After sorting: Here the sorted array and the array position of each value will be displayed.
By default, sorting type is ‘ascending’.


sorted_array =     5     6     7     8     8     9    10    50    88   100


pos =     1    10     6     2     8     3     4     7     9     5


To find the maximum value:
A =   5     8     9    10   100     7    50     8    88     6

[max_value,max_pos]=max(A)

max_value  =    100


max_pos  =      5

To find the minimum value:

A =   5     8     9    10   100     7    50     8    88     6


[min_value,min_pos]=min(A)

min_value =      5


min_pos =      1


Two dimensional array sorting:

B=[17 5 8 9; 45 23 50 6; 7 49 21 11]


Before sorting:

B =     17     5     8     9
           45    23    50     6
            7    49    21    11


[sorted_array, pos]=sort(B(:))

After sorting:

sorted_array =

     5
     6
     7
     8
     9
    11
    17
    21
    23
    45
    49
    50


pos =

     4
    11
     3
     7
    10
    12
     1
     9
     5
     2
     6
     8

Maximum value:
[sorted_array, pos]=max(B(:))

sorted_array =

    50


pos =

     8


Minimum Value:
[sorted_array, pos]=min(B(:))

sorted_array =

     5


pos =

     4

To retrieve the minimum value: B(4)
ans =      5
like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com