[英]Searching Across a Line in a Matrix in Octave
The attached image has a line with a break in it.附加的图像有一条带中断的线。
My code finds the line using a hough transform resulting in r=32
and theta=2.3213
.我的代码使用霍夫变换找到了导致
r=32
和theta=2.3213
。 The hough transform isn't perfect, the angle (especially with a more complex image) is always off by a little bit, and in this case, because of the edge detection, the line is offset.霍夫变换并不完美,角度(尤其是更复杂的图像)总是有一点点偏离,在这种情况下,由于边缘检测,线被偏移。 I want to read values across the line to find the breaks in it.
我想跨行读取值以找到其中的中断。 In order to do this, I will need to be able to sample values on either side of the line to find where the maximum density of the line is.
为了做到这一点,我需要能够对线的任一侧的值进行采样,以找到线的最大密度所在的位置。
Further explanation (if you want it): If you look closely at the image you can see areas where the line crosses a pixel pretty much dead on resulting in a value of nearly 1/white.进一步的解释(如果你想要的话):如果你仔细观察图像,你可以看到线与像素交叉的区域几乎死了,导致值接近 1/白色。 Other areas have two pixels side by side with values of about .5/gray.
其他区域有两个像素并排,值约为 0.5/灰度。 I need to find a solution that takes into account the anti-aliasing of the line, and allows me to extract the breaks in it.
我需要找到一个解决方案,考虑到线条的抗锯齿,并允许我提取其中的中断。
%Program Preparation
clear ; close all; clc %clearing command window
pkg load image %loading image analyzation suite
pkg load optim
%Import Image
I_original = imread("C:/Users/3015799/Desktop/I.jpg");
%Process Image to make analysis quicker and more effective
I = mat2gray(I_original); %convert to black and white
I = edge(I, 'sobel');
%Perform Hough Transform
angles = pi*[-10:189]/180;
hough = houghtf(I,"line",angles);
%Detect hot spots in hough transform
detect = hough>.5*max(hough(:));
%Shrink hotspots to geometric center, and index
detect = bwmorph(detect,'shrink',inf);
[ii, jj] = find(detect);
r = ii - (size(hough,1)-1)/2;
theta = angles(jj);
%Cull duplicates. i.e outside of 0-180 degrees
dup = theta<-1e-6 | theta>=pi-1e-6;
r(dup) = [];
theta(dup) = [];
%Compute line parameters (using Octave's implicit singleton expansion)
r = r(:)'
theta = theta(:)'
x = repmat([1;1133],1,length(r)); % 2xN matrix, N==length(r)
y = (r - x.*cos(theta))./sin(theta); % solve line equation for y
%The above goes wrong when theta==0, fix that:
horizontal = theta < 1e-6;
x(:,horizontal) = r(horizontal);
y(:,horizontal) = [1;:];
%Plot
figure
imshow(I)
hold on
plot(y,x,'r-','linewidth',2)
If you are only interested in the length of the gap, this would be very easy:如果您只对间隙的长度感兴趣,这将非常简单:
clear all
pkg load image
img_fn = "input.jpg";
if (! exist (img_fn, "file"))
urlwrite ("https://i.stack.imgur.com/5UnpO.jpg", img_fn);
endif
Io = imread(img_fn);
I = im2bw (Io);
r = max(I);
c = max(I');
ri = find (diff(r));
ci = find (diff(c));
## both should have 4 elements (one break)
assert (numel (ri) == 4);
assert (numel (ci) == 4);
## the gap is in the middle
dx = diff(ri(2:3))
dy = diff(ci(2:3))
# the length is now easy
l = hypot (dy, dx)
gives给
dx = 5
dy = 5
l = 7.0711
without any hogh transform.没有任何 hogh 变换。 Of course you have to also check the corener cases for horizontal and vertical lines but this should give you an idea
当然,您还必须检查水平线和垂直线的核心案例,但这应该会给您一个想法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.