Fit countours to shape
Show older comments
Yo, image processing experts, any way to automatically fit the 4 boundary lines of this shape:

This what I want:

I believe this should be easy.
Any ideas? This should be done in less than 1 second.
4 Comments
John D'Errico
on 19 Dec 2024
Edited: John D'Errico
on 19 Dec 2024
It is amazing how what the human brain thinks is easy, even trivial, is not so trivial at all when done by computer. I would note that you only THINK it should be easy. Perhaps you have been watching too many TV shows, where computers can do anything? Where any problem can be solved using now more than a couple of keystrokes?
André
on 19 Dec 2024
André
on 19 Dec 2024
Moved: John D'Errico
on 20 Dec 2024
John D'Errico
on 20 Dec 2024
NO. It is not at all trivial to do what your brain thinks is easy. Obviously, if it was as easy as you seem to think, you would already have written the code.
What are the issues?
- Some of the sides of that "polygon" are curved. Straight lines are one thing. But a general curved arc is less easy.
- Those curved lines were constructed so they seem to ignore SOME of the bumps in the black regions. So sort of a least squares fit, but that would involve a fit with deviations in both x and y. And that alone is a little more difficult, but doable, but when that involves a curved line, things go all to hell.
- There is stuff inside the region that you have blithely chosen to ignore. Again, you know what you want to see. But writing code for what you want to see is not as easy as you think.
- The curves for each side intersect in a point that is sometimes unconstrained by your data. Essentially, you are extrapolating those curved arcs to where they intersect. But extrapolation is a difficult thing to accomplish (at least, doing extrapolation well by any definition of the word "well".)
- And you want code that does all of this automatically in less than a second. Sigh.
- I'm sure there is a number 6, but we can stop here.
Again, if it is easy to do, then start writing! Surely you know how to solve all of the issues I described? As I said, I think we all see too many TV shows and movies where the computers are godlike. That may be coming one day, sooner than I like, but...
My recommendation is an easy one. Draw the lines yourself. Take advantage of the processor in your brain that knows exactly what you want to see. You can use a tool like ginput to pick points off the figure, then connect them in a polyfon. Easy, peasy.
The result will be just as good as any code you will write. Yes, it will not be as fast as you want. Life sucks.
Accepted Answer
More Answers (2)
I made an attempt using the Image Segmenter app. I first thresholded the image, then eroded the mask to get a roughly square shape, filled in the holes, then dilated the mask using a square shape that touched all 4 sides.
Once I had that, I borrowed some code from the Boundary Tracing in Images example to generate an outline.
The solution is defniitely customized for this particular image, but may work for similar images.
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1821389/image.png');
imshow(img)
% Threshold image with global threshold
BW = imbinarize(im2gray(img));
% Erode mask with square
width = 100;
se = strel('square', width);
BW = imerode(BW, se);
% Fill holes
BW = imfill(BW, 'holes');
% Dilate mask with square
width = 100;
se = strel('square', width);
BW = imdilate(BW, se);
boundaries = bwboundaries(BW);
b = boundaries{1};
hold on
plot(b(:,2),b(:,1),"g",LineWidth=3);
André
on 20 Dec 2024
0 votes
Categories
Find more on Red in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
