I need to know how to map a normal rectangular image into any user-created quadrilateral (none of the sides have to be parallel in a general quadrilateral). One website had info that has given me a good starting point http://alumni.media.mit.edu/~cwren/interpolator/ but it isn't really what I ultimately need. They give the formula:
NewX = (OldX * A + OldY * B + C) / (OldX * G + OldY * H + 1)
NewY = (OldX * D + OldY * E + F) / (OldX * G + OldY * H + 1)
But the problem with that is that it assumes I already know the values for the variables. I DON'T know the values of those variables. All I know are the X and Y coordinates of each of the 4 corner points of the destination quadrilateral, and the X and Y coordinates of each of the 4 corners of the original rectangular image. From the X and Y coordinates of these 8 points corner points (4 on the original image, and 4 on the destination quadrilateral), I must calculate the 8 variables used in this algorithm. Unfortunately the above mentionedt website did NOT provide an explanation on HOW to calculate the values of these 8 variables from the X and Y coordinates of the 4 corner points of the destination quadrilateral and 4 corner points of the original image.
Algebraically it SHOULD be possible. I have 8 points, each with an X and a Y coordinate. That's 16 known values. All I'm trying to do is to find 8 unknown values. According to the laws of algebra, to find N number of unknown values, you must have at least N number of known values. I have MORE than enough info to calculate the unknown values here using a system of Equations. I already have one equation, but all it does is use the 8 values I NEED to calculate, in order to convert between to coordinate systems. I need several OTHER equations to to FIND those 8 values that I need to use in the transformation equation. And unfortunately I have NO CLUE how to convert the 8 coordinate pairs into the 8 unknown variables which are to be used in the transformation equation.
I've gotten a START, via trial and error (not via anything I've found on the net, because there seems to be quite a LACK of info on the net when it comes to what I'm working on here), I managed to use 3 of the four points of my destination quadrilateral (as well as the values of width and height of my source image) to create an AFFINE transform (but not a perspective transform, as that will require all 4 points, not 3 points as used in an affine transform).
Below is the code I've gotten so far:
Now if someone could fill in the info on the 2 variables I've so far been unable to derive an equation to calculate (VarG and VarH), as well as any reworking of any of the other equations I'm using in there that may need to be refined to accommodate the use of the 4th point, PLEASE chip in and help. I'm really stuck here.
And yes it IS possible to do what I'm trying to do. Photoshop (even the cheap version Photoshop Elements) can do this transformation as shown in the image below that I created in Photoshop Elements.
![]()
Now I just would like to figure out HOW to do this in VB6, as there's many PRACTICAL applications for this (not just using it for cool effects in PSE), that would make it a useful tool in a practical VB6 program that I might write.
NewX = (OldX * A + OldY * B + C) / (OldX * G + OldY * H + 1)
NewY = (OldX * D + OldY * E + F) / (OldX * G + OldY * H + 1)
But the problem with that is that it assumes I already know the values for the variables. I DON'T know the values of those variables. All I know are the X and Y coordinates of each of the 4 corner points of the destination quadrilateral, and the X and Y coordinates of each of the 4 corners of the original rectangular image. From the X and Y coordinates of these 8 points corner points (4 on the original image, and 4 on the destination quadrilateral), I must calculate the 8 variables used in this algorithm. Unfortunately the above mentionedt website did NOT provide an explanation on HOW to calculate the values of these 8 variables from the X and Y coordinates of the 4 corner points of the destination quadrilateral and 4 corner points of the original image.
Algebraically it SHOULD be possible. I have 8 points, each with an X and a Y coordinate. That's 16 known values. All I'm trying to do is to find 8 unknown values. According to the laws of algebra, to find N number of unknown values, you must have at least N number of known values. I have MORE than enough info to calculate the unknown values here using a system of Equations. I already have one equation, but all it does is use the 8 values I NEED to calculate, in order to convert between to coordinate systems. I need several OTHER equations to to FIND those 8 values that I need to use in the transformation equation. And unfortunately I have NO CLUE how to convert the 8 coordinate pairs into the 8 unknown variables which are to be used in the transformation equation.
I've gotten a START, via trial and error (not via anything I've found on the net, because there seems to be quite a LACK of info on the net when it comes to what I'm working on here), I managed to use 3 of the four points of my destination quadrilateral (as well as the values of width and height of my source image) to create an AFFINE transform (but not a perspective transform, as that will require all 4 points, not 3 points as used in an affine transform).
Below is the code I've gotten so far:
Code:
Private Sub TransformCoords(ByVal X As Long, ByVal Y As Long, NewX As Long, NewY As Long, p1 As PointType, p2 As PointType, p3 As PointType, p4 As PointType)
Dim VarA As Double
Dim VarB As Double
Dim VarC As Double
Dim VarD As Double
Dim VarE As Double
Dim VarF As Double
Dim VarG As Double
Dim VarH As Double
'offset variables
VarC = p1.X
VarF = p1.Y
'scaling variables
VarA = (p2.X - p1.X) / Picture1.Width
VarE = (p3.Y - p1.Y) / Picture1.Height
'skew variables
VarB = (p3.X - p1.X) / (p3.Y - p1.Y) * VarE
VarD = (p2.Y - p1.Y) / (p2.X - p1.X) * VarA
'perspective variables
VarG = 0 'not sure how to calculate this from the 4 corner points of the quadrilateral I wish to transform into
VarH = 0 'not sure how to calculate this from the 4 corner points of the quadrilateral I wish to transform into
NewX = (X * VarA + Y * VarB + VarC) / (X * VarG + Y * VarH + 1)
NewY = (X * VarD + Y * VarE + VarF) / (X * VarG + Y * VarH + 1)
End Sub
Now if someone could fill in the info on the 2 variables I've so far been unable to derive an equation to calculate (VarG and VarH), as well as any reworking of any of the other equations I'm using in there that may need to be refined to accommodate the use of the 4th point, PLEASE chip in and help. I'm really stuck here.
And yes it IS possible to do what I'm trying to do. Photoshop (even the cheap version Photoshop Elements) can do this transformation as shown in the image below that I created in Photoshop Elements.

Now I just would like to figure out HOW to do this in VB6, as there's many PRACTICAL applications for this (not just using it for cool effects in PSE), that would make it a useful tool in a practical VB6 program that I might write.