[英]Efficiency of the script (finding a pair of integers which have the same remainder)
[英]Finding items in an image which have the same intensity
如何在具有3維的圖像中找到所有點(或區域),其中前兩個維顯示分辨率,第三個維顯示密度? 我可以使用Matlab或Python。 我想知道是否存在本機函數來查找那些計算成本最低的點。 更新:假設我有以下內容:
A= [1,2,3; 4,6,6; 7,6,6]
A =
1 2 3
4 6 6
7 6 6
>> B=[7,8,9; 10,11,11; 1, 11,11]
B =
7 8 9
10 11 11
1 11 11
>> C=[0,1,2; 3, 7, 7; 5,7,7]
C =
0 1 2
3 7 7
5 7 7
如何找到A的所有值都等於B的所有值和C的所有值的下平方? 如果太多,我如何才能找到A中所有值都相等的A的下平方? *顯示的值是圖像的強度。
更新:嘗試提供的答案並得到以下錯誤:
>> c=conv2(M,T, 'full');
Warning: CONV2 on values of class UINT8 is obsolete.
Use CONV2(DOUBLE(A),DOUBLE(B)) or CONV2(SINGLE(A),SINGLE(B)) instead.
> In uint8/conv2 (line 10)
Undefined function 'conv2' for input arguments of type 'double' and attributes 'full 3d real'.
Error in uint8/conv2 (line 17)
y = conv2(varargin{:});
*還嘗試了convn
,它花了很convn
,所以我才停止了! 基本上如上所述,如何對2D陣列執行此操作?
您可以使用一對水平和垂直一維濾鏡,以便水平濾鏡的內核為[1 -1]
而垂直濾鏡的內核為[1; -1]
[1; -1]
。 這樣做的效果是,每個維度中的每個元素分別需要水平和垂直成對距離。 然后,您可以使用這兩個內核執行圖像過濾或卷積,以確保復制邊界。 為了能夠找到均勻的區域,通過檢查兩個結果中的哪個區域在兩個區域之間映射為0,可以為您提供在所有通道上獨立的區域。
為此,您首先要對兩個過濾結果進行相反的處理 ,以使將變為0的均勻區域現在為1,反之亦然。 您必須對這兩個元素同時執行logical
AND操作,然后確保對於每個像素在時間上所有值均為true
。 這意味着對於此圖像中的空間位置,所有值都將達到您期望的相同均勻性。
在MATLAB中,假定您具有“圖像處理工具箱”,請使用imfilter
過濾圖像,然后在MATLAB中使用all
來臨時查看這兩個過濾結果,然后使用regionprops
查找所需區域的坐標。 所以做這樣的事情:
%# Reproducing your data
A = [1,2,3; 4,6,6; 7,6,6];
B = [7,8,9; 10,11,11; 1, 11,11];
C = [0,1,2; 3, 7, 7; 5,7,7];
%# Create a 3D matrix to allow for efficient filtering
D = cat(3, A, B, C);
%# Filter using the kernels
ker = [1 -1];
ker2 = ker.'; %#
out = imfilter(D, ker, 'replicate');
out2 = imfilter(D, ker2, 'replicate');
%# Find uniform regions
regions = all(~out & ~out2, 3);
%# Determine the locations of the uniform areas
R = regionprops(regions, 'BoundingBox');
%# Round to ensure pixel accuracy and reshape into a matrix
coords = round(reshape([R.BoundingBox], 4, [])).';
coords
是一個N x 4
矩陣,每行告訴邊界框原點的左上坐標以及邊界框的寬度和高度。 行中的第一和第二元素是列和行的坐標,而第三和第四元素是邊界框的寬度和高度。
我們檢測到的區域可以在regions
變量中找到。 這兩個都顯示:
>> regions
regions =
3×3 logical array
0 0 0
0 1 1
0 1 1
>> coords
coords =
2 2 2 2
這告訴我們,我們已經將“均勻性”區域定位為右下角,而邊界框的左上角的坐標是第2行,第2列,其寬度和高度分別為2和2。
可能的解決方案:
A = [1,2,3; 4,6,6; 7,6,6];
B = [7,8,9; 10,11,11; 1, 11,11];
C = [0,1,2; 3, 7, 7; 5,7,7];
%create a 3D array
D = cat(3,A,B,C)
%reshape the 3D array to 2D
%its columns represent the third dimension
%and its rows represent resolution
E = reshape(D,[],size(D,3));
%third output of the unique function applied row-wise to the data
%represents the label of each pixel a [m*n, 1] vector created
[~,~,F] = unique(E,'rows');
%reshape the vector to a [m, n] matrix of labels
result = reshape(F, size(D,1), size(D,2));
您可以將3D矩陣整形為2D矩陣( E
),其列表示三維尺寸,其行表示分辨率。
然后,使用unique
功能可以標記圖像。
我們有一個3D矩陣:
A =
1 2 3
4 6 6
7 6 6
B =
7 8 9
10 11 11
1 11 11
C =
0 1 2
3 7 7
5 7 7
當我們將3D矩陣整形為2D矩陣E
我們得到:
E =
1 7 0
4 10 3
7 1 5
2 8 1
6 11 7
6 11 7
3 9 2
6 11 7
6 11 7
因此,我們需要根據行的值對行進行分類。
唯一功能能夠提取唯一的行,並將相同的標簽分配給彼此相等的行。
此處,變量F
捕獲唯一功能的第三條輸出,即每行的標簽。
F =
1
4
6
2
5
5
3
5
5
應該重塑為2D
結果=
1 2 3
4 5 5
6 5 5
因此每個區域都有不同的標簽。
如果要分割不同的區域(基於它們的值和空間位置),則需要在循環中標記圖像
numcolors = max(F);
N = 0;
segment = zeros(size(result));
for c = 1 : numcolors
[label,n] = bwlabel(result==c);
segment = segment +label + logical(label)*N;
N = N + n;
end
因此,這里需要用不同的標簽標記具有相同值的斷開連接的區域。 由於MATLAB沒有灰色分割功能,因此可以多次使用bwlabel
函數進行分割,並將上一次迭代的結果添加到當前迭代的結果中。 segment
變量包含分割的圖像。
*注意:此結果是從GNU Octave獲得的,其標簽不同於MATLAB。 如果您使用unique(E,'rows','last');
MATLAB和Octave的結果相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.