advanced convert usage?

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?".
macsurveyr

advanced convert usage?

Post by macsurveyr »

I think ImageMagick convert command has the capability of doing what I would like to do in batch, on many files, but I am apparently not up to the task of figuring it out.

Here is the story.

I have software that can create an image that not only has RGB values for each pixel, but a 3D coordinate for each pixel. I can visualize the 3D model in several ways, and that is wonderful. However, getting the 3D model projected back into a 2D image is also very useful.

What I am currently doing is using convert to read a tif file and create a text file with the RGB values - yes 16 bit values

# ImageMagick pixel enumeration: 307,145,65535,rgb
0,0: (40349,36237,32896) #9D9D8D8D8080 rgb(157,141,128)
1,0: (41634,37008,33667) #A2A290908383 rgb(162,144,131)
2,0: (39321,33410,30840) #999982827878 rgb(153,130,120)
3,0: (37522,32639,30069) #92927F7F7575 rgb(146,127,117)
4,0: (34438,30069,27499) #868675756B6B rgb(134,117,107)
5,0: (43433,39064,36494) #A9A998988E8E rgb(169,152,142)
6,0: (39321,34952,32382) #999988887E7E rgb(153,136,126)
7,0: (40349,35980,33410) #9D9D8C8C8282 rgb(157,140,130)
8,0: (41120,37008,33667) #A0A090908383 rgb(160,144,131)
9,0: (33153,28527,25443) #81816F6F6363 rgb(129,111,99)
...

I then extract just the 16 bit RGB values and paste them into a file so the xyz coordinate has a rgb value

4589.918 2602.008 -620.766 40349 36237 32896
4587.991 2601.901 -619.187 41634 37008 33667
4585.957 2601.825 -617.705 39321 33410 30840
4584.189 2601.673 -615.987 37522 32639 30069
4582.551 2601.486 -614.154 34438 30069 27499
4580.698 2601.359 -612.516 43433 39064 36494
4578.678 2601.280 -611.028 39321 34952 32382
4576.746 2601.176 -609.464 40349 35980 33410
4574.785 2601.081 -607.927 41120 37008 33667
4572.937 2600.955 -606.292 33153 28527 25443

Now the hard part.

I want to convert the text file with xyzrgb values into an image by interpolating the x and y columns as if they are pixels - get rid of the z since the result is 2D in nature - to the nearest unit or fraction of a unit, in this case say 1/10 of a unit - 0.1

I think I have to sort the file second column (y) then first column (x) and try to make an even row and column file based on the original rows and columns - somehow - but there is not a one to one match - evenly that is

I have tried a couple of things with convert and interpolate NearestNeighbor since the xy coords are not evenly space and looking at virtual-pixel to fill in the areas where there is no image but I just can't seem to get anything to work.

Sigh

Looking for ImageMagick convert experts and any ideas.

Hope this makes sense and Thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: advanced convert usage?

Post by fmw42 »

see Shepard's sparce color interpolation at http://www.imagemagick.org/Usage/canvas/#shepards. you may have to truncate your x,y values to integer pixel coordinates. The Shepard's interpolation is an inverse squared distance interpolation, but you can have as many points as you like, each with an x,y and color.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: advanced convert usage?

Post by anthony »

So you basically have a list of coordinates you want to plot.

You can draw symbols (or just points) at those coordinates using 'symbol drawing methods'
http://www.imagemagick.org/Usage/draw/#symbols

However you can also draw points at random using that same IM Enumerated pixel format. Any
http://www.imagemagick.org/Usage/files/#txt
points not drawn will be left as the background color, and you can specify any color you lke for each pixel.

This technique is used as part of a demonstration of the limitations of 'forward pixel mapped distortions'
http://www.imagemagick.org/Usage/distor ... rd_mapping
Here I convert a image into a 'TXT' enumerated format. Then use some maps to rotate each pixel to a new coordinate, creating a new 'TXT' image to display. Some pixels may overlay other pixels, some actually leave the image bounds causing a 'warning', and some pixels in the output image are not drawn at all causing -background 'black' color to be used instead.

The Enumerated Pixel Image Format is actually very useful for this type of thing!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: advanced convert usage?

Post by anthony »

fmw42 wrote:see Shepard's sparce color interpolation at http://www.imagemagick.org/Usage/canvas/#shepards. you may have to truncate your x,y values to integer pixel coordinates. The Shepard's interpolation is an inverse squared distance interpolation, but you can have as many points as you like, each with an x,y and color.
Warning:

While draw, image and text placement locates things in pixel coordinates -- that is a location of 2,3 is the third pixel form the left and the fourth from the top -- both -sparse-colors and -distort use a more mathematical image coordinate system, better suited to their nature.

Image coordinates mean 0 is the EDGE of the image (left or top edge), not the center of the first pixel whcih is at 0.5 That means to exactly locate the color at pixel 2,3 you need image coordinates 2.5,3.5

See Image Coordinates vs Pixel Coordinates
http://www.imagemagick.org/Usage/distor ... oordinates

Their has been suggestions that IM should have settings to define the coordinate system, (pixel or image) and the measurement system (pixels, percentage, image_size) to use for defining coordinates in distort, sparse colors, annotate, draw, geometry (composition), etc...
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
macsurveyr

Re: advanced convert usage?

Post by macsurveyr »

Thanks for the responses. I will explore more, but in the mean time, while looking at the +distort option, another possibility occurred to me, but I am not able to make it work either.

What I would like to do is

convert somefile.tif +distort affine "READ A LIST OF COORINATE PAIRS FROM A FILE" somenewfile.tif

I can't seem to read in, redirect, or otherwise bring in a list of coordinate pairs from a file. I can put a bunch of pairs on the command line and the +distort gives me the desired result, but need to be able to supply the coordinate pairs from a file.

