Follow by Email

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

Recent Posts

Text On Sphere - MATLAB CODE


Text on Sphere-MATLAB CODE

              We can see how to wrap a text around a sphere. First obtain the x,y and z co-ordinates of  the sphere.These co-ordinates can be used as an input for surface object.
Surface object generates sphere using the co-ordinates.  Example:[x,y,z]=sphere(25); surface(x,y,z);
Now read a text image and convert it into binary.  Label the components in the image. You can also check how to label components without BWLABELfunction in MATLAB.   Now use the labeled image as a CDATA value in surface object.
Example: surface(x,y,z, ‘CDATA’,matrix);

MATLAB CODE:


%Input a text image
A=imread('quotes.png');


         


A=~im2bw(A);
B=bwlabel(A,8);
F=double(B);
mytext=flipud(F);


% Create the surface.
[x,y,z] = sphere(100);


figure,surface(x,y,z,'CData',mytext,'FaceColor','texturemap','FaceLighting','phong','EdgeLighting','phong','EdgeColor','none');




%set the colormap. Some built-in colormaps are pink, copper, jet, hot...
%colormap pink

%Here I created my own colormap, the colors are white[1 1 1], black[0 0 0] and %blue [0 0 1]. Initialize the map with zeros(black) for 256x3 matrix. %Add blue 
%color to some components(words). Initialize the first row with [ 1 1 1](white).





map=zeros([253 3]);
map(1,:)=0;
map(50:80,3)=1;
colormap(map);



%Create light object
light('position',[1 2 0 ],'Style','infinite','color',[0.8 0.7 0.8]);
light('position',[-2 -3 0 ],'color',[0.8 0.7 0.8]);


%Specify the viewpoint
axis square off
view(12,0);










Another Example: To create a GLOBE from a WORLD MAP



INPUT IMAGE















To convert a RGB image into SPHERE:
MATLAB CODE:




A=imread('mountain.jpg');
[mytext,map]=rgb2ind(A,256);
mytext=flipud(mytext);

% Create the surface.
[x,y,z] = sphere(100);

figure,surface(x,y,z,'CData',mytext,'FaceColor','texturemap','FaceLighting','phong','EdgeLighting','phong','EdgeColor','none');
%set the colormap. 

colormap(map);


%Create light object
light('position',[1 2 0 ],'Style','infinite','color',[0.8 0.7 0.8]);
light('position',[-2 -3 0 ],'color',[0.8 0.7 0.8]);


%Specify the viewpoint
axis square off
%view(12,0);
view(180,0);










Hope you enjoyed this post. Check the link for more images:http://www.facebook.com/media/set/?set=a.352096201499476.75050.241768945865536&type=1

like button Like "IMAGE PROCESSING" page

How to add caption/subtitles to Video in MATLAB


                                    In my previous post, I discussed about how to create videofrom still images and now we can add subtitles to the video. I used the ‘text’ function to add the subtitles.
The procedure to add subtitles to the Video:
·         Read an video file
·         Read each video frame data
·         Add text to each frame using the syntax ‘text(x,y,string)’.
·         Use imshow to display the image.
·         Get the frame from the figure displayed.

MATLAB CODE:
%Input a Video file
obj=mmreader('Poem.avi');
A=read(obj);
j=1;
%Preallocate the frame structure
Frame=struct('cdata',1,'colormap',cell([1 100]));

                                 INPUT VIDEO
 

%Get the first frame as image
%Add text
%Use 'getframe' to grab the figure
Caption={'How to add Caption/Subtitles';'to video in MATLAB';'GET CODE AT http://angeljohnsy.blogspot.com/';};
I=A(:,:,:,1);
imshow(I),hold on
text(80,100,Caption{1},'Color','w','FontWeight','Bold','FontSize',30);
text(150,200,Caption{2},'Color','w','FontWeight','Bold','FontSize',30);
text(300,480,Caption{3},'Color','r','FontWeight','Bold','FontSize',15);
Frame(j:j+9)=getframe;
j=j+10;


%Get the Second frame as an image i.eA(:,:,:,2);
%Add the Poem title and the author name
Title_={'THE ROAD NOT TAKEN';'-ROBERT FROST'};
I=A(:,:,:,2);
imshow(I), hold on
text(40,100,Title_{1},'Color','k','FontWeight','Bold','FontSize',40);
text(350,550,Title_{2},'Color','k','FontWeight','Bold','FontSize',30);
Frame(j:j+9)=getframe;
j=j+10;


%Create a cell array with the Poem versus
Txt={'TWO roads diverged in a yellow wood,';
'And sorry I could not travel both';
'And be one traveler, long I stood';
'And looked down one as far as I could';   
'To where it bent in the undergrowth;';
'Then took the other, as just as fair,';   
'And having perhaps the better claim,';
'Because it was grassy and wanted wear;';  
'Though as for that the passing there';
'Had worn them really about the same,';
'And both that morning equally lay';   
'In leaves no step had trodden black.';
'Oh, I kept the first for another day!';   
'Yet knowing how way leads on to way,';
'I doubted if I should ever come back.';
'I shall be telling this with a sigh'
'Somewhere ages and ages hence:';  
'Two roads diverged in a wood, and I—';
'I took the one less traveled by,';
'And that has made all the difference.';};

