2015-11-02 69 views
-2

我正试图实现一个算法。它需要我计算矢量化的补丁pi和pj之间的欧氏距离。 我正在使用MATLAB。MATLAB:向量化补丁之间的欧几里得距离

这是我的代码:

I = imread('Image\0\0_0_77.jpg'); 
I_dub = double(I); 
[rows,cols,dims] = size(I); 
C_LAB = makecform('srgb2lab'); 
I_LAB = applycform(I_dub,C_LAB); 

window_size = 7; 
rows_new = rows; 
cols_new = cols; 
while mod(rows_new,window_size) ~= 0 
    rows_new = rows_new + 1; 
end 
while mod(cols_new,window_size) ~= 0 
    cols_new = cols_new + 1; 
end 

diff_rows = rows_new - rows; 
diff_cols = cols_new - cols; 

I_new = padarray(I_LAB,[diff_rows,diff_cols],'post'); 
x_dir_pos = 1:window_size:rows; 
y_dir_pos = 1:window_size:cols; 

[x_index,y_index] = ndgrid(x_dir_pos,y_dir_pos); 
I_cells(:,:,1) = arrayfun(@(x,y) I_new(x:x+window_size-1,y:y+window_size-1,1),x_index,y_index,'un',0); 
I_cells(:,:,2) = arrayfun(@(x,y) I_new(x:x+window_size-1,y:y+window_size-1,2),x_index,y_index,'un',0); 
I_cells(:,:,3) = arrayfun(@(x,y) I_new(x:x+window_size-1,y:y+window_size-1,3),x_index,y_index,'un',0); 

现在我想计算将所有其他细胞等(1)在I_cells细胞之间的差异。

有没有办法做到这一点,而不使用for循环。

请帮忙! 预先感谢您。

+0

你的意思是矢量图像补丁?这是以前的任何相关性的问题吗? http://stackoverflow.com/questions/30549445/how-to-calculate-variance-of-an-image-patch-with-vectorization – paisanco

+0

@paisanco嗨。感谢您分享链接!但是,不,我不在寻找什么。我试图实现这篇文章“上下文感知显着性检测”第3页:第3.1节:我想计算d_color – Nachiket

+0

您尝试链接:mathworks.com/help/stats/linkage.html例如,作为“Z =链接(inputdata”, '病房', '欧氏')' – horseshoe

回答

0

这是解决上述问题的方法。 嗯,我仍然在这里使用一个for循环,但我真的想不出任何其他方式来获得所需的结果。

下面是代码:

I = imread('Image\0\0_0_77.jpg'); 
I_dub = double(I); 
[rows,cols,dims] = size(I); 
C_LAB = makecform('srgb2lab'); 
I_LAB = applycform(I_dub,C_LAB); 
max_I_LAB = max(max(I_LAB)); 
I_LAB(:,:,1) = I_LAB(:,:,1)/max_I_LAB(1); 
I_LAB(:,:,2) = I_LAB(:,:,2)/max_I_LAB(2); 
I_LAB(:,:,3) = I_LAB(:,:,3)/max_I_LAB(3); 

diff_rows = rows_new - rows; 
diff_cols = cols_new - cols; 

I_new = padarray(I_LAB,[diff_rows,diff_cols],'post'); 
x_dir_pos = 1:window_size:rows; 
y_dir_pos = 1:window_size:cols; 

[x_index,y_index] = ndgrid(x_dir_pos,y_dir_pos); 
I_cells(:,:,1) = arrayfun(@(x,y) I_new(x:x+window_size-1,y:y+window_size-1,1),x_index,y_index,'un',0); 
I_cells(:,:,2) = arrayfun(@(x,y) I_new(x:x+window_size-1,y:y+window_size-1,2),x_index,y_index,'un',0); 
I_cells(:,:,3) = arrayfun(@(x,y) I_new(x:x+window_size-1,y:y+window_size-1,3),x_index,y_index,'un',0); 

no_elements = size(I_cells,1) * size(I_cells,2); 
I_cells_vec = cell2mat([reshape(I_cells(:,:,1),[no_elements 1]) reshape(I_cells(:,:,2),[no_elements 1]) reshape(I_cells(:,:,3),[no_elements 1])]); 
I_cells_pos = [x_index(:) y_index(:)]; 

d_color = zeros(no_elements,no_elements); 
d_position = zeros(no_elements,no_elements); 
for i = 1:no_elements 
    indices = (i-1)*window_size + 1:i*window_size; 
    first_window = repmat(I_cells_vec(indices',:),no_elements,1); 
    first_position = repmat(I_cells_pos(i,:),no_elements,1); 
    d_color(i,:) = sqrt(sum(reshape(sum((first_window -  I_cells_vec).^2,2),window_size,[]),1)); 
    d_position(i,:) = sqrt(sum((first_position - I_cells_pos).^2,2)); 
end 

请让我知道是否有某种方式通过使用一些MATLAB函数来代替for循环。

相关问题