Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image Matlab在保持三维不变的情况下,用线性标记索引二维_Image_Matlab_Matrix_Indexing - Fatal编程技术网

Image Matlab在保持三维不变的情况下,用线性标记索引二维

Image Matlab在保持三维不变的情况下,用线性标记索引二维,image,matlab,matrix,indexing,Image,Matlab,Matrix,Indexing,假设我有2D线性标记: linInd = sub2ind(imSize,rowPnts,colPnts); 我有一个3D彩色图像我: I = rand(64,64,3)*255 有没有什么方法可以为这样的东西建立索引,以便获得2D平面中的所有坐标,但图像的每个通道都是这样的?也就是说,我可以使用为2D指定的线性标记,通过一个命令获得每个像素的所有颜色通道信息吗 I(linInd,:) 所以我不必把图像分成3部分,然后重新组装 谢谢。据我所知,首先重塑矩阵是以这种方式使用线性索引的唯一方法

假设我有2D线性标记:

linInd = sub2ind(imSize,rowPnts,colPnts);
我有一个3D彩色图像我:

I = rand(64,64,3)*255
有没有什么方法可以为这样的东西建立索引,以便获得2D平面中的所有坐标,但图像的每个通道都是这样的?也就是说,我可以使用为2D指定的线性标记,通过一个命令获得每个像素的所有颜色通道信息吗

I(linInd,:)
所以我不必把图像分成3部分,然后重新组装


谢谢。

据我所知,首先重塑矩阵是以这种方式使用线性索引的唯一方法

I2=reshape(I,[],3)
I2(ind,:)
您可以使用
2D
线性索引到
3D
案例,而不干扰输入数组,如下所示-

[m,n,r] = size(I);
out = I(bsxfun(@plus,linInd,(m*n*(0:r-1))'))

样本设置

%// ---------------- 2D Case ---------------------
im = randi(9,10,10);
imSize = size(im);

rowPnts = [3,6,8,4];
colPnts = [6,3,8,5];
linInd = sub2ind(imSize,rowPnts,colPnts);

%// ---------------- 3D Case ---------------------
I = randi(9,10,10,4);

%// BSXFUN solution
[m,n,r] = size(I);
out = I(bsxfun(@plus,linInd,(m*n*(0:r-1))')); %//'

%// Tedious work of splitting
Ir = I(:,:,1);
Ig = I(:,:,2);
Ib = I(:,:,3);
Ia = I(:,:,4);
输出

>> Ir(linInd)
ans =
     8     9     1     6
>> Ig(linInd)
ans =
     1     5     9     8
>> Ib(linInd)
ans =
     8     5     3     8
>> Ia(linInd)
ans =
     8     8     3     3
>> out
out =
     8     9     1     6
     1     5     9     8
     8     5     3     8
     8     8     3     3

你是否特别需要将这个维度保持为第三维度?如果没有,您可以
排列
数组以将该维度移动到第一个位置,然后使用
arr(i3d,linInd)