Page 1 of 1

Calculate perspective matrix from GIMP data

Posted: 2016-07-23T09:27:18-07:00
by artouiros
Hello, I am trying to use -distort Perspective and I was thinking it worksthe same way as in gimp. So I tryed to manualy change perspective in GIMP and got this:
Image
But I guess IM -distort Perspective uses other way to calculate perspective, because when using gimp matrix it doesn't work.

I am using this to fill one image on top of another in specific area (the area is not simetric)
convert bg.jpg ( overlayimage.jpg -distort Perspective "1.28116, -0.29417, -112.92397, 0.26620, 1.17435, -385.33134,
0.00002, -0.00005, 0.99882" ) -composite result.jpg

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-23T10:25:49-07:00
by Bonzo
You do not have enough numbers as you need an x,y point for each start point and each end point.

I have a javascript/imagemagick interactive tool here, but the file size is limited: http://www.rubbleimages.com/Distort.php

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-23T10:38:20-07:00
by fmw42
You need to use -disort Perspective-Projection not -distort perspective. See http://www.imagemagick.org/Usage/distor ... projection. And properly you need to normalize your coefficients so that the last one is identically 1. But your coefficients are probably close enough to get a reasonable fit.

It is possible that your matrix needs to be inverted. I do not know which direction IM and GIMP present/supply the matrix.

It might be easier to just get the control points used from GIMP and provide them to -distort perspective. See http://www.imagemagick.org/Usage/distorts/#perspective and http://www.imagemagick.org/Usage/distor ... rol_points

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T03:24:31-07:00
by artouiros
fmw42 wrote:You need to use -disort Perspective-Projection not -distort perspective. See http://www.imagemagick.org/Usage/distor ... projection. And properly you need to normalize your coefficients so that the last one is identically 1. But your coefficients are probably close enough to get a reasonable fit.

It is possible that your matrix needs to be inverted. I do not know which direction IM and GIMP present/supply the matrix.

It might be easier to just get the control points used from GIMP and provide them to -distort perspective. See http://www.imagemagick.org/Usage/distorts/#perspective and http://www.imagemagick.org/Usage/distor ... rol_points
Thanks for reply, I've tried the perspective-projection. And I guess it's not what I want to achieve. I've tried inverting matrix, tried using +-disort. Also tried to manualy enter the start/end coordinates according thos this:

Code: Select all

Sx1,Sy1 Dx1,Dy1   Sx2,Sy2 Dx2,Dy2   Sx3,Sy3 Dx3,Dy3   ...   Sxn,Syn Dxn,Dyn  
I got coordinates from every corner of the image and still didn't get what I expected.
in -distort but still no luck. I attached a video of what I want. It's a 10 seconds video showing GIMP window. - https://www.youtube.com/watch?v=HaWFsP5s3g0
The picture with roses is overlay image, the other is background. In this particular example I could use resize with ignoring aspect ration and rotate the overlay iamge, but it's only in this particulat example. SO I need the exact way to change perspective as gimp does

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T10:19:51-07:00
by fmw42
If you post your two input images, I can process it and show you the command line. What version of Imagemagick are you using and what platform. Syntax for distort control points changed quite a while ago. So if you are using an ancient version of Imagemagick, then the control point syntax may be different. You post to some place such as dropbox.com and put the URLs here

Please always provide your IM version and platform, since syntax may differ.

Try the following:

Unix syntax

Code: Select all

convert backgroundimage \
\( foregroundimage -virtual-pixel none +distort perspective \
"Sx1,Sy1 Dx1,Dy1   Sx2,Sy2 Dx2,Dy2   Sx3,Sy3 Dx3,Dy3  Sx4,Sy4 Dx4,Dy4" \) \
-layers merge +repage resultimage
Windows syntax

Code: Select all

convert backgroundimage ^
( foregroundimage -virtual-pixel none +distort perspective ^
"Sx1,Sy1 Dx1,Dy1   Sx2,Sy2 Dx2,Dy2   Sx3,Sy3 Dx3,Dy3  Sx4,Sy4 Dx4,Dy4" ) ^
-layers merge +repage resultimage

where S are the 4 corners x,y in foreground image and D are the corners of your box shape in the background image.

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T11:19:41-07:00
by artouiros
fmw42 wrote:If you post your two input images, I can process it and show you the command line. What version of Imagemagick are you using and what platform. Syntax for distort control points changed quite a while ago. So if you are using an ancient version of Imagemagick, then the control point syntax may be different. You post to some place such as dropbox.com and put the URLs here

Please always provide your IM version and platform, since syntax may differ.
Thanks for reply. I am using the latest IM version 7.0.2-5-Q16-x64 for windows. I tried what you suggested, it seemed simple, but it doesn't work for some case.
1. here is download link for background https://yadi.sk/i/kaCIs2V1taeAF
2. here is download link for foreground https://yadi.sk/i/urYccDzFtaeAD

I made everything as you wrote, here is my command:

Code: Select all

magick convert background.png ^( foreground.jpg +distort perspective "0,0 874,240  322,0 1286,338  0,572 732,948  322,572 1150,1026" )^ -layers merge +repage resultimage.png

Code: Select all

0,0 is the top left corner of the foreground image
874,240 is the top left corner of the red box area

322,0 is the top right corner of the foreground image
1286,338 is the top right corner of the red box area

0,572 is the bottom left corner of foreground image
732,948 is the bottom left corner of the red box area

322,572 is the bottom right corner of the foreground image
1150,1026 is the bottom right corner of the red box area
Note: The coordinates of the red box area are a bit inaccurate, I quickly grabbed them in gimp for testing purposes.(but I guess this doesn't matter because the result is much more different then coornitates inaccuracy.
The problem: Result image. See below
Image

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T11:30:46-07:00
by artouiros
Managed to see your edited code! the

Code: Select all

-virtual-pixel none
made the trick! Thanks you very much! But how do I place the foreground image under the background? I was using Dst_Over in -compose but it seems you way needs another function

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T11:31:51-07:00
by fmw42
magick convert background.png ^( foreground.jpg +distort perspective "0,0 874,240 322,0 1286,338 0,572 732,948 322,572 1150,1026" )^ -layers merge +repage resultimage.png
I did not write this as a single line. The ^ are new line characters and need a space before and then go to a new line. Try again with my code exactly as written with new lines after the ^.

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T11:37:40-07:00
by fmw42
It should not matter which is front or back if both your images have transparent backgrounds and your coordinates align closely. So try this with -background none added.

Code: Select all

magick convert background.png ^
( foreground.jpg -background none -virtual-pixel none ^
+distort perspective "0,0 874,240  322,0 1286,338  0,572 732,948  322,572 1150,1026" ) ^
-layers merge +repage resultimage.png
However add +swap to reverse the order of the images, if you prefer.

Code: Select all

magick convert background.png ^
( foreground.jpg -background none -virtual-pixel none ^
+distort perspective "0,0 874,240  322,0 1286,338  0,572 732,948  322,572 1150,1026" ) ^
+swap -layers merge +repage resultimage.png

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T11:41:50-07:00
by artouiros
fmw42 wrote:It should not matter which is front or back if both your images have transparent backgrounds and your coordinates align closely. So try this with -background none added.

Code: Select all

magick convert background.png ^
( foreground.jpg -background none -virtual-pixel none ^
+distort perspective "0,0 874,240  322,0 1286,338  0,572 732,948  322,572 1150,1026" ) ^
-layers merge +repage resultimage.png
However add +swap to reverse the order of the images, if you prefer.

Code: Select all

magick convert background.png ^
( foreground.jpg -background none -virtual-pixel none ^
+distort perspective "0,0 874,240  322,0 1286,338  0,572 732,948  322,572 1150,1026" ) ^
+swap -layers merge +repage resultimage.png
You are the best guy I've seen in internet, thanks you very much. I've learned something today.

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T11:44:22-07:00
by fmw42

Re: Calculate perspective matrix from GIMP data

Posted: 2016-07-24T16:08:05-07:00
by fmw42
Out of curiosity, does this work:

Code: Select all

convert bg.jpg ( overlayimage.jpg -background none -virtual-pixel none +distort Perspective-Projection "1.28116, -0.29417, -112.92397, 0.26620, 1.17435, -385.33134,0.00002, -0.00005, 0.99882" ) -layers merge +repage result.jpg