Clear Filters
Clear Filters

Finding the neighbouring regions

9 views (last 30 days)
Akhil
Akhil on 1 Jan 2024
Commented: Akhil on 4 Jan 2024
How I can find the neighbouring regions of each region or how to find the region sharing commn boundary

Answers (2)

Image Analyst
Image Analyst on 1 Jan 2024
Not too hard. Simply call bwlabel to give each region an ID. Then call bwmorph with the thicken option to dilate the regions to get rid of the black border between regions. Then use the gray level cooccurrence matrix, graycomatrix, to discover which regions are next to each other. I'm attaching a demo of graycomatrix. Adapt as needed.
  6 Comments
Image Analyst
Image Analyst on 3 Jan 2024
It's not working. The JPG corruption and artifacts are cluttering up the black dividing lines. Can you save it as PNG format?
Akhil
Akhil on 4 Jan 2024
Kindly find the image in the attachment.

Sign in to comment.


Hassaan
Hassaan on 1 Jan 2024
% Assuming 'binaryImage' is your binary segmented image, where each object
% is separated by black borders.
% Step 1: Label each region with a unique identifier using bwlabel.
[labeledImage, numRegions] = bwlabel(binaryImage);
% Step 2: Thicken the regions to remove black borders using bwmorph.
% This step may not be necessary if your regions are already adjacent to each other.
% Adjust the number of iterations of thickening as needed.
thickenedImage = bwmorph(labeledImage, 'thicken', 1);
% Step 3: Use graycomatrix to find adjacent regions.
% This step involves creating a gray level co-occurrence matrix for the labeled image.
% For this, you might need to loop over each pair of regions to check adjacency.
% Since graycomatrix works on grayscale images, we ensure our labeled image is in the
% range 0-255 (typical for grayscale images).
scaledLabeledImage = uint8(255 * mat2gray(labeledImage));
% Initialize adjacency matrix
adjacencyMatrix = zeros(numRegions);
% Find adjacent regions
for region1 = 1:numRegions
for region2 = region1+1:numRegions
% Create a binary image for each pair of regions
binaryPair = ismember(scaledLabeledImage, [region1, region2]);
% Calculate the co-occurrence matrix for the pair of regions
glcm = graycomatrix(binaryPair, 'Offset', [0 1; -1 0; -1 1; -1 -1]);
% If there is co-occurrence, the regions are adjacent
if any(glcm(:) > 1)
adjacencyMatrix(region1, region2) = 1;
adjacencyMatrix(region2, region1) = 1;
end
end
end
% Now, adjacencyMatrix contains information about which regions are adjacent.
% An entry of 1 at (i,j) means region i is adjacent to region j.
% Please note that the exact implementation may vary depending on your specific
% image and requirements. This is a general approach and may require adaptations.
Make sure to replace binaryImage with your actual segmented image variable. The code above assumes you have a binary image where objects are marked with ones and the background is marked with zeros. The bwlabel function labels connected objects with unique identifiers. The bwmorph function with 'thicken' is used to ensure that adjacent regions touch each other, which may or may not be necessary depending on your specific image. The graycomatrix function is used to create a gray level co-occurrence matrix, which is then used to determine if regions are adjacent.
You'll need to test and possibly adjust the code to suit your specific image and analysis needs.
Note
This is a general approach and may require adaptations.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering
  1 Comment
Akhil
Akhil on 2 Jan 2024
Thanks for the help. I tried the suggested code and made some of my adjustments, however, adjacencyMatrix giving values 1 where i is not equal to and 0 where i and j equal to zero.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!