Dear ImageMagick users,
I installed the ImageMagick some days ago and made some basic operations with images. I took a look at some functions and suppose it's possible to do the task described below:
I have some coins pictures over a black background and all of them are photographed the same way. I'd like to cut the coin image and save it in a new file. The program could detect a range of pixels near black color and crop the circle at the coin position.
Is this possible to do? How should this can be done? If somebody give some tips I can right a routine to do this job. To facilitate the analysys of that I disponibilized some pictures according I told.
https://drive.google.com/open?id=0B99SD ... Tg0MHpJNTQ
https://drive.google.com/open?id=0B99SD ... HZyNTBTN1k
https://drive.google.com/open?id=0B99SD ... 29lWFBpQ2M
https://drive.google.com/open?id=0B99SD ... mhXdWdjOTg
https://drive.google.com/open?id=0B99SD ... XRvVmZNWFE
I'll be very glad and thankfull with a helping of that. Thanks for everybody to be patience with my question.
Regards,
Rangel.
Detecting an coin image over a black background and crop it?
-
- Posts: 3
- Joined: 2016-11-15T09:23:23-07:00
- Authentication code: 1151
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Detecting an coin image over a black background and crop it?
Using image 20161112_185348.jpg on Unix-based systems, you can do it as follows:
# automatically threshold the image using my script (otsuthresh -- see link below)
# do connected components labeling to find all the isolated regions in the image (see http://magick.imagemagick.org/script/co ... onents.php)
# loop over all regions and extract color, bounding box and centroid
# if the color is white and the centroid is within dist_thresh=250 (adjustable) of the center of the image then stop
# crop the image according to the last bounding box found at the break
Please always provide your IM version and platform? Sorry, I do not know how to convert the Unix commands to Windows syntax.
# automatically threshold the image using my script (otsuthresh -- see link below)
# do connected components labeling to find all the isolated regions in the image (see http://magick.imagemagick.org/script/co ... onents.php)
# loop over all regions and extract color, bounding box and centroid
# if the color is white and the centroid is within dist_thresh=250 (adjustable) of the center of the image then stop
# crop the image according to the last bounding box found at the break
Code: Select all
infile="20161112_185348.jpg"
dist_thresh=250
dim=`convert "$infile" -format "%wx%h" info:`
ww=`echo $dim | cut -dx -f1`
hh=`echo $dim | cut -dx -f2`
cx=`convert xc: -format "%[fx:$ww/2]" info:`
cy=`convert xc: -format "%[fx:$hh/2]" info:`
#echo "ww=$ww; hh=$hh; cx=$cx; cy=$cy;"
otsuthresh "$infile" tmp1.gif
OLDIFS=$IFS
IFS=$'\n'
dataArr=(`convert tmp1.gif -define connected-components:verbose=true -connected-components 8 null: | tail -n +2`)
IFS=$OLDIFS
num=${#dataArr[*]}
echo "num=$num"
for ((i=0; i<num; i++)); do
centroid=`echo "${dataArr[$i]}" | sed 's/^[ ]*//' | cut -d\ -f3`
xc=`echo "$centroid" | cut -d, -f1`
yc=`echo "$centroid" | cut -d, -f2`
color=`echo "${dataArr[$i]}" | sed 's/^[ ]*//' | cut -d\ -f5`
bbox=`echo "${dataArr[$i]}" | sed 's/^[ ]*//' | cut -d\ -f2`
#echo "centroid=$centroid; xc=$xc; yc=$yc; color=$color; bbox=$bbox;"
if [ "$color" = "srgb(255,255,255)" ]; then
dist=`convert xc: -format "%[fx:hypot(($xc-$cx),($yc-$cy))]" info:`
test=`convert xc: -format "%[fx:$dist<$dist_thresh?1:0]" info:`
if [ $test -eq 1 ]; then
break
fi
fi
done
convert "$infile" -crop $bbox +repage coin.png
rm -f tmp1.gif
Please always provide your IM version and platform? Sorry, I do not know how to convert the Unix commands to Windows syntax.
-
- Posts: 3
- Joined: 2016-11-15T09:23:23-07:00
- Authentication code: 1151
Re: Detecting an coin image over a black background and crop it?
Hi, Thanks for you repply.
My imagemagick version is: ImageMagick-7.0.3-Q16
I took a look at the script and I will try to make one in windows. But before that, do you think is possible to make this function to crop the coin image on its format. (a circle shape, without black borders)?
I've been thinking in a way to do that, since to cut the image one by one it takes a long time and is very slow and boring task.
I thank you until the moment to show me some solution.
When is possible don't hesitate to contact me.
Have a nice day.
Regards,
Rangel.
My imagemagick version is: ImageMagick-7.0.3-Q16
I took a look at the script and I will try to make one in windows. But before that, do you think is possible to make this function to crop the coin image on its format. (a circle shape, without black borders)?
I've been thinking in a way to do that, since to cut the image one by one it takes a long time and is very slow and boring task.
I thank you until the moment to show me some solution.
When is possible don't hesitate to contact me.
Have a nice day.
Regards,
Rangel.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Detecting an coin image over a black background and crop it?
I am not sure what you mean by "to cut the image one by one it takes a long time"? Do you mean that you have multiple coins in the same image? That is much harder depending upon your background. Can you post an example?
If I misunderstand and you already have individual images, then a script can be made to loop over the processing that I provided and do the same for each image.
It is also harder to make your background transparent, since it is not too different from the coin in many cases. If you can photograph your coins on a pure background color that is quite a different color than your coins with diffuse lighting, then it might be easier to remove the background. Black or white may not be the best choice.
If I misunderstand and you already have individual images, then a script can be made to loop over the processing that I provided and do the same for each image.
It is also harder to make your background transparent, since it is not too different from the coin in many cases. If you can photograph your coins on a pure background color that is quite a different color than your coins with diffuse lighting, then it might be easier to remove the background. Black or white may not be the best choice.
-
- Posts: 3
- Joined: 2016-11-15T09:23:23-07:00
- Authentication code: 1151
Re: Detecting an coin image over a black background and crop it?
Thanks for you replying. Sorry for delaying. I've got too busy last days.
I tryed to say that doing this job in a GUI interface (In a convetional way) it's takes a long time. Imagine open the file, select the image, cut and export to a specific file extension. and every time I got new coins I'll have to do it. And many times I got many of it at the same time. It's really boring and laborius.
About the images, I have individual ones. So, maybe the loop is a way to do it. I just don't understand why when there's a black or white background at this case is not the best option to remove it from images.
My proccess follow this way:
I make a photo of each coin under a black bakcground, since this colour is better to avoid reflection from the metal at the camera lens. Besides that I use a special tecnique with a glass to put the light at the right place. After that I cut just the coin image and past in a jpg file. So, the image get a white background. I cannot use white background since is very reflexive and disturb the image quality light direction, reflexion, etc.
So, after that I have the image ready to make what I need and present it. My idea is to make an algorithm to make this hard and boring job.
Here I have an image link after cut just the "coin image" and save it in a jpg file:https://drive.google.com/file/d/0B99SDo ... sp=sharing
But you give a hope to use imagemagick to make this task. What should you advice in this situation?
Thanks for your replying and I hope your contact soon.
Regards,
Rangel.
I tryed to say that doing this job in a GUI interface (In a convetional way) it's takes a long time. Imagine open the file, select the image, cut and export to a specific file extension. and every time I got new coins I'll have to do it. And many times I got many of it at the same time. It's really boring and laborius.
About the images, I have individual ones. So, maybe the loop is a way to do it. I just don't understand why when there's a black or white background at this case is not the best option to remove it from images.
My proccess follow this way:
I make a photo of each coin under a black bakcground, since this colour is better to avoid reflection from the metal at the camera lens. Besides that I use a special tecnique with a glass to put the light at the right place. After that I cut just the coin image and past in a jpg file. So, the image get a white background. I cannot use white background since is very reflexive and disturb the image quality light direction, reflexion, etc.
So, after that I have the image ready to make what I need and present it. My idea is to make an algorithm to make this hard and boring job.
Here I have an image link after cut just the "coin image" and save it in a jpg file:https://drive.google.com/file/d/0B99SDo ... sp=sharing
But you give a hope to use imagemagick to make this task. What should you advice in this situation?
Thanks for your replying and I hope your contact soon.
Regards,
Rangel.
Re: Detecting an coin image over a black background and crop it?
This sort of works for the 50p coin but the problem you have as fmw42 mentioned some of your colours are in the coin and the background also has a lot of colours.
You may need some lateral thinking and perhaps work the other way around and create a mask from the coin. But in any case you want to try and keep the coin in the same place so you can remove as much of the background as possible first.
There is probably some way to clean up the stray blobs and so trim a bit neater and possibly enable you to reduce the fuzz factor.
Code: Select all
convert 20161112_190155.jpg -crop 500x500+605+275 -fill white -fuzz 25% -floodfill +10+10 "#3a3937" -trim -background white -gravity center -extent 500x500 coin.png
You may need some lateral thinking and perhaps work the other way around and create a mask from the coin. But in any case you want to try and keep the coin in the same place so you can remove as much of the background as possible first.
There is probably some way to clean up the stray blobs and so trim a bit neater and possibly enable you to reduce the fuzz factor.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Detecting an coin image over a black background and crop it?
The stray blobs can be removed using -morphology close. see http://www.imagemagick.org/Usage/morphology/
Re: Detecting an coin image over a black background and crop it?
I tried using different -morphology settings but it effected the coin to much. I am leaning towards creating a mask from the -morphology image with -black-threshold and using that to cut out the coin.
The main problem I am having is the coin background. I would suggest the OP puts some "flatter" material behind the coin like velvet or felt to have less colours.
Also I notice the two coins above have burnt out areas shown as white.
Anyway it was interesting but as I say a better original images would probably make for better results.
The main problem I am having is the coin background. I would suggest the OP puts some "flatter" material behind the coin like velvet or felt to have less colours.
Also I notice the two coins above have burnt out areas shown as white.
Anyway it was interesting but as I say a better original images would probably make for better results.