how t oget straight edge in binary image?

1 view (last 30 days)
how t
o get straight edge in binary image?

Accepted Answer

John BG
John BG on 27 Jul 2017
Edited: John BG on 29 Jul 2017
Hi Selva
please find attached script to straighten the small corrugations caused by small amount of pixels
1.
acquiring image
clear all; close all;
A=imread('031.jpg');
figure(1);imshow(A)
2.
amplifying image
d1=10
B2=imresize(A,d1);
figure(2);imshow(B2)
3.
binaryzing
B2=B2(:,:,1);
B3=imbinarize(B2);
figure(3);imshow(B3)
4.
capturing contour
B4=del2(double(B3));
figure(4);imshow(B4)
[x,y,v]=find(B4); % capturing contour points
5.
directly amplifying and reducing with different pairs of factors doesn't work
% B5=imresize(B4,1/(.8*d)); % doesn't work
% figure(5);imshow(B5)
6.
checking the points to process are all contour and only contour points
figure(4);hold all;plot(y,x,'g*') % check
d2=20;
x2=x([1:d2*d1:end]);y2=y([1:d2*d1:end]);
figure(4);hold all;plot(y2,x2,'r*') % check
P2=[x2,y2]'
Now all points are available in P2
7.
there is a problem encountered when attempting to directly apply golay or spline directly as explained in the following links respectively:
IA example has 2 wonderful scripts using spline and golay polynomials interpolation
.
such scripts assume points acquired consecutively, but it doesn't happen with all points of interest in this question
Following some of the curves obtained when attempting to directly apply spline or golay as shown in the link above
Adjacent points in variable to process are not necessarily nearest points measuring euclidean distance.
% NOT USED
% comment spline
% originalSpacing=[1:numel(x2)]; % spline directly doesn't work, doesn't work
% d3=.2;
% finerSpacing=[1:d3:numel(x)];
% spXY=spline(originalSpacing,P2,finerSpacing);
% figure(4);plot(spXY(2,:),spXY(1,:),'r')
% zig zag caused by non consecutive ordering of points
% weuc = @(XI,XJ)(sqrt(bsxfun(@minus,XI,XJ).^2));
% D = pdist2(P2(1,:),P2(2,:), @(Xi,Xj) weuc(Xi,Xj));
% D=((P2(1,:)'-P2(1,:)).^2+(P2(2,:)'-P2(2,:)).^2).^.5; % calculating distances, no need for N*N matrix
8.
Rearranging points according to proximity
P2c=P2;
s=1;p=1;
P3=P2c(:,s)
figure(4);plot(P3(2,end),P3(1,end),'bo');plot(P3(2,end),P3(1,end),'b*') % used to track points
while length(P3)<length(P2)
D=zeros(1,length(P2c));
for s1=1:1:length(P2c)
D(s1)=((P2c(1,s1)-P3(1,end))^2+(P2c(2,s1)-P3(2,end))^2)^.5;
end
DL=D(s,:) % read pth line of distances matrix
p=find(DL==min(nonzeros(DL))) % find location of nearest element number p
P3=[P3 P2c(:,p)] % append found element to result chain
if p==2 % if next point is nearest point
P2c(:,s)=[] % remove previous point from input list of points
end
if p>2 % if nearest point further ahead than next point
P2c(:,s)=[]
LS=P2c(:,p-1)
P2c(:,p-1)=[]
P2c=[LS P2c] % bring forward nearest point
end
end
figure(4);plot(P3(2,:),P3(1,:),'y','LineWidth',2)
figure(5);imshow(B4)
hold all; plot(P3(2,:),P3(1,:),'y','LineWidth',.7) % d1=3 d2=20 d3=0.2
Now, without spline or higher order polynomials needed, just plot, it already shows straight edges.
d1=3 d2=20
.
.
d1=10 d2=20
.
.
Now one can use spline or golay polynomials interpolation, once the points have been ordered according to proximity.
Selva
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  3 Comments
Selva Karna
Selva Karna on 31 Jul 2017
it is not working clearly, i get loss in original image edges?
John BG
John BG on 31 Jul 2017
Edited: John BG on 31 Jul 2017
Hi Selva
it's good you mention this because the bottom down corners of the original image have 2 disconnected short chunks of edge,
.
that it would greatly help those short edges to either be attached to the main edge,
.
or to ignore them, does it make sense?
Please note that the left side of the image has a long vertical edge that again, should be considered connected to the main line, and the bottom left corner short chunk, or ignore it.
When considering the long left side of the image,there is a sharp turn of about 315º
that may be ok after applying the script, but it may round the tip when attempting interpolation with Golay polynomials.
The script works ok with the attached image 031.jpg, which is the image you supplied in the question, without the bottom left right corner short bits.
John BG

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!