Contour cut images

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?".
prasan
Posts: 6
Joined: 2017-03-27T03:46:05-07:00
Authentication code: 1151

Contour cut images

Post by prasan »

Hello,
Is it possible to contour cut the bitmap image with ImageMagick as in http://stackoverflow.com/questions/4294 ... r-with-php
I need to cut the outer contour of the image.

Thank you
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Contour cut images

Post by snibgo »

"-morphology erode disk:40" gives the basic shape, but you'll need to "-extent" first so it doesn't clip the edges. The you can CopyOpcity, and do the shadow.

As you haven't said what version IM you use or on what platform, I can't write out the command for you.
snibgo's IM pages: im.snibgo.com
prasan
Posts: 6
Joined: 2017-03-27T03:46:05-07:00
Authentication code: 1151

Re: Contour cut images

Post by prasan »

Hello snibgo,
Thank you for the information.
I have installed the php_imagick-3.4.3rc1-5.6-ts-vc11-x86 dll and the ImageMagick-6.9.3-7-vc11-x86 binary.
My system is windows 10 (64 bit os) x86 architecture with xampp server 5.6.8 (php version 5.6.8 ) http://prntscr.com/epbyc3

I want to do it by php command. Also, the image will be dynamic. It can be any bitmap image.
Thank you
prasan
Posts: 6
Joined: 2017-03-27T03:46:05-07:00
Authentication code: 1151

Re: Contour cut images

Post by prasan »

Hello,
I tried with morphology and the result image is like http://prntscr.com/ephbfb
But I want the content of the inner portion of the image too as http://prntscr.com/ephdkk
Thank you
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Contour cut images

Post by fmw42 »

In Nnix command line IM 6.9.8.3

Code: Select all

convert 6lvvc.png -bordercolor white -border 40 \
\( -clone 0 -negate -morphology dilate disk:40 \) \
\( -clone 0,1 -alpha off -compose copy_opacity -composite \) \
\( -clone 1 -negate -blur 0x10 -evaluate pow 0.75 +level 0x95% \) \
-delete 0,1 \
-reverse -compose over -composite result.png
For Windows:

Code: Select all

convert 6lvvc.png -bordercolor white -border 40 ^
( -clone 0 -negate -morphology dilate disk:40 ) ^
( -clone 0,1 -alpha off -compose copy_opacity -composite ) ^
( -clone 1 -negate -blur 0x10 -evaluate pow 0.75 +level 0x95% ) ^
-delete 0,1 ^
-reverse -compose over -composite result.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Contour cut images

Post by fmw42 »

Here is another variation (unix syntax)

Code: Select all

convert 6lvvc.png -bordercolor white -border 40 \
\( -clone 0 -negate -morphology dilate disk:40 -blur 0x1 -level 0x50%  \) \
\( -clone 0,1 -alpha off -compose copy_opacity -composite \) \
\( -clone 1 -negate -fill gray50 -opaque black -blur 0x10 -fill gray95 -opaque gray50 \) \
-delete 0,1 \
-reverse -compose over -composite result2.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Contour cut images

Post by fmw42 »

One more variation:

Code: Select all

convert 6lvvc.png -bordercolor white -border 40 -write mpr:img +delete \
\( mpr:img -negate -morphology dilate disk:40 -blur 0x1 -level 0x50% -write mpr:msk +delete \) \
\( mpr:img  mpr:msk -alpha off -compose copy_opacity -composite \) \
\( mpr:msk -negate -fill gray30 -opaque black -blur 0x10 -fill gray95 -opaque gray30 \) \
-reverse -compose over -composite \
\( mpr:msk -morphology edgein diamond:1 -negate \) \
-compose multiply -composite \
result3.png
prasan
Posts: 6
Joined: 2017-03-27T03:46:05-07:00
Authentication code: 1151

Re: Contour cut images

Post by prasan »

Hello fmw42,
Thank you for the scripts. It helped me a lot. I manage to make it work with last variation script. I got the following output http://prntscr.com/epwo9f
But for some images like http://prntscr.com/epwqfp , the contour was not fine. The output was distorted http://prntscr.com/epwqun . Maybe it is because of the shape of the image.
Also on some images, the contour cut was distorted. For eg for this image http://prntscr.com/epwrx7, the output was like http://prntscr.com/epwrmc
Any idea why? Sorry I am new to Imagemagick.

Thanks again
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Contour cut images

Post by snibgo »

Fred's commands assume the pixels in the image you want contoured are black, and all other pixels are white.

So you need to process your inputs to make that true. For example, for the yellow badge, you might have a black circle where the badge is, and white elsewhere.
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: Contour cut images

Post by fmw42 »

