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

Reverse the words position in a string


This can be done in two ways.
First Method:
 a.Find the length of the string
 b. Use MATLAB built in function strtok to separate the first word and the remaining sentence.
For instance, the input string is ‘Lets Learn together... Happy Reading’.
[Token,remain]=strtok(input_string);
The variable token will contain ‘Lets
And the variable remain will contain ‘Learn together… Happy Reading’.
Use while loop to extract all the strings.

c. In the first iteration, the string in the token variable is concatenated with a empty variable ‘t’.
Now t has ‘Hello’.
In the second iteration, token has ‘Learn’ and remain has ‘together… Happy Reading’.
Now the string ‘Learn’ is concatenated with the string t.

d. Here to preserve the space ,the syntax [string2, ‘  ’,string1] is used.
The length of the input_string gets reduced during each iteration and finally when all the strings are extracted, the loop is terminated.

MATLAB CODE:
%To reverse the words position in a string
str='Lets Learn together... Happy Reading';
%str=fliplr(str);
len=length(str);
t='';

 while(len>0)
     [token,str]=strtok(str);
     len=length(str);
     %token=fliplr(token);
     %t=[token,' ',t];
     t=[' ',token,t];
        
 end
 display(t);


Output:

t =

 Reading Happy together... Learn Lets



Second  Method:

This  differs  in the way of concatenation.
The input string position is reversed. And inside the loop after extracting each token, it is again reversed.
Then  the  string ‘t’ is concatenated with the token. The first method itself sufficient and just know the other way also.
like button Like "IMAGE PROCESSING" page

Pop-Up Menu


A very simple example to show how the pop-up menu / Drop down list in MATLAB works.



MATLAB CODE:
function Popup_menu
global filename Img  I T;
scz=get(0,'ScreenSize');

figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 800 500],'MenuBar','None','NumberTitle','off','Name','PopUpMenu Example','Resize','off');
axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);

directory=dir('*.jpg');
files={directory.name}';

