【问题标题】:Find closest point of labelled area to a point in an image with Matlab使用Matlab查找标记区域与图像中点的最近点
【发布时间】:2017-05-18 17:44:32
【问题描述】:

我正在尝试使用 Matlab 找到图像中某个区域的关闭点:

考虑这个示例代码:

myimg = rgb2gray(imread('tissue.png')); %load grayscale example image of cells
BW=bwareaopen(myimg<150,10); %only look at dark pixels, remove small objects
BW=bwlabel(imfill(BW,'holes')) %fill holes of structures, label structures
figure; 
imagesc(BW); %display image

我想找到离某个点最近的结构的最近点,例如[0,0]。到目前为止,我的方法是获取所有连接结构的所有质心,然后遍历所有结构以找到最接近的结构(不准确且效率低下)。

【问题讨论】:

    标签: matlab image-segmentation nearest-neighbor


    【解决方案1】:

    如果您只想找到一个最近的点,您可以使用 bwdist 和第二个输出参数。这将为您提供一个矩阵,该矩阵在每个点都包含输入图像最近的非零点的线性索引。然后,您只需要选择与您感兴趣的点相对应的索引。bwdist 的输入图像应该是二进制的,所以在您的情况下,您可以尝试类似

    % Make the image binary
    binaryimage = BW > 0;
    
    % Get the linear indices of the closest points
    [~, idx] = bwdist(binaryimage);
    
    % Find the linear index for point (3, 2)
    x = 3;
    y = 2;
    pointindex = sub2ind(size(binaryimage), y, x);
    
    % Find the index of the closet point from the segmentation
    closestpointindex = idx(pointindex);
    
    % Get coordinates of the closest point
    [yc, xc] = ind2sub(size(binaryimage), closestpointindex);
    

    这会给你坐标(xc, yc)和矩阵索引(closestpointindex)在二进制图像中最接近点(x,y)的非零值像素,其中x和@ 987654328@ 是 Matlab 索引,记住 Matlab 索引从 1 开始,行在前,即 BW(y,x)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 2016-07-01
      相关资源
      最近更新 更多