peak-to-valley segmentation
Show older comments
i need help for the vertical segmentation of my binary image by using the peak-to-valley method

notice the valley. I need to segment each character when it meet a valley. I saw many thesis that explain this method but it kinda hart to perform in matlab.
clear all,
close all
clc
f=imread('C:\Users\Yasmin\Documents\MATLAB\Data0.jpg');
f=imresize(f,[300 NaN]);
g=rgb2gray(f);
org=f;
H = fspecial('unsharp');
g2 = imfilter(g,H,'replicate');
w=[0 1 0; 1 -2 1; 0 1 0];
g3=imfilter(g2,w);
g4=1-im2double(g3);
se1=strel('disk',1);
%gi = imdilate(g4,se1);
%figure,imshow(gi)
ge=imerode(g4,se1); %%%%morphological image processing
%gdiff=imsubtract(gi,ge);
gdiff=mat2gray(ge);
gdiff=conv2(gdiff,[1 1;1 1]);
A=imadjust(gdiff,[0.5 0.7],[0 1],.1);
%B=logical(gdiff);
[a1 b1]=size(A);
figure,imshow(A)
[h,w,f]=size(A);
org=A;
A=padarray(A,[0 10]);
%c=bwlabel(A);
%max=(max(c))
%im1=(c==8);
%imshow(im1)
%extraction
final=bwareaopen(A,floor((a1/15)*(b1/15)));
final(1:floor(.9*a1),1:2)=1;
final(a1:-1:(a1-20),b1:-1:(b1-2))=1;
yyy=template(2);
imshow(final)
Iprops=regionprops(final,'BoundingBox','Image');
hold on
for n=1:size(Iprops,1)
rectangle('Position',Iprops(n).BoundingBox,'EdgeColor','g','LineWidth',2);
end
hold off
NR=cat(1,Iprops.BoundingBox); %%Data storage section
[r ttb]=connn(NR);
if ~isempty(r)
xlow=floor(min(reshape(ttb(:,1),1,[])));
xhigh=ceil(max(reshape(ttb(:,1),1,[])));
xadd=ceil(ttb(size(ttb,1),3));
ylow=floor(min(reshape(ttb(:,2),1,[]))); %%%%%area selection
yadd=ceil(max(reshape(ttb(:,4),1,[])));
final1=A(ylow:(ylow+yadd+(floor(max(reshape(ttb(:,2),1,[])))-ylow)),xlow:(xhigh+xadd));
[a2 b2]=size(final1);
final1=bwareaopen(final1,floor((a2/20)*(b2/20)));
imshow(final1)
Iprops1=regionprops(final1,'BoundingBox','Image');
NR3=cat(1,Iprops1.BoundingBox);
I1={Iprops1.Image};
end
[h,w,f]=size(final1);
org=final1;
%segmetation
i=padarray(final1,[0 10]);
%segmetation
final=bwareaopen(i,floor((a1/15)*(b1/15)));
final(1:floor(.9*a1),1:2)=1;
final(a1:-1:(a1-20),b1:-1:(b1-2))=1;
yyy=template(2);
imshow(final)
Iprops=regionprops(final,'BoundingBox','Image');
hold on
for n=1:size(Iprops,1)
rectangle('Position',Iprops(n).BoundingBox,'EdgeColor','g','LineWidth',2);
end
hold off
NR=cat(1,Iprops.BoundingBox); %%Data storage section
[r ttb]=connn(NR);
if ~isempty(r)
xlow=floor(min(reshape(ttb(:,1),1,[])));
xhigh=ceil(max(reshape(ttb(:,1),1,[])));
xadd=ceil(ttb(size(ttb,1),3));
ylow=floor(min(reshape(ttb(:,2),1,[]))); %%%%%area selection
yadd=ceil(max(reshape(ttb(:,4),1,[])));
final1=i(ylow:(ylow+yadd+(floor(max(reshape(ttb(:,2),1,[])))-ylow)),xlow:(xhigh+xadd));
[a2 b2]=size(final1);
final1=bwareaopen(final1,floor((a2/20)*(b2/20)));
imshow(final1)
Iprops1=regionprops(final1,'BoundingBox','Image');
NR3=cat(1,Iprops1.BoundingBox);
I1={Iprops1.Image};
end
[h,w,f]=size(final1);
A=final1
verticalProjection = sum(i, 1);
subplot(2, 2, 1);imshow(i);
subplot(2,2,3);
plot(verticalProjection, 'b-');
grid on;
above is my code and i attached the Data0
Answers (1)
Image Analyst
on 25 Apr 2017
0 votes
Why not simply use a template and a lookup table? Just examine each segment of each character. Depending on which segments are dark or not, you can simply look up the number in the look up table. Make sure your images are aligned so that the template overlaps the character segments.
Categories
Find more on Process Point Clouds in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!