%display the image
%Add the poem lines
%Grab the frame
%There are 20 lines totally
%Add 3 and 2 lines alternatively in each frame
    inc=0;
for i=3:size(A,4)
   
    n=mod(i,2);  
    I=A(:,:,:,i);
   
    imshow(I), hold on
for k=1:2+n
    text(80,440+(k*40),Txt{k+inc},'Color','w','FontWeight','Bold','FontSize',25);
   
end
    inc=inc+2+n;
    Frame(j:j+19)=getframe;
    j=j+20;
 end


Create a Video File:
obj=avifile('Road Not Taken.avi','FPS',3);
 obj=addframe(obj,Frame);
obj=close(obj);


                            VIDEO WITH  SUBTITLE






Explanation:
       text(40,100,Title_{1},'Color','k','FontWeight','Bold','FontSize',40);

                This code will add the text ‘THE ROAD NOT TAKEN’ with color font size 40, font weight bold and color black (k).

          

like button Like "IMAGE PROCESSING" page

Extraction of Connected components without using BWLABEL function


             Many of the visitors to this blog mailed me to post a MATLAB code for extracting the connected components.  In MATLAB, a function called BWLABEL is available to label the connected components.  
Based on the following iterative expression, the connected components are extracted.
where k=1,2,3…

Using the above expression, connected components are extracted without using the function BWLABEL
LABELLING:

MATLAB CODE:


I=imread('label3.jpg');





I=im2bw(I);

%Structuring element
B=strel('square',3);
A=I;
%Find a non-zero element's position.
p=find(A==1);
p=p(1);
Label=zeros([size(A,1) size(A,2)]);
N=0;

while(~isempty(p))
    N=N+1;%Label for each component
    p=p(1);
X=false([size(A,1) size(A,2)]);
X(p)=1;

Y=A&imdilate(X,B);
while(~isequal(X,Y))
    X=Y;
    Y=A&imdilate(X,B);
end

Pos=find(Y==1);

A(Pos)=0;
%Label the components
Label(Pos)=N;

p=find(A==1);

end
imtool(Label);












EXPLANATION:
  1. Read an image (A) and convert it into binary image.
  2. Define a structuring element (B).
  3. Initialize the Label matrix with zeros.
  4. Find a non-zero element position in the input matrix A.
  5. Initialize a matrix X with zeros and place 1 in the non-zero element position found in the previous step.
  6. Perform dilation using the structuring element B on matrix X. i.e. imdilate(X,B);
  7. Perform intersection with the matrix A. Y= A&imdilate(X, B).
  8. Check whether Y==X. If no, then X=Y and perform steps 6 and 7 again else stop the iteration.
  9. Find the non-zero elements position in the Y. In matrix Label place a number N in those positions. N is for labeling the connected components.
  10. Similarly, place zero in those positions in the input matrix A.
  11. Again find a non-zero element position in the matrix A. If found, goto step 5 else stop the iteration.
  12. Using the labels the connected components can be extracted.


EXTRACTION:

MATLAB CODE:






%Extracting the components
Im=zeros([size(A,1) size(A,2)]);
ele=find(Label==1);
Im(ele)=1;
figure,imshow(Im);title('Label:1');






To obtain the first component, find the positions with value=1 in the Label Matrix. Similarly, other components can be extracted.




%Extracting the characters 'I M A G E'
ele=find(Label==2|Label==3|Label==6|Label==7|Label==9);
Im1=zeros([size(A,1) size(A,2)]);
Im1(ele)=1;
figure,imshow(Im1);title('Specific components');






From the Label matrix, I extracted the characters I, M, A, G and E alone by specifying the label numbers 2,3,6,7 and 9.


%Total number of Letters



Total=sprintf('Total Number of Letters:%d',N);
display(Total);




 The total number of components extracted in the above example Image:

Total =

Total Number of letters:14




%Differentiate each component with a specific color
RGBIm=zeros([size(Label,1) size(Label,2) 3]);
R=zeros([size(Label,1) size(Label,2)]);
G=zeros([size(Label,1) size(Label,2)]);
B=zeros([size(Label,1) size(Label,2)]);
U=64;
V=255;
W=128;
for i=1:N
    Pos=find(Label==i);
    R(Pos)=mod(i,2)*V;
    G(Pos)=mod(i,5)*U;
    B(Pos)=mod(i,3)*W;
   
   
 end
RGBIm(:,:,1)=R;
RGBIm(:,:,2)=G;
RGBIm(:,:,3)=B;
RGBIm=uint8(RGBIm);
figure,imshow(RGBIm);title('Labelled Components');






For each label, unique color is given.  Here is another example for labeling and extracting the components. Read the image, ‘coins.png’ , convert it into binary and fill the holes. Then perform labeling and extraction.





Total =

Total Number of coins: 10



like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Related Posts Plugin for WordPress, Blogger...
Google ping Hypersmash.com