Thanks again!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: advanced convert usage?

Post by fmw42 »

NOTE distort affine takes only 2 or 3 points, and not a long list to do a best fit. So why the need for a file. But you can try @filename for the control points if you want. I don't know if distort works with a file or not. I have not seen that demonstrated, yet. But elsewhere I know you can input a file using the @filename, but usually for a list of files as per http://www.imagemagick.org/Usage/files/
macsurveyr

Re: advanced convert usage?

Post by macsurveyr »

Thank you fmw42 - the @filename does work for the control points with distort. Both the affine and perspective options will use many points and do a least squares fit. It appears to work very well, but have only tried one small test file so far. Need to write a script to make the control point files that I need to test more. Only so much time!

Thanks again. I had looked at a lot of usage pages and had not noticed or made the connection for that option.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: advanced convert usage?

Post by fmw42 »

Sorry for the misinformation about the least square fit. I had forgot about that enhancement and overlooked Anthony's section on that. Glad the @filename worked.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: advanced convert usage?

Post by anthony »

The @filename syntax was an addition that I purposely wanted. This way you can generate a list of image registration point pairs from some other program, for the purposes of panorama joining of photos (Perspective).

Note that numbers can be comma or white space separated, which includes newlines. That means you can organise your file so that you have one coordinate pair per line in columns, making it easier for text manipulation scripts like cut, paste, column, as well as more advanced text processors such as sed, awk, perl, etc to manipulate the coordinates.

That is the file can be of the form...

X1 Y1 U1 V1
X2 Y2 U2 V2
X3 Y3 U3 V3

I am not however sure if you can specify comments in such coordinate files, though that would make a nice addition.

The use of files of coordinate pairs will become more important when 'grid' and possibly 'mesh' distortions become available. As these can require Huge numbers of coordinates, and coordinate linkage information.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: advanced convert usage?

Post by fmw42 »

Anthony,

Have you documented that, yet. I did not see it in a quick review. If not, could you add a short note something like the above, perhaps in the section about the least squares.

Fred
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: advanced convert usage?

Post by anthony »

I have moved the affine least squares to its own section, just after 'perspective'
Control Point Least Squares Fit
http://www.imagemagick.org/Usage/distor ... ol_leastsq

and added another section
Control Point from Files
http://www.imagemagick.org/Usage/distor ... trol_files

into the IM Examples. (should appear in a hour or two).

These two sections will eventually be followed by a section on 'image registration' where both features will be used. As soon as I find a nice semi-automatic image registration discovery tool.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: advanced convert usage?

Post by fmw42 »

These two sections will eventually be followed by a section on 'image registration' where both features will be used. As soon as I find a nice semi-automatic image registration discovery tool.
Are you talking about a control point picking tool for two images or the tool that warps one image to match the other?

If the former, see pointpicker, at http://bigwww.epfl.ch/thevenaz/pointpicker/. I have used it an then filtered the results to make a file with just the points. But it works reasonably well and makes a text file.

With enough control points that are well distributed, a second or third order polynomial warp has been used often to warp satellite images to match a cartographic map. Other more sophisticated elastic warping techniques such as bUnwarpJ are also available, but more complex to implement, see http://biocomp.cnb.uam.es/~iarganda/bUnwarpJ/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: advanced convert usage?

Post by anthony »

I have never gotten point picker to work. But I was referring more to a program that find image registation points automatically.

Also 'polynomial' distortion is available, but only as a test distortion under development.
Basically I just never got around to verifying its behaviour, and in fact I think it is going wrong.

-distort Polynomial 'order control_point_pairs....'

The minimum number of control point pairs (4 numbers) depends on the order
  • affine 1 (3) u = c0 + c1*x + c2*y
  • bilinear 1.5 (4) u = '' + c3*x*y
  • quadratic 2 (6) u = '' + c4*x*x + c5*y*y
  • cubic 3 (10) u = '' + c6*x^3 + c7*x*x*y + c8*x*y*y + c9*y^3
  • quartic 4 (15) u = '' + c10*x^4 + ... + c14*y^4
  • quintic 5 (21) u = '' + c15*x^5 + ... + c20*y^5
The first number is the order, the second in parenthesis is the number of control points needed whcih is also equal to the number of coefficient terms the function requires. If more pairs than this is given they least squares fit, just as you would expect.

The function however is applied as a reverse map. As such -distort polynomial '1.5 ....' should be equivelent to a BilinearReverse distortion.

Currently 5 is the highest order provided, though even higher orders (requiring more control points) may be added.

At this time no 'polynomial projection' is provided.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: advanced convert usage?

Post by fmw42 »

I have never gotten point picker to work. But I was referring more to a program that find image registation points automatically.
That is a tough problem for totally disparate images such as maps and photos. Even for two photos, you have no idea where to look for any given control point, unless you do some first order (scale, rotate, skew) manually to get somewhere close.

The panoramic stitchers are much more well controlled as you have two very similar image areas and you know that the one overlaps only slightly with the other. They probably work by finding edge like features, making a small subsection for each and looking only in a restricted area using something like compare. (Best guess).

I know of no good tool for the general case.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: advanced convert usage?

Post by anthony »

Polynomial distorts are now working correctly (minor bug, once I managed to track it down)
-distort Polynomial '1 ....'
is the same as
-distort Affine '....'

simularly
-distort Polynomial '1.5 ....'
is the same as
-distort BilinearReverse '....'

after that your get quadratic and higher order fitted polynomial functions and progressively more control points are needed to fully define the function. least Squares best fit is applied if more than the minimum number of control points are provided.

Now to figure out some good higher order' examples. Such as 6 control point pairs for quadratic functions. or even 21 pairs for an order 5 function.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply