#### Archived

This topic is now archived and is closed to further replies.

# interpolation^4

This topic is 5654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Waah! I''m going crazy on this! Imagine you have a square. Each corner represents a color (e.g. UpLeft=red, UpRight=green, LowerLeft=blue, LowerRight=yellow). Now you place a point somewhere in the quad to pick a color. This color is computed by the distance to each corner: The nearer the point is to a corner, the more influential this color is. So if you place the point in the top left corner you''ll get red. If you place it in the middle you''ll get an even mixture of all 4 colors. This concept sounds rather simple, but I don''t manage to compute the color. Of course I know the length of the square''s sides, the position of the point and the distance from the point to every edge, but that doesn''t help me much... any ideas?

##### Share on other sites
Well... If I am correct you can interpolate first on the y axis... and do that for both x1 (most left x) and x2 (most right x) and then use the colors at those positions for interpolation

interpolation itself goes like

x1 + alpha*(x2-x1)

so for example x1 = 2 and x2 = 4 and alpha = .3 which gives 2.6
you can of course do this for the y as well...

hope this has helped

##### Share on other sites
This is not as straight forward as it looks. For starters, using four colours red, green, blue and yellow doesn't help. Three of these are primary colours and the last is a mixture of red and green. This means that in your algorithm you must take into account that the red and green values have to be adjusted by a function of the yellow colour. This leaves something like,

Tr = (1 - Sr) + (1 - Sy)/2
Tg = (1 - Sg) + (1 - Sy)/2
Tb = (1 - Sb)

Where Sr, Sg, Sb and Sy are the red, green, blue and yellow components of the square respectively. T is the target point colour. Note that the components of S must be the normalised distance from the target point.

HTH

[edited by - IvanTheTerrible on April 1, 2003 12:13:26 PM]

##### Share on other sites
Ahm, yes... the color thing was just an example to make clearer what I mean. Actually there are no colors! ;-)

but nevertheless: thanks

##### Share on other sites
Dark described bilinear interpolation. It is one very viable solution, but it is NOT what you described.

You can easily get what you described, however, by using a weighted average, weighting by 1/r or 1/r2.

Example:

i = value of top right corner
j = value of top left corner
k = value of bottom right corner
l = value of bottom left corner

a = pythagorean distance from top right corner
b = pythagorean distance from top left corner
c = pythagorean distance from bottom right corner
d = pythagorean distance from bottom left corner

z = 1/a + 1/b + 1/c + 1/d

s = (i/a + j/b + k/c + l/d) * z

x = s/4 //Final value

I spelled everything out here to make it clear. I also did it quickly, so there might be a mistake. However, you should get what I''m trying to do here. I''m using simple 1/r weights. You may want to use 1/r2 weights; they''re common in nature (they also eliminate square roots). Whatever the case, this technique, as you can see, though still relatively simple, is also significantly slower than bilinear interpolation. I''ve also never actually seen it used. I''m just trying to give you the math that would describe exactly what you described in your first post. If all you want is same-old interpolation, though, just use bilinear. For better quality, some now use bicubic interpolation, which is still, AFAIK, dependant on the order of interpolation.

Even if you don''t decide to use the technique I gave you the math for, at least I will have given you some keywords to use with google.

##### Share on other sites
Bilinear is easiest if it''s what I think it is.

colour = (red * (1-x) + green * x) * (1-y) + (blue * (1-x) + yellow * x) * y
Where red is at 0,0 and yellow is at 1,1. This only works really nicely for rectangles. With other polygons there isn''t a simple way to do it.

##### Share on other sites
Thanks a lot for your ideas. I''ll check them out soon. They''ll surely do! :-)

##### Share on other sites
For beeing lazy I just tried this bilinear method. Here's the code:

    	int partLen = 256;					// Length of the cell (only one this time)	int intX = x/partLen;				// In which X-cell are we?	int fracX = x - intX*partLen;		// How many X-pixels are we into this cell?	int intY = y/partLen;				// In which Y-cell are we?	int fracY = y - intY*partLen;		// How many Y-pixels are we into this cell?	// the four colors	unsigned char pixel1 = 0;	// TopLeft	unsigned char pixel2 = 64;	// TopRight	unsigned char pixel3 = 128;	// BottomLeft	unsigned char pixel4 = 255;	// BottomRight	// Calculate fracX/Y to a value between 0 and 1	float fracX2 = (1.0f/(float)partLen)*(float)fracX;	float fracY2 = (1.0f/(float)partLen)*(float)fracY;	// Do this bilinear stuff	return 256*((pixel1*(1-fracX2) +pixel2*fracX2)*(1-fracY2) + (pixel3*(1-fracX2) +pixel4*fracX2)*fracY2);

But the result is not what I expected it to be... ;-) Take a look:

booyaa! That's hypnotic!
I thought rather of a smooth gradient... Well, I guess I'll have to take another look at my code...

[edited by - VanKurt on April 2, 2003 2:16:52 AM]

##### Share on other sites
i got a weird pattern like that when i was drawing lines from one side of the screen to the other and incrementing the y position on the left side by 1 and the y position on the right side by 5. it was crazy.

edit: maybe those numbers were in twips b/c that was in my old Visual Basic days

[edited by - nate0326 on April 2, 2003 11:11:02 AM]

##### Share on other sites
Try splitting the last line into several steps, make it less error prone and easier to see what is going on.

One thing I''ve noticed is that you are almost certainly getting massive overflow errors, hence the banding in your image. This is because your pixel values lie in the range 0-255, and you are multiplying them by 255 again at the end.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
13
5. 5

• 14
• 10
• 24
• 9
• 57
• ### Forum Statistics

• Total Topics
632639
• Total Posts
3007609
• ### Who's Online (See full list)

There are no registered users currently online

×