我试图把我的MATLAB代码转换成python,但是我遇到了一些问题。这段代码是用来从图片中分割字母的。在
以下是MATLAB中的全部代码:
he = imread('r.jpg');
imshow(he);
%C = makecform(type) creates the color transformation structure C that defines the color space conversion specified by type.
cform = makecform('srgb2lab');
%To perform the transformation, pass the color transformation structure as an argument to the applycform function.
lab_he = applycform(he,cform);
%convert to double precision
ab = double(lab_he(:,:,2:3));
%size of dimension in 2D array
nrows = size(ab,1);
ncols = size(ab,2);
%B = reshape(A,sz1,...,szN) reshapes A into a sz1-by-...-by-szN array where
%sz1,...,szN indicates the size of each dimension. You can specify a single
% dimension size of [] to have the dimension size automatically calculated,
% such that the number of elements in B matches the number of elements in A.
% For example, if A is a 10-by-10 matrix, then reshape(A,2,2,[]) reshapes
% the 100 elements of A into a 2-by-2-by-25 array.
ab = reshape(ab,nrows*ncols,2);
% repeat the clustering 3 times to avoid local minima
nColors = 3;
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = he;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
figure,imshow(segmented_images{1}), title('objects in cluster 1');
figure,imshow(segmented_images{2}), title('objects in cluster 2');
figure,imshow(segmented_images{3}), title('objects in cluster 3');
mean_cluster_value = mean(cluster_center,2);
[tmp, idx] = sort(mean_cluster_value);
blue_cluster_num = idx(1);
L = lab_he(:,:,1);
blue_idx = find(pixel_labels == blue_cluster_num);
L_blue = L(blue_idx);
is_light_blue = im2bw(L_blue,graythresh(L_blue));
% target_labels = repmat(uint8(0),[nrows ncols]);
% target_labels(blue_idx(is_light_blue==false)) = 1;
% target_labels = repmat(target_labels,[1 1 3]);
% blue_target = he;
% blue_target(target_labels ~= 1) = 0;
% figure,imshow(blue_target), title('blue');
到目前为止,我在Python中得到的结果如下:
^{pr2}$现在我要讨论的是,在Matlab代码中,我有lab_he(:,:,2:3),这意味着深度2和深度3的所有行和列。但是,当我试图在Python中复制img2a=img2.shape[2][1:2]时,它不起作用,也没有意义。请帮忙。在
在倍频程/MATLAB中
在
^{pr2}$numpy
中:或者使用numpy标准的“C”排序,并在第一维度上建立索引
相同的索引思想,虽然匹配数字和形状需要一些小心,而不仅仅是在0 v 1索引基础上。三维阵列以不同的排列方式显示。Octave在最后一个索引(它的主迭代器)上将它划分为块,numpy在第一个索引上迭代。在
在3d中,谈论第一、第二、第三维度比讨论行、列、深度更有意义。在4d里你的名字就用完了。:)
我必须在特定深度重塑数组,并编写了一个小递归函数来实现这一点:
它允许获取特定深度的最小值和最大值之间的所有数组/值,例如,如果您有一个(3,4)tab=[[0,1,2,3],[0,1,2,3],[0,1,2,3]]并且只需要最深数组的最后两个值,您可以这样调用:tab=recursive_array_cutting(tab,1,0,0,2)得到输出:[[0 1][0 1][0 1]]。在
如果你有一个更复杂的数组,像这样tab=[[[[[[0,1,2,2,3],[1,1,2,3],[2,1,2,3],[[0,1,2,2,3],[1,1,2,2,3],[2,1,2,3],[[0,1,1,2,3],[1,1,2,3,3],[2,1,1,2,3,3]]]]](3,3,4)并且想要一个(3,2,4)阵列阵列阵列(3,3,3,4)阵列,想要一个(3,2,3,4)阵列阵列阵列,您可以这样调用这样的方式:tab=递归[tab=递归[1,递归[1,1得到这个output,去掉深度1中的最后一个维度。在
这样的功能肯定存在于numpy中,但我没有找到它。在
相关问题 更多 >
编程相关推荐