%Get the File Name
%Position =[X Y length width]
uicontrol('style','text','Position',[565,453 60 20]','String','Filename:');
%The file names are the options here.
uicontrol('Style','popupmenu','position',[628 445 170 30],'Value',1,'String',files,'Callback',@displayfile);

%Select the Conversion type
uicontrol('style','text','Position',[565,415 90 20]','String','Image 
Conversion:');

%The options for image conversion are : Original Image, Convert to grayscale,
Convert to binary, Edge detected Image.
uicontrol('Style','popupmenu','position',[565 375 200 30],'Value',1,'String',{'Convert to GrayScale','Convert to Binary','Original Image','Edge detected Image'},'Callback',@convert);

%Select the edge type
T=uicontrol('Style','popupmenu','position',[565 305 200 30],'Value',1,'String',{'Canny','Sobel','Prewitt','log'},'callback',@findedge);
F=uicontrol('style','text','Position',[565,345 90 20]','String','Edge Type:');

%These components can be made visible or invisible
set(T,'Visible','off');
set(F,'Visible','off');

function displayfile(obj,eve)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        Img=imread(filename);
        imshow(Img);
end

%Switch case
%Value 1 performs the conversion of the orginal image to grayscale image
%Value 2 performs the conversion of the orginal image to binary image
%Value 3 shows the orginal image
%Value 4 performs the edge detection of the image based on the type.




    function convert(obj,eve)
        ptr=get(obj,'value');
        switch ptr
            case {1}
                set(T,'Visible','off');
                set(F,'Visible','off');
                I=rgb2gray(Img);
                imshow(I);
            case {2}
                set(T,'Visible','off');
                set(F,'Visible','off');
                I=im2bw(Img);
                imshow(I);
            case {3}
                set(T,'Visible','off');
                set(F,'Visible','off');
                imshow(Img);
            case {4}
            set(T,'Visible','on');
            set(F,'Visible','on');
   
                           
        end
    end

                                                              
                                                             
                                                              


                                      
                                 
                                      
                                     


       
  %To perform edge detection based on the type
    function findedge(obj,event)
            ptr=get(obj,'value');
            B=rgb2gray(Img);
            switch ptr
                case {1}
                    imshow(~edge(B,'canny'));
                case {2}
                    imshow(~edge(B,'sobel'));
                case {3}
                    imshow(~edge(B,'Prewitt'));
                case {4}
                    imshow(~edge(B,'log'));
                   
            end
           
           
        end
               
   
end




like button Like "IMAGE PROCESSING" page

Some Random effects

  • Negative Image
Subtract 255 from the RGB image, to obtain negative image.
MATLAB CODE:
%Here the  I am subtracting 255 from R,G and B.
A=imread('person.jpg');
imshow(255-A);

Original Image
Negative Image







  • Sine Wave

    First find the mid point of the RGB image.
    Convert the points from Cartesian to polar co_ordinates
    Find the sine value for the angle 'theta'
    Again convert the points to Cartesian co_ordinates

    SAMPLE CODE:
    x2=zeros([size(A,1) size(A,2)]);
    y2=zeros([size(A,1) size(A,2)]);
    for i=1:size(A,1)
        x=i-midx;
        for j=1:size(A,2)
    %Cartesian to Polar co-ordinates
               [theta1,rho1]=cart2pol(i,j-midy);
        
          
                  
            phi=sin(theta1);
          
    %Polar to Cartesian co-ordinates
            [l,m]=pol2cart(phi,rho1);
            x2(i,j)=ceil(l)+midx;
            y2(i,j)=ceil(m)+midy;
          
        end
    end

    Sine wave


Original Image

like button Like "IMAGE PROCESSING" page

Number of occurrences in cell array

To find the number of occurrence of a string in an array:

Consider a cell array
c={'one';'two';'three';'nine';'five';'two';'six';'one'; 'two';'five';};


The number of times the string ‘one’ occurs in the array

sum(strcmp('one',c))

ans =

     2

To find the number of occurrence of all the elements in the array:


%c={'one';'two';'three';'nine';'five';'two'; 'six';'one'; 'two';'five';}; %dim=1

c='one','one','two','two','three','three','three','four','five','six','five','five'};%dim=2;

dim=2;
%Initialize the 'cell_array'
cell_array=cell([2 size(c,dim)]);
display(c);
inc=1;
for i=1:size(c,dim)
    %Compare the elements in the 'cell_array' with the elements in 'c'
   if(strcmp(cell_array(1,:),c(i))==0)
       %If the element is not present, then add it to 'cell_array'.
       cell_array(1,inc)=c(i);
       %Find the number of occurence of the element
     num=   sum(strcmp(c(i),c));
     cell_array(2,inc)=num2cell(num);
     inc=inc+1;
   else
       %Delete if the element is already present in the 'cell_array'.
       cell_array(:,inc)='';
      
      
   end
  
  
end

display(cell_array);



The result shows the elements in the first row and the number of occurrences in the second row.
like button Like "IMAGE PROCESSING" page

How to draw in MATLAB

I used the function ‘getline’ to get the points from the user. Using plot function the lines are drawn.

MATLAB CODE:
scz=get(0,'ScreenSize');

fig=figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Draw Lines','Resize','off');



[x, y] = getline(fig);
plot(x,y);
axis([0 1 0 1]);

Draw lines
                    
To draw a rectangle:

x=zeros([1 5]);
y=zeros([1 5]);

r=getrect(fig);
x(:)=r(1);
y(:)=r(2);
x(2:3)=r(1)+r(3);
y(3:4)=r(2)+r(4);



plot(x,y);
axis([0 1 0 1]);

Draw Rectangle

The function ‘getrect’ returns the [xmin ymin width height].
We need 4 points to draw a rectangle.
The first point is (x,y) , here it is (xmin,ymin).
The second point is(x+width,y)
Third point is (x+width, y+height)
Fourth point is (x,y+height)
Since I am using the plot function I need to connect the 1st and the fourth point.
So I need a fifth point i.e (x,y) which completes the rectangle.
like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com