Digital Image Processing using Matlab
March 18, 2011 Leave a comment
Here, in this post I will write a code which will find the area in the picture where the sentence “Digital Image Processing” is written and outlines it.
So the result should be the picture where the sentence “Digital Image Processing” is highlighted.
%Reading image
I = imread(‘img1.jpg’);
%Show the original image
figure; imshow(I); title(‘Original Image’);
% Add some text on the right side of the picture, if you want to place this code in one
%line, remove dots.
text(size(I,2),size(I,1)+15,‘Edited by Mariam Kupatadze’,…
‘FontSize’,…
7,…
‘HorizontalAlignment’,…
‘right’);
%Detect edges using sobel method
Isobel = edge(I,‘sobel’);
%Show image
figure; imshow(Isobel); title(‘Edge Detected Image’);
text(size(I,2),size(I,1)+15,‘Edited by Mariam Kupatadze’,…
‘FontSize’,…
7,…
‘HorizontalAlignment’,…
‘right’);
%Remove objects which are connected to the border
Inobord = imclearborder(Isobel);
%Show Image
figure; imshow(Inobord); title(‘Border Cleared Image 1’);
text(size(I,2),size(I,1)+15,‘Edited by Mariam Kupatadze’,…
‘FontSize’,…
7,…
‘HorizontalAlignment’,…
‘right’);
%Creates a disk-shaped structuring element by radius=15
StructEl1 = strel(‘disk’,15);
%Creates a linear structuring elements
StructEl2 = strel(‘line’, 3, 90);
StructEl3 = strel(‘line’, 3, 0);
%Remove the linear gaps from the picture
Idil = imdilate(Inobord, [StructEl2 StructEl3]);
%Because of, there left extra objects, I will dilate it by disk-shaped structuring element for to %make them connect to the border.
Idil = imdilate(Idil, StructEl1);
%Show image
figure; imshow(Idil); title(‘Dilated Image’);
text(size(I,2),size(I,1)+15,‘Edited by Mariam Kupatadze’,…
‘FontSize’,…
7,…
‘HorizontalAlignment’,…
‘right’);
%Again remove objects which are connected to the border
Inobord1 = imclearborder(Idil);
%Show image
figure; imshow(Inobord1); title(‘Border Cleared Image 2’);
text(size(I,2),size(I,1)+15,‘Edited by Mariam Kupatadze’,…
‘FontSize’,…
7,…
‘HorizontalAlignment’,…
‘right’);
%Fill existing holes in the picture
Ifilled = imfill(Inobord1, ‘holes’);
%Show image
figure; imshow(Ifilled); title(‘Holes Filled Image’);
text(size(I,2),size(I,1)+15,‘Edited by Mariam Kupatadze’,…
‘FontSize’,…
7,…
‘HorizontalAlignment’,…
‘right’);
%Find perimeter of “Ifilled” image, with default connectivity 4
Ioutlined = bwperim(Ifilled);%The same as bwperim(Ifilled,4);
IOut = I;
%Highlight the desired area
IOut(Ioutlined) = 255;
%Show image
figure; imshow(IOut); title(‘Sentence Highlighted Image’);
text(size(I,2),size(I,1)+15,‘Edited by Mariam Kupatadze’,…
‘FontSize’,…7,…
‘HorizontalAlignment’,…
‘right’);