这是解决上述问题的方法。 嗯,我仍然在这里使用一个for循环,但我真的想不出任何其他方式来获得所需的结果。
下面是代码:
I = imread('Image _0_77.jpg');
I_dub = double(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;
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循环。