Page 1 of 1

trying to rotate vector image based on angle histogram

Posted: 2015-11-01T04:43:58-07:00
by marchywka
I have not used ImageMagick recently and just downloaded and built the latest release but was not able to figure out
how to achieve a specific result although I did find some features, like those related to hough lines in the link at bottom,
that may be close.

I've got an image source that has a really nice API although I have not found documentation it
appears to generate high quality png images, easily converted to pnm, with some parameters but the only ones
I know about right now are size,

https://pubchem.ncbi.nlm.nih.gov/image/ ... height=512

Ideally it would return vector images with some angles variable as that is the nature of the cartoon chemical structure but I can probably
work with png using potrace as shown below ( CON points to imagemagick convert in my development
directory),

$CON "$DEST" -trim -threshold 80% "xxx"
$CON "xxx" -trim "$DESTPNM"
potrace -b pgm -a -10 -T 0 -B 0 -L 0 -R 0 "$DESTPNM"
potrace -b pdf -a -10 -T 0 -B 0 -L 0 -R 0 "$DESTPNM"
potrace -b eps -a -10 -T 0 -B 0 -L 0 -R 0 "$DESTPNM"


In this specific image, the long "chain" is apparently oriented to be along a diagonal but as I wish
to put many of these images into a latex document, I'd like to rotate this and make the chain horizontal.
I can probably do this if I have a histogram of vector angles but not sure how to get that from "identify"
or other utility. I suppose the ellipse angles would be a help, and that was what I considered using, but
that is just an average distorted by the ring component. Is there some way to dump just a raw list
of vector length and orientation? If I can compute an optimal rotation angle for a given
object ( making the chain part horizontal in this and related molecular images ) that would
be even better.Thanks.

Maybe using data like this?

viewtopic.php?t=25476

Re: trying to rotate vector image based on angle histogram

Posted: 2015-11-01T10:26:53-07:00
by snibgo
I suppose you want a technique that would find the result of +120 or -60 degrees.

A trial-and-error technique of rotation and finding the minimum height bounding box will do the trick for this image.

Code: Select all

convert molecule.png -fuzz 20% -rotate XX -format %@ info:
Where XX is a number from 0 to 180 in steps of 30 or 15 or 10. However, a step size of 5 would find the "better" solution of +115 or -65 degrees.

Perhaps you can define "making the chain part horizontal" in the general case. Possile definitions:

1. Find all the line-ends. Rotate so as many as possible are on a horizontal line.

2. Find all the mid-points of lines. Rotate so as many as possible are on a horizontal line.

There may be a more suitable definition. Potrace can make an SVG file, and a program or script could then find line ends or middles, and calculate a rotation.

Re: trying to rotate vector image based on angle histogram

Posted: 2015-11-01T10:43:39-07:00
by marchywka
Thanks, I'll play with the bounding box idea and since the input image and potrace output is reasonable quality I can
confine rotations guesses to fairly course increments.

I thought this or some other vector graphics packages may have some existing routines for things
like this otherwise I will have to think about more precise definitions of "horizontal" but I was
hoping to develop those from raw dumps and maybe something like "R". I think I have seen some
examples about orienting scanned text for things like OCR and I may end up with scanned or otherwise
difficult images to manipulate.

I took a quick look at the SVG but just offhand, depending on how IM handles vectors internally,
it may be easier to just write something that dumps them as I already built the latest version to
get the "-moments" option on identify as the Ubuntu RPM seems to be from 2011.

Re: trying to rotate vector image based on angle histogram

Posted: 2015-11-01T11:27:37-07:00
by snibgo
IM has a "-deskew" operator, but it is designed for small angles, and is influenced by the ring structures at the end of the molecule. It might be useful if you can isolate the long-chain portion.

Re: trying to rotate vector image based on angle histogram

Posted: 2015-11-01T18:23:05-07:00
by marchywka
Thanks. I might be able to use the --group option with the svg output but I am a bit surprised there is not a set of utilities to operate
on vector images along these lines. I took a few different molecules and tried to put them into the latex doc and there is
also a scale problem- having all the bonds the same length among comparable molecules will likely be important although
probably the as generated images from this specific server are ok until I trim and then latex tries to make them fit.

I guess I could just assemble a composite image using IM and stick the single result into latex although
generally I would think that making images uniform would be a common enough problem to have generated
a utility to do that. Somewhere between playing with a custom IM build and using awk and sed on the svg
I'm sure I can find something to work....

Re: trying to rotate vector image based on angle histogram

Posted: 2015-11-06T13:35:23-07:00
by marchywka
fwiw, it looks like this python script mol2chemfig can be used as a basis for my immediate
objectives

http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3551648/

and I could probably add features as needed if I learn Python.

Thanks.