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

Darker Edges


Captured Image

Last week my friend was in need of her signature in digital format. Fortunately, it was not for any official purpose. So we captured the signature using the camera and used it.

The image that we acquired using the camera was not that much good. So we processed the image and then included in the document.

After this thing happened, I thought of trying the image with MATLAB.
First I adjusted the image intensity using the imadjust function . 
The syntax for the imadjust for rgb image is
imadjust(RGB,[Low_R Low_G Low_B; High_R High G High B],[]);
The Low values should always be lower than High values.
The range of these values between 0 to 1.
After Imadjust

Then I detected the edge of the image. Then to enlarge the image parts I dilated it. Use an appropriate structure element. Here I used 'disk'.
After that, using labeling method, I labeled all the connected components. Based on the size of the components the components where finally used and components whose size are less are ignored.
Edge detected Image

MATLAB CODE:



function my_edge
global filename A I ;
scz=get(0,'ScreenSize');
figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Pencil sketch Application','Resize','off');
axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);
Low=uicontrol('Style','slider','Position',[500,280 200 20],'Max',0.99 ,'Min',0,'Value',0.0,'SliderStep',[0.05,0.1],'Callback',@draw);
High=uicontrol('Style','slider','Position',[500,370 200 20],'Max',1,'Min',0.1,'Value',.5,'SliderStep',[0.05,0.1],'Callback',@draw);
uicontrol('Style','pushbutton','String','Done','Position',[500 200 40 20],'Callback',@pushme);
directory=dir('*.jpg');
files={directory.name}';
uicontrol('style','text','Position',[500,310 100 20]','String','Low:');
uicontrol('style','text','Position',[500,395 100 20]','String','High :');
uicontrol('style','text','Position',[500,455 100 20]','String','Filename:');
uicontrol('Style','popupmenu','position',[500 420 160 30],'Value',1,'String',files,'Callback',@displayfile);
    function displayfile(obj,eve)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        A=imread(filename);
        A=imresize(A,0.4);
        subplot('Position',[0.05 0.1 .6 .9]);imshow(A);
    end
    function draw(obj,eve)
        H=get(High,'Value');
        L=get(Low,'Value');
      uicontrol('Style','edit','position',[500 250 50 20],'String',L);
      uicontrol('Style','edit','position',[500 340 50 20],'String',H);
        if( L < H )
            %The intensity values for each R,G,B planes are adjusted.
            I=imadjust(A,[L L L; H H H],[]);
        end
           subplot('Position',[0.05 0.1 .6 .9]);imshow(I);
       end
                                                                                                         
                                                                                        
                                                                                         
                                                                                   
                                                                                    
    function pushme(obj,eve)
        myedges(I);
    end
    function myedges(A)
      
A1=rgb2gray(A);
B=(edge(A(:,:,1))|edge(A(:,:,2))|edge(A(:,:,3)));

Edge detected




SE=strel('disk',3);
C=~(imdilate(B,SE));
target=(ones([size(A1,1) size(A1,2)] ));
[BW,label]=bwlabel(~C,8);
for l=1:max(max(label))
[row, col] = find(BW==l);
if(size(row,1)>30)
for i=1:size(row,1)
    x=row(i,1);
    y=col(i,1);
    target(x,y)=C(x,y);
end
end
end


subplot('Position',[0 0.5 .8 .45]);imshow(A);title('Original Image');
subplot('Position',[0 0   .8 .45]);imshow(target);title('Final Image');
    end
end


Another example:

After Imadjust and edge detection (structuring element:disk Radius=1)


like button Like "IMAGE PROCESSING" page

Mean, Median , Variance, Standard deviation and Mode


A=[10 10 20 40 60; 12 1 2 3 4; 7 8 9 11 4 ; 3 4 5 9 10];

A =

    10    10    20    40    60
    12     1     2     3     4
     7     8     9    11     4
     3     4     5     9    10

%MATLAB code to find the mean, median, variance, standard deviation and mode for an image
%A=imread('pout.tif');
%Convert the image to type ‘double’.
%A=double(A);


[r,c]=size(A);

% MEAN:
 The average of sum of all the values in the matrix.
%To find the mean for each column
colmean=sum(A)/r;
display(colmean);


%To calculate the mean of the matrix
totmean=sum(A(:))/(r*c);
display(totmean);


%To calculate the variance and standard-deviation column-wise
myvar=zeros([c,1]);
mystd=zeros([c,1]);
for i = 1: c
    diff=A(:,i)-colmean(i);
    myvar(i)=sum(diff.^2)/(r-1);
    mystd(i)=sqrt(myvar(i));
end

display(myvar);
display(mystd);





%To calculate the variance and standard deviation

totdiff=(A-totmean).^2;
totsum=sum(totdiff(:));
nele=(r*c)-1;
totvar=totsum/nele;
display(totvar);
totstd=sqrt(totvar);
display(totstd);










%MEDIAN

 %To find the median of the matrix row-wise
 n=r/2;
 B=sort(A,1);
 if(mod(r,2)==0)
      rowmedian=(B(round(n),:)+B(round(n)+1,:))/2;
 else
     rowmedian=B(round(n),:);
 end
 display(rowmedian);

 %To find the median of the matrix column-wise
 n=c/2;
 B=sort(A,2);
 if(mod(c,2)==0)

     colmedian=(B(:,round(n))+B(:,round(n)+1))/2;
 else
     colmedian=B(:,round(n));
 end
 display(colmedian);




























%To find the total median of the image
 C=reshape(A,[],1);
 min1=min(C);
 max1=max(C);

 E=sort(A(:));
 num=round((r*c)/2);

 if( (mod(r,2)==0) || (mod(c,2)==0) )
     totmedian=(E(num)+E(num+1))/2;
    
 else
     totmedian=E(num);
 end
 display(totmedian);



    
%To find the mode of the matrix
   D=hist(C,min1:max1);
   D1=find(D==max(D));
   mymode=D1(1)+min1-1;
   display(mymode);
 






like button Like "IMAGE PROCESSING" page

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
Previous Post Next Post Home
Google ping Hypersmash.com