Calculate perspective matrix from GIMP data
Calculate perspective matrix from GIMP data
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:
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
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
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
I have a javascript/imagemagick interactive tool here, but the file size is limited: http://www.rubbleimages.com/Distort.php
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Calculate perspective matrix from GIMP data
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
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
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: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
Code: Select all
Sx1,Sy1 Dx1,Dy1 Sx2,Sy2 Dx2,Dy2 Sx3,Sy3 Dx3,Dy3 ... Sxn,Syn Dxn,Dyn
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Calculate perspective matrix from GIMP data
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
Windows syntax
where S are the 4 corners x,y in foreground image and D are the corners of your box shape in the background image.
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
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
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.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.
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
The problem: Result image. See below
Re: Calculate perspective matrix from GIMP data
Managed to see your edited code! the 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
Code: Select all
-virtual-pixel none
Last edited by artouiros on 2016-07-24T11:32:48-07:00, edited 1 time in total.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Calculate perspective matrix from GIMP data
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 ^.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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Calculate perspective matrix from GIMP data
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.
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" ) ^
-layers merge +repage resultimage.png
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
You are the best guy I've seen in internet, thanks you very much. I've learned something today.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.
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" ) ^ -layers merge +repage resultimage.png
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Calculate perspective matrix from GIMP data
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Calculate perspective matrix from GIMP data
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