Adding border only around filled area

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
researcher
Posts: 3
Joined: 2020-03-26T09:19:18-07:00
Authentication code: 1152

Adding border only around filled area

Post by researcher »

Hello,

I am trying to remove the grey oval/rectangle in an image, such as in the following image on the left, without affecting the text ("17 sec"):

https://i.postimg.cc/MGXqz2L0/original.png

The ovals are coloured #8b8b8f, so I tried filling that colour with white:

Code: Select all

convert original.png -fuzz 1% -fill white -opaque '#8b8b8f' new.png
But this leaves the oval edges:

https://i.postimg.cc/0Q2mkQsW/new.png

If I increase the fuzz any, the text is affected.

Is there a way to get rid of the oval edges too? Perhaps by using -floodfill (but without knowing the coordinates of the seed point in the oval)? Or filling then adding a border around the filled area?

Constraints: Each image has several ovals and the ovals vary in their length and position across the images.

Thank you!
Last edited by researcher on 2020-03-26T10:38:58-07:00, edited 2 times in total.

User avatar
fmw42
Posts: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding border only around filled area

Post by fmw42 »

Your links are not going to any images that make sense for what you are describing.

researcher
Posts: 3
Joined: 2020-03-26T09:19:18-07:00
Authentication code: 1152

Re: Adding border only around filled area

Post by researcher »

I've edited the text to make it clearer — does it make sense now? The images should be of a grey oval/rectangle with the text "17 sec" beside it.

User avatar
fmw42
Posts: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding border only around filled area

Post by fmw42 »

You can use -connected-components to find each individual gray region in the image and get its centroid and then do a flood fill. You can identify the oval since it has the largest area of gray. I suggest thresholding first, then find the oval centroid. Then go back to the original and do your flood fill. Or better flood fill the thresholded image, use that as a mask to composite white in the original image. That way if it leaves a ring, you can use morphology to make the mask bigger where needed.

researcher
Posts: 3
Joined: 2020-03-26T09:19:18-07:00
Authentication code: 1152

Re: Adding border only around filled area

Post by researcher »

Thank you very much for the help. I was able to get it working with the following:

Code: Select all

convert original.png \
    \( +clone -fuzz 1% -fill white +opaque '#8b8b8f' \
    -define connected-components:area-threshold=10 \
    -connected-components 4 -white-threshold 1 \
    -transparent black -channel A -morphology EdgeOut Diamond +channel \
    +level-colors white \
    \) -composite -fuzz 1% -fill white -opaque '#8b8b8f'  \
    show:
(For some reason this does not work on the example images perhaps due to compression while saving, but it works on the originals.)

Post Reply