Filling empty region with line segment

Answers (2)

regionprops() asking for Centroid and extrema. kmeans() to cluster the x coordinates of the centroids into two clusters. Now pdist2() between the centroids of one cluster to the centroids of the other cluster. For each of the clusters on one side find the nearest (smallest distance) to any member of the other cluster. This will tell you how the clusters pair up.
Now knowing how the clusters pair up, go back to the extrema data you asked for and find the coordinates of the right side of each cluster and the left side of the corresponding paired cluster. Draw lines connecting the two.
... All of this can be much easier if you can make simplifying assumptions such as that the clusters will always be in two sides (not distributed around the box) and that each blob is to be joined with another blob on very much the same horizontal level. Further simplifications might be possible depending on what you need to do with the joined lines.

1 Comment

Hello, thank you for your suggestions. They gave me more insight on functions I had never heard of. However, for some odd reason, the centroid for the bottom right side is closer to the middle left centroid. Nonetheless, it seems like kmeans() lists them in an order that can be used to connect them later on. Furthermore, the extrema includes some values that are far to the left in the left cluster, but I am only interested in connecteing the edges. I believe this is due to to the horizontal lines not being completely smooth. I am only interested in connecting the edges and not changing the other points in the horizontal line. Again, thank you for your input. It has given me a direction I can take.

Sign in to comment.

What assumptions can we make? Can we assume that during your processing we have an equal number of line blobs on the left hand side and right hand side? Can we assume they're always roughly horizontal?
Do you really want to "connect" them, or maybe just crop out the center of the image?
How about if we label the lines with bwlabel (in the Image Processing Toolbox) and then used ismember to extract out each pair in turn. Then use find() to get the coordinates of all the pixels in the blob pair. Then use polyfit to get the equation of a line through them. Then draw that line (burn it into the image) in the gap zone. It might get a little more complicated than that if, for example, the line blobs on each side are not aligned, but that can be handled without too much additional difficulty should that happen.

4 Comments

Yes, they should have the same number of blobs on each side. I have a picture of a droplet in some horizontal strands. I want to have an image of just the horizontal strands. However, the horizontal strands seem to vary in thickness due to some shadows that are casted. We want to keep the shadows in the "skeleton" because I am subtracting the image from future frames and only want the droplet interactions to be shown. The first image I showed was where my current algorithm detects the droplet and deletes the columns. The situations can vary so I am trying to find a generalized solution for most of these situations. I am trying to minimize the amount of shadow I lose, so that is why I want to connect one edge to the other edge. Here is an example of an image before removing the columns with the droplet.
Can you attach an original gray scale image, before segmentation? And give your code for segmenting and making the binary image?
sensitivity_threshold = 0.59;
PixelFilter = 20;
blackandwhite=imbinarize(first_img,'adaptive','ForegroundPolarity','dark','Sensitivity',sensitivity_threshold);
blackandwhite = ~blackandwhite;
filteredimage=bwareaopen(blackandwhite,PixelFilter);
filteredimage = imfill(filteredimage, 'holes');
sensitivity_threshold = 0.59;
PixelFilter = 20;
blackandwhite=imbinarize(first_img,'adaptive','ForegroundPolarity','dark','Sensitivity',sensitivity_threshold);
Unrecognized function or variable 'first_img'.
blackandwhite = ~blackandwhite;
filteredimage=bwareaopen(blackandwhite,PixelFilter);
filteredimage = imfill(filteredimage, 'holes');
Darn. I'll check back later for the missing first part of the code that you forgot to post.

Sign in to comment.

Asked:

on 20 Jul 2023

Edited:

on 25 Jul 2023

Community Treasure Hunt

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

Start Hunting!