Page 2 of 2

Re: Get center coordinates of object

Posted: 2014-07-12T08:27:58-07:00
by fmw42
Thanks a lot for the quick implementation and the information fmw42!
However, I can't get your script to work in my environment. I get to following errors with the original image:
What version of IM? What do you get from

convert -version



What is convert.im6:? My script does not use that syntax!

Send me your email address in a PM on the forum and I will send you a script with some debugging statements and we can see what is going on. It works fine for me on IM 6.8.9.5 Q16 Mac OSX Snow Leopard.

Did you edit the file as described on my home page?


Be sure you have installed the unix calculator, bc. It is an option that you may have to select when installing unix.
Download the script
Change the name to add or remove the .sh as desired when running
Set the script to executable (chmod u+x)
Find the full path to where IM (convert) resides by typing in a shell terminal window: type -a convert
If type -a convert returns more than one path, type in a shell terminal window: path2/convert -version, where path2 is each of the paths found. Decide which version of IM you want to use.
Modify your PATH environment variable so that it includes the full path to where IM (convert) resides (often /usr/bin or /usr/local/bin). This can be done by editing your .profile file.
Alternately, edit the script somewhere between the comments and the first use of any IM command, such as just below the defaults section to add the following two lines:
imdir="path2" #(such as imdir="/usr/local/bin" or imdir="/usr/bin")
PATH="${imdir}:${PATH}"
Open a shell terminal window
bash /fullpathto/scriptname(.sh) arguments /fullpathto/inputimage /fullpathto/outputimage
To avoid the bash and just use scriptname(.sh) ... set your PATH to contain the location of the script
Optionally edit the script to change the default directory (found after the defaults section) from dir="." to dir="/tmp"
If you have trouble with filenames with spaces in them, then you will need to edit the script in several places until I have time to review and fix all my scripts.
Find where infile and outfile (maskfile or any others) are defined at the end of the argument trapping section. Make sure to enclose in double quotes any declarations of those files that include $1, $2, etc, such as infile="$1", etc
Find any occurrences of $infile or $outfile and enclose them in double quotes as "$infile", etc.

Re: Get center coordinates of object

Posted: 2015-05-20T09:38:37-07:00
by ladiko
I really like the result of thinning. I have a picture of a person holding two red objects and i need the approximate coordinates of the center of each object. I updates the Ubuntu 14.04 imagemagick version 6.7.7. to 6.8.9-1 to use sparse-color:-.

So now i have the command:

Code: Select all

convert test.ppm \
	-fuzz 37% \ # similar colors are detected as "red"
	-fill black +opaque red \ # fill the not matching area with black
	-fill white -opaque red \ # fill the matching area with white
	-morphology Open Disk:5 \ # make the objects more solid
	-bordercolor Black -border 1 \ # draw a black border around the image
	-write out.png \
	sparse-color:-
which needs 1.241 seconds and looks like that:
Image

if i add the Thinning to reduce the objects to a single pixel:

Code: Select all

convert test.ppm \
	-fuzz 37% \ # similar colors are detected as "red"
	-fill black +opaque red \ # fill the not matching area with black
	-fill white -opaque red \ # fill the matching area with white
	-morphology Open Disk:5 \ # make the objects more solid
	-bordercolor Black -border 1 \ # draw a black border around the image
	-morphology Thinning:-1 "LineEnds;Corners" \ # reduce the white objects to single pixels
	-write out2.png \
	+transparent white \ # hide the black area to sparse-color
	sparse-color:-
which results in this picture with two white pixels:

Image

but it takes 40 seconds which is way to long. Why does only using LineEnds need 2 seconds and Corners need less than 10 seconds, but both together adds up to almost 40 seconds? Isn't there another way which might be less accurate but faster? It's the best sollution to get the center of each objects, but if it's much faster, every other single pixel of each object would be sufficient.

The separate script does not work for me, [/i]convert -list configure[/i] complains convert: unable to access configure file `configure.xml'. Seems like the built i am using, misses that file: https://launchpad.net/~oded-geek/+archi ... multimedia

Ok i added a fixed im_version="06080901" to the separate script and executed ./separate -m 5 -v -t -l out.gif tmp.png but beside using 34 seconds CPU time, it didn't to anything.

Re: Get center coordinates of object

Posted: 2015-05-20T15:02:04-07:00
by snibgo
Repeating a {LineEnds,Corners} multiple times is different to repeating LineEnds then repeating Corners.

There are many ways of finding the "center" of an object. One very fast method is "-connected-components". See http://magick.imagemagick.org/script/co ... onents.php

Code: Select all

convert blob2.jpg -threshold 50% -define connected-components:verbose=true -connected-components 4 NULL:
This finds three areas: two white, and the large black area. It return barycentres, which are not necessarily contained within the found area (the barycentre of a crescent-moon shape is not within the shape).

EDIT: "-connected-components" also returns the coords of each containing rectangle, which is a different measure of the "centre".

Re: Get center coordinates of object

Posted: 2015-05-20T22:45:59-07:00
by ladiko
I just get "convert: unrecognized option `-connected-components' @ error/convert.c/ConvertImageCommand/1156.". What is the minimal version for this parameter?

Edit1: A i found it: 6.8.9-10 - so i have to find a newer PPA.

Edit2: This one works: https://launchpad.net/~apps-z/+archive/ ... diabrowser

Edit3: That needs 4.5 seconds - much better and even more details, cause i can choose if i want to get the area or the center or calculate something from the borders myself!

Code: Select all

time convert test.ppm -fuzz 37% -fill black +opaque red -fill white -opaque red -morphology Open Disk:5 -threshold 50% -define connected-components:verbose=true -connected-components 4 NULL:
Objects (id: bounding-box centroid area mean-color):
  0: 960x720+0+0 479.4,360.5 687506 srgb(0,0,0)
  1: 91x84+549+61 594.5,102.6 2596 srgb(255,255,255)
  2: 52x45+249+335 278.4,352.8 1098 srgb(255,255,255)

real	0m4.486s
user	0m4.464s
sys	0m0.028s
Thanks for this great solution!