【问题标题】:Searching Across a Line in a Matrix in Octave在 Octave 中搜索矩阵中的一条线
【发布时间】:2018-02-09 21:20:25
【问题描述】:

附加的图像有一条线,其中有一个中断。

我的代码使用导致r=32theta=2.3213 的霍夫变换找到该行。霍夫变换并不完美,角度(尤其是更复杂的图像)总是偏离一点点,在这种情况下,由于边缘检测,线是偏移的。我想跨行读取值以找到其中的中断。为了做到这一点,我需要能够对线条两侧的值进行采样,以找出线条的最大密度在哪里。

进一步的解释(如果你想要的话): 如果您仔细观察图像,您会看到线条与像素相交的区域几乎完全死角,导致值接近 1/white。其他区域有两个并排的像素,其值约为 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)

【问题讨论】:

  • 停止删除和重复同样的问题。您上一个问题→stackoverflow.com/q/48708998
  • 你知道这个突破有多大吗?我会尝试扩张和侵蚀以缩小差距,然后找到它的确切位置和进一步的过程
  • 图片中是否只有两行(一行有断线),以便您可以找到这两行并从那里开始工作?

标签: matlab octave hough-transform


【解决方案1】:

如果您只对间隙的长度感兴趣,这将非常容易:

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 变换。当然,您还必须检查核心盒的水平线和垂直线,但这应该会给您一个想法

【讨论】:

    猜你喜欢
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多