Try the following. First remove the black border on the right and bottom. Then trim the image and add a 50 pixel border all around.

Code: Select all

convert 88d342464188407ca76c3705330d1b64.png \
    -gravity south -chop 0x3 -gravity east -chop 2x0 \
    -trim +repage -bordercolor white -border 50 mario.png
Then process that with the following:

Code: Select all

convert mario.png -write mpr:img +delete \
\( mpr:img -negate -threshold 0 -morphology close disk:1 \
    -morphology dilate disk:30 -blur 0x1 -level 0x50% \
    -write mpr:msk +delete \) \
\( mpr:img  mpr:msk -alpha off -compose copy_opacity -composite \) \
\( mpr:msk -negate -fill gray30 -opaque black -blur 0x10 -fill gray95 -opaque gray30 \) \
-reverse -compose over -composite \
\( mpr:msk -morphology edgein diamond:1 -negate \) \
-compose multiply -composite \
result4.png
prasan
Posts: 6
Joined: 2017-03-27T03:46:05-07:00
Authentication code: 1151

Re: Contour cut images

Post by prasan »

Hello Fred,
Thank you for the script. I tried with the above script and the output was okay but the contour lines are little bit irregular(not sharp) as you can see http://prntscr.com/eqcbsi
Also, with this script http://prntscr.com/eqceqi, on some images especially with red colored like http://prntscr.com/eqcdi0, the output was like http://prntscr.com/eqcd8j . The border color takes the color of the nearest portion of the image.
I am working on trying to identify the problem.
Thank you for you help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Contour cut images

Post by fmw42 »

You have two problems with your image.

1) It has an alpha channel which is perfectly opaque. That needs to be removed
2) The white is not perfectly white. It is near white close to the mario figure. It needs to be pure white or a larger fuzz value used.

Try this script. I trims off the black border and removes the alpha channel first. Then the main script has a larger fuzz value to smooth the contour of the mario figure when making a mask image.

Note that there is a trade in the fuzz value. A larger fuzz will be smoother, but will remove part of his white hands.

Code: Select all

convert 3b01580253b94dce89bbbf3aa30b7b17.png -trim +repage -alpha off mario2.png
convert \
\( mario2.png \
    -trim +repage \
    -bordercolor white -border 40 \
    -fuzz 5% -transparent white \
    -alpha off -write mpr:img -alpha on \
    -alpha extract -morphology dilate disk:20 \
    -blur 0x1 -level 0x50% -write mpr:msk1 +delete \) \
\( mpr:msk1 -negate -fill gray30 -opaque black -blur 0x10 \
    -fill gray95 -opaque gray30 -write mpr:msk2 +delete \) \
\( mpr:msk1 -morphology edgein diamond:1 -negate -write mpr:edg +delete  \) \
mpr:img mpr:msk1 -alpha off -compose copy_opacity -composite \
mpr:msk2 -reverse -compose over -composite \
mpr:edg -compose multiply -composite \
mario2_result.png
Image
prasan
Posts: 6
Joined: 2017-03-27T03:46:05-07:00
Authentication code: 1151

Re: Contour cut images

Post by prasan »

Hello Fred,
Thank you for the info.

I want all the part of the figure. Is there any solution to achieve that without loosing any part.
Thank you so much
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Contour cut images

Post by fmw42 »

prasan wrote: 2017-03-31T06:55:55-07:00 Hello Fred,
Thank you for the info.

I want all the part of the figure. Is there any solution to achieve that without loosing any part.
Thank you so much
What part seems to be missing. I do not see anything missing in my last example.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Contour cut images

Post by fmw42 »

replace the -transparent with -draw "matte 0,0 floodfill"

Code: Select all

convert 3b01580253b94dce89bbbf3aa30b7b17.png -trim +repage -alpha off mario2.png
convert \
\( mario2.png \
    -trim +repage \
    -bordercolor white -border 40 \
    -fuzz 5% -fill none -draw "matte 0,0 floodfill" \
    -alpha off -write mpr:img -alpha on \
    -alpha extract -morphology dilate disk:20 \
    -blur 0x1 -level 0x50% -write mpr:msk1 +delete \) \
\( mpr:msk1 -negate -fill gray30 -opaque black -blur 0x10 \
    -fill gray95 -opaque gray30 -write mpr:msk2 +delete \) \
\( mpr:msk1 -morphology edgein diamond:1 -negate -write mpr:edg +delete  \) \
mpr:img mpr:msk1 -alpha off -compose copy_opacity -composite \
mpr:msk2 -reverse -compose over -composite \
mpr:edg -compose multiply -composite \
mario2_result2.png
Image
Post Reply