This post is about labeling the connected components
in a binary image and crop the connected components based on the
label. The main two functions used for this simple operation are ‘bwlabel’ and ‘regionprops’.
I used ‘bwlabel’ to label
the connected components. After labeling,
I used ‘regionprops’ function to find the rectangle containing the region. To find the rectangle points, I used ‘BoundingBox’
property.
Then the labeled components are
automatically cropped and the image is displayed. To crop an image, ‘imcrop’
function is used.
%Convert the Image to binary
B=im2bw(A);
%Fill the holes
C=imfill(B,'holes');
%Label the connected components
[Label,Total]=bwlabel(C,8);
figure,imshow(C); title('Labelled Image');
%Rectangle containing the region
Sdata=regionprops(Label,'BoundingBox');
%Crop all the Coins
for i=1:Total
Img=imcrop(A,Sdata(i).BoundingBox);
Name=strcat('Object Number:',num2str(i));
figure,imshow(Img); title(Name);
end
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.
%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).
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)’.
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:
Read an image (A) and convert it into binary image.
Define a structuring element (B).
Initialize the Label matrix with zeros.
Find a non-zero element position in the input matrix A.
Initialize
a matrix X with zeros and place 1 in the non-zero element position found
in the previous step.
Perform
dilation using the structuring element B on matrix X. i.e. imdilate(X,B);
Perform
intersection with the matrix A. Y= A&imdilate(X, B).
Check
whether Y==X. If no, then X=Y and perform steps 6 and 7 again else stop
the iteration.
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.
Similarly,
place zero in those positions in the input matrix A.
Again
find a non-zero element position in the matrix A. If found, goto step 5
else stop the iteration.
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.
MATLAB CODE to get images from video: To extract frames from a video.
Frames can be obtained from a video and converted into
images. To convert a video frame into an image, the MATLAB function ‘frame2im’
is used.
To read a video in avi format,
the function ‘aviread’ is used. The
original format of the video that I am using as an example is .gif file format.
I converted the .gif file format image into an avi format video. Learn how to convert a gif image into a video...
For instance, in the ‘fun.avi’
video I am interested in the frame number 58. So I am converting the 58th
frame into an image.
MATLAB CODE:
%How to get frames from a
video
mov=aviread('fun.avi');
Im=frame2im(mov(1,58));
figure,imshow(Im);
Frame 58
The above process can be done using 'mmreader' function also. MATLAB CODE: Vobj=mmreader('fun.avi'); Im=read(Vobj,58); imshow(Im);
After playing the video, I thought
of modifying some frames. So I converted the frames from 58 to 64 into images
and added a smiley to the images by performing image arithmetic operations.
Then I converted those images into
frames and saved as a video file in ‘smiley.avi’.
Lmov=aviread('smiley.avi');
MIm=frame2im(Lmov(1,1));
figure,imshow(MIm);
Modified Frame 58
%Preallocating the structure
array
frame=struct('cdata',1,'colormap',cell([1 91]));
I added the modified frames to
the original video.
%Modify the video in the
middle
frame(1,1:57)=mov(1,1:57);
frame(1,58:87)=Lmov(1,1:30);
frame(1,88:91)=mov(1,64:67);
implay(frame,5);
If we
are interested in playing only some of the frames in the original video then,
we can view those frames alone. Here I am interested in viewing the frames from
27 to 33.
%Here 1 is for the number of
times the video has to be repeated
%27:33 indicates the frames from
27 to 33.
movie(scz,mov,FRange,5);
We can also
save the desired frames into a file. An avifile object is created with the file
name ‘VidPart.avi’. Then the desired frames from 27 to 33 are added by using
the syntax ‘addframe’.
Using a single JPEG image, we can create image sequences. To create image sequences, we need to convert
the image into frame. The matlab function ‘im2frame’
is used to create frame from an image. In this example, I took an image and
created some frames by performing rotation operation on that image.
Initially, I pre allocated a structure array. When the function ‘im2frame’ is used on an
image, a frame is created. The frame is
a structure that contains the cdata and the colormap.
Example:
I=imread(‘ball.jpg’);
frame(1)=im2frame(I);
frame(1)
ans =
cdata:
[323x400x3 uint8]
colormap: []
After allocating the space for the frames, I rotated the
image from 1 to 360 degrees with the interval of 10 degrees. 36 frames will be obtained.
During each rotation operation, the size of the image will
be modified. So, I resized the image to the original size after every rotation.