wildcat
wildcat

Reputation: 285

Matlab image processing build region connections

I am using MATLAB for image processing and have images with segmented regions. Here is an example image: http://www.mathworks.de/help/releases/R2013b/images/examples/ipexroundness_04.png

How can I find the minimum distance from one region to the closest neighbour region? I don't need full implementation but can anyone refer an algorithm which finds the closest neighbour regions and caluclates the minimum distance and the closest points of the region.

I want to use this information to connect the image regions, i.e. build bridges between image regions.

Upvotes: 0

Views: 288

Answers (1)

Notlikethat
Notlikethat

Reputation: 20984

Off the top of my head, the quick-and-dirty nested loop approach:

For region ii=1:n, run the mask of region ii through bwdist to generate a distance transform. For regions jj=1:n, use the mask of region jj to index into that distance transform. That gets you the distances of every pixel in region jj from their closest counterpart in region ii - find the minimum value (and its coordinates) and stuff it in some kind of pairwise distance matrix. Repeat until done, then process the pairwise distance matrix to work out which regions you want to connect.

Edit: Having got that far, I figured I might as well knock something together - here's a rough version that takes a binary image and returns the distances, x and y coordinates of the potential connection points as pairwise matrices:

function [d x y] = regiondist(img)
label = bwlabel(img);
n = max(label(:));
[x y d] = deal(zeros(n));
for ii = 1:n
  dt = bwdist(label == ii);
  for jj = 1:n
    if ii == jj
      continue
    end
    reg = (label == jj);
    [mindist idx] = min(dt(reg));
    d(ii, jj) = mindist;
    [ry rx] = find(reg);
    x(ii, jj) = rx(idx);
    y(ii, jj) = ry(idx);
  end
end

Upvotes: 1

Related Questions