Connected region (grayconnected)

Hi all,
I am using the grayconnected() function (multiplication of grayconnected on all 3 color layers) in order to obtain a mask of a part of the image that is separated by a black line (example given bellow).
You can see how the first line is consistent and it devides the image (using grayconnected), but the second line is not.
For this example I would like to have 3 different masks, but because the bottom line is not consistent, I am getting only 2.
I can't use imclose() on the black lines, because in some cases the lines are very dense, so imclose() will turn my image into one big black blob.
Is there a way to specify (using grayconnected) how many pixels do I consider as "connected" (so that a hole 1-2 pixel wide would not be considered as a connection between two regions)?
Should I consider a different approach?
Thank you very much.

Answers (1)

Image Analyst
Image Analyst on 3 Mar 2019
What tolerance value did you use in your call to grayconnected()? Did you try adjusting it? Attach your original image and code.

4 Comments

Thank you for your reply.
I didn't adjust the tolerance. All of my images consist of about 5-10 monotonic colors (segments). These segments are devided to smaller pieces by black lines. I am not sure how the tolerance adjustment can be helpful (it is used to define what color range is considered the same, right?). The tolerance that I am looking for is "what is considered connected" instead of "what is considered the same color".
Unfortunately, It will be difficult for me to find and attach the relevant part of the code, as it is a part of a much bigger problem that I am working on.
I am scanning the RGB image (2 for loops over i and j) and I am looking for the piece that the pixel is part of:
segment=grayconnected(myIMG(:,:,1),i,j).*grayconnected(myIMG(:,:,2),i,j).*grayconnected(myIMG(:,:,3),i,j)
The image above represents the classic case of my problem. I need it to be 3 different red segments, but because the bottom line is not consistent - the bottom two segments are considered the same by the grayconnected().
I am looking for a way to define "what connected is" (lets say 3 pixels hole and above).
Once again, I appreciate your help.
I'd be willing to help if you can simplify it down to something that lets me help. Right now I have virtually nothing.
One of the images is attached. You can see how some black lines are consistent, but some are not (even a 1 pixel hole in the separation line merges the segments).
The code is something like this:
myIMG=imread('myIMG.png');
[rows,cols,~]=size(myIMG);
for i=1:rows %run on entire image
for j=1:cols
%segment mark
segment=grayconnected(myIMG(:,:,1),i,j).*grayconnected(myIMG(:,:,2),i,j).*grayconnected(myIMG(:,:,3),i,j);
%operation
% on each such segment im making an operation (once per segment,
% so I delt with marking the segment as completed so that I will not hit
% the same segment on next pixel in loop).
end
end
I want the segment to be as small as possible (devided by black lines, including the inconsistent ones).
I hope it gives enough information about my problem.
Thank you.
I'd probably try 4-connected labeling instead of the default 8-connected. That will help.
You might also try watershed to split apart the blobs, if needed.click here
Or you might try a simple erosion to enlarge all the black lines.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 3 Mar 2019

Commented:

on 3 Mar 2019

Community Treasure Hunt

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

Start Hunting!