how to draw an outline for a given image

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?".
acpaps
Posts: 11
Joined: 2016-01-05T16:44:00-07:00
Authentication code: 1151

how to draw an outline for a given image

Post by acpaps »

Can anyone suggest me how to draw outline for an image in C#, image is of any shape.
Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to draw an outline for a given image

Post by fmw42 »

Please always provide your IM version and platform. What do you mean by outline? edges in an image? or just the outside borders?

Please show an example of what you want.

Please read the top-most post in this forum at viewtopic.php?f=1&t=9620.

I have moved this topic to the User's forum.
acpaps
Posts: 11
Joined: 2016-01-05T16:44:00-07:00
Authentication code: 1151

Re: how to draw an outline for a given image

Post by acpaps »

Thanks for the reply, for now i am not using any IM version, would like to know if by using IM is it possible to draw border by removing extra white spaces around image. I am trying to detect actual image and draw border/outline around that image (shape can be varied based on input).
For example, for a flower image. I would like to draw outline around that flower.
Sorry I didn't find any tool to attach images here that shows an example of what I need.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to draw an outline for a given image

Post by fmw42 »

Can you provide an example image? You can post to some free hosting service such as dropbox.com and put the URL here. Is the background of your flower image outside the flower a constant color or near-constant color. If not, background removal is rather hard with a cluttered background. Imagemagick does not have any direct function for the latter or the following.

See
http://clippingmagic.com/
https://www.microsoft.com/en-us/researc ... rabCut.pdf
http://docs.opencv.org/3.1.0/d8/d83/tut ... #gsc.tab=0
acpaps
Posts: 11
Joined: 2016-01-05T16:44:00-07:00
Authentication code: 1151

Re: how to draw an outline for a given image

Post by acpaps »

Thank you for the reply, yes background color is constant color.
For example, take your profile pic as input. I need to draw outline only around hat and magic wand, and remove extra white space around that.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to draw an outline for a given image

Post by snibgo »

Trimming off a constant-colour border is trivial: "-trim" does the job.

Drawing an edge around something is easy: "-edge 1".

There are many variations on these basic techniques.

For many example of simple usage of ImageMagick, see the usage pages: http://www.imagemagick.org/Usage/ .
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to draw an outline for a given image

Post by fmw42 »

Here is a simple example in command line. I do not know C#

input:
Image

Code: Select all

convert A_image.gif -colorspace gray -edge 1 -fuzz 1% -trim +repage A_image_edge.gif
Image

The -fuzz 1% is not needed here since the background is exactly constant. But if you started with JPG, then the background would not be a perfectly flat colored. So you can use -fuzz XX% to allow some percent difference in the background color.
acpaps
Posts: 11
Joined: 2016-01-05T16:44:00-07:00
Authentication code: 1151

Re: how to draw an outline for a given image

Post by acpaps »

Thank you so much for the reply.
I need to remove edge exactly to shape 'A' leaving inside triangle as it is. need to have border around 'A' shape.
Thank you, will do more research on how to use.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to draw an outline for a given image

Post by fmw42 »

Fill the hole with blue:

Code: Select all

convert A_image.gif -fuzz 85% -fill "rgb(0,0,128)" -draw "color 75,75 floodfill" -alpha off -colorspace gray -edge 1 -fuzz 1% -trim +repage A_image_edge2.gif
Image

Or add a 1 pixel border of background white, floodfill the outside of the letter A with red from the top left corner, shave off the 1 pixel border, change non-red to white and red to black, extract edge, trim off the excess region around the outside of the A.

Unix syntax:

Code: Select all

convert A_image.gif -bordercolor white -border 1 \
-fuzz 5% -fill "red" -draw "color 0,0 floodfill" -alpha off \
-shave 1x1 \
-fill white +opaque red -fill black -opaque red \
-edge 1 \
-fuzz 1% -trim +repage \
A_image_edge3.gif
Image


I used unix syntax, since you did not reply to my request to provide your IM version and platform! Syntax differs
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to draw an outline for a given image

Post by fmw42 »

Please supply your image. You can upload to some place such as dropbox.com and put the URL here. Then we can better understand what you are trying to do.
acpaps
Posts: 11
Joined: 2016-01-05T16:44:00-07:00
Authentication code: 1151

Re: how to draw an outline for a given image

Post by acpaps »

Please check the images
I have this
Image

and I need this
Image

cropped image is in image shape not regular rectangle or square
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to draw an outline for a given image

Post by fmw42 »

I have asked for you IM version and platform, but you have not provided it. Please always provide that. Please read the top-most post in this forum at viewtopic.php?f=1&t=9620.


If on IM 6, then

Code: Select all

convert 781c0085d6.jpg -bordercolor white -border 1 -fuzz 20% -fill none -draw "matte 0,0 floodfill" -shave 1x1 result.png
If on IM 7, then

Code: Select all

magick 781c0085d6.jpg -bordercolor white -border 1 -fuzz 20% -fill none -draw "alpha 0,0 floodfill" -shave 1x1 result.png
acpaps
Posts: 11
Joined: 2016-01-05T16:44:00-07:00
Authentication code: 1151

Re: how to draw an outline for a given image

Post by acpaps »

Thanks for the reply I will try the code...
About IM version, I am not using any IM version for now. I wanted to know if it possible with IM.
I would like to use C# version if possible.
acpaps
Posts: 11
Joined: 2016-01-05T16:44:00-07:00
Authentication code: 1151

Re: how to draw an outline for a given image

Post by acpaps »

Is it possible to get rid of the black color part that is outside of the image
here is the result i got (I am using IM7 for this result)
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to draw an outline for a given image

Post by fmw42 »

What was your exact code? What is your IM version and platform? Why do you not answer this question?

Please read the top-most post in this forum at viewtopic.php?f=1&t=9620.


What do you get from

Code: Select all

convert -version
Your output should be transparent. If you tried to save as JPG, then that is the issue. JPG does not support transparency. Use PNG or TIFF.
Post Reply