How to smooth edges of polygons?

I created a image (uint8) with multiple filled polygons. First, I used the vision.ShapeInserter with 'Antialiasing' but this was not enough. Also insertShape + 'SmoothEdges' produced insufficient smooth edges. Are there any other ways how I can fix that?
Looking forward to your suggestions. Thanks!

Answers (2)

Perhaps imopen with a disk structuring element will provide the functionality you are looking for?
Ex:
se = strel('disk',5);
new_img = imopen(original_image,se);
Hope this helps!

3 Comments

Hi Andrew and thanks for your answer! As you can see it did not really help. I guess the edges are too fine. Are there any other suggestions?
Did Image Analyst's answer get you what you wanted? Otherwise, maybe you could clarify what you mean by "smooth edges". What in your mind constitutes a sufficiently smooth edge?
For example, in this image polygons show proper edges without these awful "stairs" when the shapes get rotated. By the way, this image was drawn in photoshop. But now I have to create a lot of stimuli in MATLAB by using coordinates for my polygons. Consequently, I am looking for a MATLAB routine to generate images of same quality. I hope this explanation helps. Thanks!

Sign in to comment.

Image Analyst
Image Analyst on 13 Oct 2017
You can smooth the edges with a filter. See attached.

8 Comments

Thanks for the code! But is there is also an easier and more fundamental solution? Due to the fact that I create these polygons in MATLAB directly I cannot understand why the output of such simple shapes can be so bad. Could maybe a vector based format be the right way? Unfortunately, I did not find such a format for imwrite.
I don't know how they were created and what they are. If they are images, are they grey scale, or binary? If they're binary images, you can smooth them nicely by blurring with conv2() or imfilter() and then thresholding.
m975's "Answer" moved here because it's not an "Answer" to the original question, but a reply to me.
I opened an image with
image = zeros(PicHeight,PicWidth,3,'uint8') + 255;
and inserted polygons with the vision.ShapeInserter. Finally, I saved the images with imwrite as PNG.
OK, then blurring should work, like I said. Did you try it? If not, why not?
blurredImage = imfilter(grayImage, ones(9)/81);
Because I thought you need more information about the image. Now I tried it but it looks too blurish.
It looks binary, so just threshold it. The corners should then be more rounded off.
I processed the image as follows but it did not work.
level = graythresh(I);
BW = im2bw(I, level);
You can still see these toothed edges:
Finally, the easiest workaround was to create very big images (5000 x 5000 px) and, subsequently, resize them to the favored stimulus size. Now edges look sexy again!

Sign in to comment.

Asked:

on 12 Oct 2017

Commented:

on 19 Oct 2017

Community Treasure Hunt

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

Start Hunting!