簡體   English   中英

在八度矩陣中跨線搜索

[英]Searching Across a Line in a Matrix in Octave

附加的圖像有一條帶中斷的線。

圖片

我的代碼使用霍夫變換找到了導致r=32theta=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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM