Bilinear interpolation function reversal
Surt

Is is mathematically possible to reverse the bilinear interpolation function? That is, finding the square coordinate from a point between the four points of a distorted square.

No need to do the math for me, I would rather do it my self, but I just want to know if it can be done, so I don't spend hours stressing my brain for nothing (I have allready spent quite a few on this problem).

Bob

Yes, if you have the original kernel matrix. Then, it's a simple matter of inverting the kernel matrix and reapplying it to the image.
If you don't have the original kernel, then you can try and "guess" what the original image looked like, but you'll never really get it back.

Zaphos

As a rule, if the result of some process could have been derived from more than one initial, the process is not reversible without extra information. It's a pretty simple rule, no?

Thomas Harte

Zaphos > Thats true in general, but you are being too simple. Look at what Thanatos knows in this case :
- the output data
- the family of functions applied to move from output to input
Therefore he can at least build up a probability density function on what the original data was, and eventually come up with the set of most likely bits of input data. In this case he doesn't even really need to do that.
There is a particularly nice property of a certain set of distorted squares (those where the sides remain straight, even if not parallel - e.g. 3d projections or linear mappings) that can be used here - if you look at where the two lines conecting pairs of opposite corners intersect, you have the centre of the square. Even if the square was distorted.
Whats more, if you then combine this information with the idea that if you take halfway along opposite sides and connect those with lines you end up with four 'sub squares', which map back to before the distortion in the way you would like.
Using the combination of these facts, if you can detect the edge of your distorted square you can map it back to a rigid 2d face on square (i.e. parallel sides, right angled corners, sides parallel to the axis).
Then the problem is removing the linear interpolation. Basically you are looking for 'turning points' in the rate of change of 'colour' to try and figure out the spacing of the original texels. You can do this by just considering directions of change, or you can do something realy clever like build a fourier series and differentiate that.
So, I'd say that the problem is solvable, but don't expect the computer to be able to get as much back as your imagination . . .

Zaphos

As in, "you can do it, sort of?" Yeah, I know that, kinda ... but that's not a true reversal, is it?

Thomas Harte

It can't be guaranteed to be a true reversal, but will be either entirely correct, visually correct (i.e. a few hues slightly off, but not to the human eye), or as correct as the output data allows (e.g. if a 3d projection doesn't end up including all the texels) . . . .

Thomas Harte

Also, there is the little matter of what exactly the original author means. I've shown that the answer to his question "[Is it possible to find] the square coordinate from a point between the four points of a distorted square." is exactly yes. I just don't see how a square distortion and the bilinear interpolation (which after all literall just means moving linearly simultaneously in two dimensions) are the same thing so I went of on all that about the bilinear interpolation of texture maps to remove hard texel edges . . .

Zaphos

Umm ... cool. My brain just exploded. Is that bad?

Yeah, sorry, you're over my head ...

Surt

Thank for your replies, but I don't think I explained myself very well.
Some of you seem to think I want to reverse bilinear image filtering, but this is not so, what I want is to convert mouse coordinates into coordinates in a 3d-iso world, I find the square the mouse is over using an iterative method, but I want to do a reverse bilinear-interpolation to find the mouse cordinate to sub-square accuracy. Since I know the square, I can get the screen and world cooridinates of each corner of the square, and I know the screen coordinates of the mouse position in the square, I want to convert this value to a world space. Is it possible, or are there too many variables?

Bob

Actually, I think there are betters ways of doing what you're trying to achieve. Firstly, you can project your mouse in your 3D world - this will give you a line though, not a point.
You can convert this line from camera-space to world space by multiplying the coordinates by the inverse of the transformation matrix.
Now that you have your line, I assume you want to be able to know which object the mouse is over. You can do a simply line-to-mesh check, and out of those that pass the test, pick the object closest to the camera. This will tell you where the user put his mouse.
You can't though go from 2D to 3D and get an exact answer, simply because when you go from 2D to 3D, you add a new dimention, so there can be infinitely many positions in 3D for a projected point from 2D.

Gabhonga

well, you can easily go from 2d to 3d, but you'll have a line, and no point then...
(for handling mouse in a 3d environment I use a combination of backprojection (find the inverse of your projection function) and raytracing (to see what objects lie on the line from the eye position to the dot in infinity I clicked my mouse on))
----
multiple parentheses suck!

Surt

Sorry, again I should have explained better.
I want to convert the mouse point to a 2-d location on a heightmap (like in Simcity 2000 or Transport Tycoon), so I can get the square it's on, but I want sub-square accuracy.

Bob

How is your map displayed? Using a 3d engine (polygons) or a 2d engine (tiles)? If it's 2d, then it's trivial - You simply reverse the equaion used to draw tiles. I can give more details if I know how you draw the tiles though.
If it's 3d, then it needs a little more trickery. You basically find the line where the user clicked on (as said above), then you take the intersection of that line to the heightfield. The point on the heightfield will then be where the user clicked on. Now, you want to convert this back into a location on your height map. You can just reverse the computations you did to render the height map in the first place. It could be as simple as dividing by some amount, but can be more difficult, depending on how teh height field was generated.
Oh yeah, I still don't get the link in between this and bilinear filtering...

Surt

Not bilinear filtering, bilinear interpolation.
I've got the screen coordinates of the four corners of the deformed square, and the screen coordinates of the mouse.
I want to find the 2-d in-square coordinate of the mouse pointer over the deformed square.
If it is possible to reverse the bilinear interpolation function, then I could use that.
The graphics are prerasterised 3d like in Simcity 2000, Transport Tycoon, and Populous and Populous 2.

Thread #166207. Printed from Allegro.cc