How can I find the location of the pixels between adjacent regions.

soe (view profile)

on 23 Nov 2012

Dear all, I segmented an image using watershed and got the Label matrix. I've already found the neighbors of each region using graycomatrix. My problem is that I would like to find the location of the pixels between each pair of adjacent regions. Using bwboundaries and bwperim give all the pixel locations of each region. I just want to get the locations of the pixels situated between each adjacent regions. Thanks

Products

No products are associated with this question.

Image Analyst (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 23 Nov 2012

As you can imagine, there will be some ambiguities in some places, like when three regions meet. However, that will be just at a few pixels. I'd recommend doing two for loops to find where every region overlaps every other region. Then dilate each region and AND them to find the overlap pixels. Run this demo:

```clc;	% Clear command window.
clear;	% Delete all variables.
close all;	% Close all figure windows except those created by imtool.
imtool close all;	% Close all figure windows created by imtool.
workspace;	% Make sure the workspace panel is showing.
fontSize = 15;
```
```% 1.
% Make a binary image containing two overlapping circular objects.
center1 = -10;
center2 = -center1;
dist = sqrt(2*(2*center1)^2);
radius = dist/2 * 1.4;
[x,y] = meshgrid(lims(1):lims(2));
bw1 = sqrt((x-center1).^2 + (y-center1).^2) <= radius;
bw2 = sqrt((x-center2).^2 + (y-center2).^2) <= radius;
bw = bw1 | bw2;
figure;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off');
subplot(2, 2, 1);
imshow(bw,'InitialMagnification','fit');
title('bw', 'FontSize', fontSize);
```
```%  2.
% Compute the distance transform of the complement of the binary image.
D = bwdist(~bw);
subplot(2, 2, 2);
imshow(D,[],'InitialMagnification','fit')
title('Distance transform of ~bw', 'FontSize', fontSize)
% 3.
% Complement the distance transform, and force pixels that don't belong to the objects to be at -Inf.
D = -D;
D(~bw) = -Inf;
% 4.
% Compute the watershed transform and display the resulting label matrix as an RGB images.
L = watershed(D);
rgb = label2rgb(L,'jet',[.5 .5 .5]);
subplot(2, 2, 3);
imshow(rgb,'InitialMagnification','fit')
title('Watershed transform of D', 'FontSize', fontSize)
```
```% Code above was from the watershed demo, with some changes by Image Analyst.
% Code below is from Image Analyst.
numberOfRegions = max(L(:));
figure;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
```
```for r1 = 1 : numberOfRegions
% Get the region
region1 = ismember(L, r1);
% Dilate it.
region1Dilated = imdilate(region1, true(3));
for r2 = r1 + 1 : numberOfRegions
% Get the region
region2 = ismember(L, r2);
% Dilate it.
region2Dilated = imdilate(region2, true(3));
% Find the intersection
intersectionRegions = region1Dilated & region2Dilated;

% Display everything.
subplot(2, 3, 1);
imshow(region1);
title('Region1', 'FontSize', fontSize);
subplot(2, 3, 4);
imshow(region1Dilated);
title('Region1 Dilated', 'FontSize', fontSize);
subplot(2, 3, 2);
imshow(region2);
title('Region2', 'FontSize', fontSize);
subplot(2, 3, 5);
imshow(region2Dilated);
title('Region2 Dilated', 'FontSize', fontSize);
subplot(2, 3, 6);
imshow(intersectionRegions);
title('intersectionRegions', 'FontSize', fontSize);
end
end

```

Answer by Matt J

on 23 Nov 2012
Edited by Matt J

Matt J (view profile)

on 23 Nov 2012

Use intersect(...,'rows') on the data returned from bwboundaries to get the intersection of the sets of boundary pixels for 2 regions.