2017-06-12 58 views
1

我在Matlab(Version R 2016b)中有一個簡單的pcolor圖,我已經上傳了,如下圖所示。我只需要得到藍色斜線,它從最左角的中間延伸到最右角,而不用硬編碼矩陣值。如何基於矩陣值檢測圖像的一部分?

例如:人們可以看到,所需的斜率線具有值大約從令pColor情節之間某處。 (僅僅通過查看圖表就可以粗略估計)

我在包含繪圖值的matrix named Slant上應用以下代碼。

load('Slant.mat'); 
Slant(Slant<20|Slant>50)=0; 
pcolor(Slant); colormap(jet); shading interp; colorbar; 

正如人們所看到的,我硬編碼了我不想要的值。是否有任何方法檢測某些矩陣值,同時使其餘等於零?

我使用了另一種從矩陣中取一半最大值並將其設置爲零的小算法。但這不適用於其他圖像。

[maxvalue, row] = max(Slant); 
max_m=max(maxvalue); 
Slant(Slant>max_m/2)=0; 
pcolor(Slant); colormap(jet); shading interp; colorbar; 

Slope Image

+0

是矩陣非零隨處可見的圖像? – flawr

+0

矩陣在彩色區域中不爲零。除此之外,它的值爲零。 – Sack11

+0

爲什麼你用這麼大的矩陣有太多的條目,不添加任何東西?這條線有什麼特性可以區分它和其他部分?沒有任何關於你期望的結構和信息的知識,這似乎是不可能解決的。 – flawr

回答

1

這裏是另一個建議:

  1. 刪除所有的背景
  2. 假定在數據的雙峯分佈(除去零之後)這個「行」的結果,發現下模式。
  3. 假設線的值總是比背景下,應用邏輯掩碼設置爲零的最小+ 2nd_mode所有值以上,如在下面的圖中證實(在紅色圓圈):

line_hist

這裏是它如何工作的:

A = Slant(any(Slant,2),:); % save in A only the nonzero data 

現在我們有A,看起來像這樣:

A

[y,x] = findpeaks(histcounts(A)); % find all the mode in the histogram of A 
sorted_x = sortrows([x.' y.'],-2); % sort them by their hight in decendet order 
mA = A<min(A(:))+sorted_x(2,1); % mask all values above the second mode 
result = A.*mA; % apply the mask on A 

而我們得到的result

result


所得線裏面出現一些漏洞,所以你可能想從插值結果整條生產線。這可以用簡單的數學上做索引:

[y1,x1] = find(mA,1); % find the first nonzero row 
[y2,x2] = find(mA,1,'last'); % find the last nonzero row 
m = (y1-y2)/(x1-x2); % the line slope 
n = y1-m*x1; % the intercept 
f_line = @(x) m.*x+n; % the line function 

所以我們得到了一個線功能f_line像這樣(在下面紅色):

interp_line

現在,我們想使這條線較粗,就像數據中的直線一樣,所以我們採用厚度模式(通過計算每列中的值,您可能需要改爲max),並將該線的「一半」擴展到兩邊:

thick = mode(sum(mA)); % mode thickness of the line 
tmp = (1:thick)-ceil(thick/2); % helper vector for expanding 
rows = bsxfun(@plus,tmp.',floor(f_line(1:size(A,2)))); % all the rows for each coloumn 
rows(rows<1) = 1; % make sure to not get out of range 
rows(rows>size(A,1)) = size(A,1); % make sure to not get out of range 
inds = sub2ind(size(A),rows,repmat(1:size(A,2),thick,1)); % convert to linear indecies 
mA(inds) = 1; % add the interpolation to the mask 
result = A.*mA; % apply the mask on A 

現在result看起來是這樣的:

inter and thick

+0

@ EBH和@ flawr。兩個答案都非常有幫助我用兩個答案的組合來實現曲線的結果。並且對這個概念有很好的解釋。 – Sack11

1

理念:使用Hough變換:

首先最好是創建一個新的矩陣,只有行和列,我們感興趣的是

在爲了應用hough中構建的matlab,我們必須創建一個二進制圖像:由於該行的值始終比其他值低,因此我們可以確定出現在畫面亮度的最低四分位數(使用quantile,而這些設置爲白色,一切爲黑色。

然後找到行,我們可以直接對BW圖像使用hough

+0

分位數方法證明對於曲線非常有用。 – Sack11