Page 1 of 1

Crop Image within the border of specific color

Posted: 2019-08-14T07:54:39-07:00
by krunalmaniar13
Hello All,

I have a image where i have drown a boundry of specific color, and i want to now crop this image and have only those part of the image which is inside that specific boundry.

Ex:

I have a image of world map and i want cropped image of particular country (Ex. India) whose boundry is drawn with Yellow Color.
The resultant image should only contain the India's map and all the other part of the image can be made blank.

Can somebody suggest the command which i can use for this, Ideally i believe there should some command which find the boundary of particular hash code of color and given the inner part of that boundry as a image.

Thanks in advance for your help

Re: Crop Image within the border of specific color

Posted: 2019-08-14T08:43:46-07:00
by snibgo
What version of IM, on what platform? Sample input image?

One method would be to flood-fill from the top-left corner with yellow. Then trim.

Re: Crop Image within the border of specific color

Posted: 2019-08-14T09:47:12-07:00
by fmw42
Please post an image. You will need to upload to some free hosting service and put the URL here.

Re: Crop Image within the border of specific color

Posted: 2019-08-14T10:19:51-07:00
by krunalmaniar13
Following is the image:
https://ibb.co/3pHgn6S

Here i just want the inner part of the dark yellow boundry

Re: Crop Image within the border of specific color

Posted: 2019-08-14T10:23:02-07:00
by krunalmaniar13
I am using following exe from Imagemagick site

ImageMagick-7.0.8-60-Q16-HDRI-x64-dll.exe

And yes i am doing it on windows Machine

Re: Crop Image within the border of specific color

Posted: 2019-08-14T10:50:59-07:00
by fmw42
This is in Unix. One of the Windows users can give you the equivalent.

The technique is to clone the image to make a mask. In the clone, make anything not yellow into black and everything else white. Then flood fill from the center with white to make everything inside the yellow area including the yellow into white. Then put the mask into the alpha channel of the original image.

Input:
Image

Code: Select all

declare $(magick GIS-County.jpg -format "centx=%[fx:w/2]\ncenty=%[fx:h/2]" info:)

magick GIS-County.jpg \
\( -clone 0 -fuzz 15% -fill black +opaque "rgb(243,246,67)" -fill white +opaque black \
-fill white -draw "color $centx,$centy floodfill" \) \
-alpha off -compose copy_opacity -composite \
GIS-County_result.png
Image


In Windows, remove the \ from the parentheses. Change the end of line \ to ^. Fill in the $centx,$centy with the center values so that you can ignore the declare line. Otherwise, a Windows user will need to show you the equivalent code for creating and using variables.

Re: Crop Image within the border of specific color

Posted: 2019-08-14T12:27:14-07:00
by krunalmaniar13
Hey fmw42,

Thank you very much for your response, your script was very helpful.