Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
Matlab数组作为矩阵的索引_Matlab_Image Processing_Matrix_Indexing_Vectorization - Fatal编程技术网

Matlab数组作为矩阵的索引

Matlab数组作为矩阵的索引,matlab,image-processing,matrix,indexing,vectorization,Matlab,Image Processing,Matrix,Indexing,Vectorization,我有一个图像和一个像素列表,其中有几个大对象是用regionprops定义的。现在我想生成一个新的图像,其中只包含一个对象,该对象的大小调整为对象的大小。所以我要做的是 rawimage = imread('testfile.tif'); processimage = squeeze((sum(rawimage,3))'); IMAGE_labeled = bwlabel(processimage,8); shapedata=regionprops (IMAGE_labeled,'Area','

我有一个图像和一个像素列表,其中有几个大对象是用regionprops定义的。现在我想生成一个新的图像,其中只包含一个对象,该对象的大小调整为对象的大小。所以我要做的是

rawimage = imread('testfile.tif');
processimage = squeeze((sum(rawimage,3))');
IMAGE_labeled = bwlabel(processimage,8);
shapedata=regionprops (IMAGE_labeled,'Area','PixelList');
index = find ([shapedata.Area]>5000);

for i = 1:length(index)
   ix = shapedata(index(i)).PixelList(:,1);
   iy = shapedata(index(i)).PixelList(:,2);
   newimage = zeros(max(ix)-min(ix)+1,max(iy)-min(iy)+1,3);
   for im = 1:length(ix)
      newimage(ix(im)-min(ix)+1,iy(im)-min(iy)+1,:) =   rawimage(ix(im),iy(im),:);
   end
   newimage = uint8(newimage);
   imwrite(newimage,'myimage.tif','tif')
end
有人知道我如何将第二个for循环矢量化以加速整个代码吗?最后的问题是如何在矩阵中使用两个向量作为索引

我发现

Vc = num2cell([ix,iy])
rawimage(sub2ind(size(rawimage),Vc{i,:}))
但这同样需要一个for循环来遍历Vc的所有索引,因为我不能使用它

rawimage(sub2ind(size(rawimage),Vc))  

谢谢你的建议

因为你的图像是三维的,你需要先把它重塑成一个
[M*N x 3]
阵列。然后,如图所示,您可以使用
sub2ind

for k = 1:numel(shapedata)
    % Flip the PixelList so it's row/column rather than x/y
    list = fliplr(shapedata(k).PixelList);

    % Figure out the extents of the PixelList values
    minima = min(list, [], 1);
    maxima = max(list, [], 1);

    % Grab a square of the image containing the object of interest
    subimage = rawimage(minima(1):maxima(1), minima(2):maxima(2),:);

    % Store the size
    sz = size(subimage);

    % Convert the PixelList values to linear indices within this sub-image
    inds = sub2ind(sz(1:2), bsxfun(@minus, list, minima - 1));

    % Reshape so that we can index into the first two dimensions at the same time
    subimage = reshape(subimage, [], size(subimage, 3));

    % Now create the output which is all zeros, but then we grab the values that are
    % in PixelList and set them within the new matrix.
    newimage = zeros(size(subimage));
    newimage(inds,:) = subimage(inds,:);

    % Reshape the result to be the expected dimension
    newimage = reshape(newimage, sz);
end

因为你的图像是三维的,所以你需要先把它重塑成一个
[M*N x 3]
数组。然后,如图所示,您可以使用
sub2ind

for k = 1:numel(shapedata)
    % Flip the PixelList so it's row/column rather than x/y
    list = fliplr(shapedata(k).PixelList);

    % Figure out the extents of the PixelList values
    minima = min(list, [], 1);
    maxima = max(list, [], 1);

    % Grab a square of the image containing the object of interest
    subimage = rawimage(minima(1):maxima(1), minima(2):maxima(2),:);

    % Store the size
    sz = size(subimage);

    % Convert the PixelList values to linear indices within this sub-image
    inds = sub2ind(sz(1:2), bsxfun(@minus, list, minima - 1));

    % Reshape so that we can index into the first two dimensions at the same time
    subimage = reshape(subimage, [], size(subimage, 3));

    % Now create the output which is all zeros, but then we grab the values that are
    % in PixelList and set them within the new matrix.
    newimage = zeros(size(subimage));
    newimage(inds,:) = subimage(inds,:);

    % Reshape the result to be the expected dimension
    newimage = reshape(newimage, sz);
end

Thx那更快。我遇到的问题是,如果我使用长方体,某些对象会重叠。所以一个对象的一部分会出现在另一个对象的新图像中…@horseshoe哦,对不起,我一开始读错了你的代码。我会更新。@horseshoe Updated.jay这就是我一直在搜索的内容。伟大的Thx那更快。我遇到的问题是,如果我使用长方体,某些对象会重叠。所以一个对象的一部分会出现在另一个对象的新图像中…@horseshoe哦,对不起,我一开始读错了你的代码。我会更新。@horseshoe Updated.jay这就是我一直在搜索的内容。伟大的