[英]Matlab/Octave: how to display a matrix while specifying grid line spacing?
[英]Searching Across a Line in a Matrix in Octave
附加的图像有一条带中断的线。
我的代码使用霍夫变换找到了导致r=32
和theta=2.3213
。 霍夫变换并不完美,角度(尤其是更复杂的图像)总是有一点点偏离,在这种情况下,由于边缘检测,线被偏移。 我想跨行读取值以找到其中的中断。 为了做到这一点,我需要能够对线的任一侧的值进行采样,以找到线的最大密度所在的位置。
进一步的解释(如果你想要的话):如果你仔细观察图像,你可以看到线与像素交叉的区域几乎死了,导致值接近 1/白色。 其他区域有两个像素并排,值约为 0.5/灰度。 我需要找到一个解决方案,考虑到线条的抗锯齿,并允许我提取其中的中断。
%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)
如果您只对间隙的长度感兴趣,这将非常简单:
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)
给
dx = 5
dy = 5
l = 7.0711
没有任何 hogh 变换。 当然,您还必须检查水平线和垂直线的核心案例,但这应该会给您一个想法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.