Image resampling and resizing

Started by
33 comments, last by phase_v 18 years, 6 months ago
Wow...that helped a lot, although my color values are still screwy. I updated the image to show my new results. Guess I'll be awake. And thanks sam.
Progress does not consist in replacing a theory that is wrong with one that is right. It consists in replacing a theory that is wrong with one that is more subtly wrong.- Hawkins' Theory of Progress
Advertisement
How does your filter function work? Does it return 1.0 when passed 0.0?

If so, then the sum of the filtered values over your 5 pixel range may be overflowing your byte range.

You would need to scale your final value to fit. I.e. sr/=sum(f.compute(-2.0 to +2.0));
It also looks like you unintentionally "scale" y because you calculate the index before scaling:

old_index=scale(x)*old_width+y <--- what you want
old_index=scale(x*old_width+y) <--- what you do
___________________________Buggrit, millennium hand and shrimp!
When passed 0, my filter returns 0.888 repeating. IT is an implementation of the mitchell-netravali filter (if that helps at all). sam I'm not quite sure I understand your lates post. If you are referring to my calculation of iPrime, I no longer do that. I changed my index function to reflect your post, but still get the same result. Here is my most recent code:

int index(int x, int y, int w) {	// returns an index for an rgba image	return ((y)*w+x)*4;}unsigned char* resampleX(unsigned char* source, int newWidth, int oldWidth, int height) {	unsigned char* result = new unsigned char[newWidth*height*4];	int radius = 2;	float deltaX = oldWidth/(float)newWidth;	float x0 = -0.5 + deltaX/2;	cubicFilter f;	int i = 0;   // i is result image index	for(int row = 0; row < height; row++) {		for(int col = 0; col < newWidth; col++) {			float sr = 0,sg = 0,sb = 0,sa = 0;			i = index(col,row,newWidth);			int x = x0 + i * deltaX;			for(int j = x - radius; j < x+radius; j++) {				sr += source[(int)j] * f.compute(x-j);				sg += source[(int)j+1] * f.compute(x-j);				sb += source[(int)j+2] * f.compute(x-j);				sa += source[(int)j+3] * f.compute(x-j);			}			result = sr;			result[i+1] = sg;			result[i+2] = sb;			result[i+3] = sa;		}	}	return result;}


And thanks for your help guys, I would've gone nuts over this.
Progress does not consist in replacing a theory that is wrong with one that is right. It consists in replacing a theory that is wrong with one that is more subtly wrong.- Hawkins' Theory of Progress
As an aside, if your submitting this as a marked assignment, I'd tidy your code up to make it more readable. If I was marking it I'd have to mark you down for style.

Make your variable names more descriptive. e.g. Instead of sr, use fSource_Red.
Instead of i and iPrime, use iSourceIndex and iDestIndex.

If you used a basic reverse polish notation as above (i for int, f for float) it may make some of your code errors stand out much more clearly.

Personally, I don't like seeing multiple variables declared on one line, particularly if they are also initialised as well. Put them on separate lines.

If you are going to comment your code, make sure your comments are correct.
The comment - '// set output image to new resized values' would make me believe that you are doing something like setting the resolution of the image rather than writing the new pixel colour.
Yeah, I know, currently I am in "get it to work and screw readability" mode though. Hopefully I'll have some time to polish it before I submit it.
Progress does not consist in replacing a theory that is wrong with one that is right. It consists in replacing a theory that is wrong with one that is more subtly wrong.- Hawkins' Theory of Progress
Oh yes, another point,

float x0 = -0.5 + deltaX/2;

should really be

float x0 = -0.5f + deltaX/2.0f;

0.5 would be interpreted as a double and would have to be cast to a float by the compiler as a float. A non-optimising compiler would add in code to do this conversion producing slower larger code.

The similar thing would happen with 2. It would be interpreted by the compiler as (int)2. The compiler would then have to insert the code to convert an int 2 to floating point 2.0f.
Quote:Original post by phase_v
Yeah, I know, currently I am in "get it to work and screw readability" mode though. Hopefully I'll have some time to polish it before I submit it.


That's a bad way to write code. If you write it correctly the first time you will save yourself a lot of time by avoiding writing errors, and save your hair when it comes to debugging.

I know you are learning, but it's best to learn the right way to do things now before the bad habits become your normal way of working.

Read the book 'Code Complete'. Go to Google and [google]code smells.

Good luck with the assignment and the rest of your course.
Thanks for the tips, and I'll check out that book. Considering my color problem, though, it seems to only occur in ceartain parts of the image. This can best be seen when I scale it down to about 1/3 the original size. Here is an image. I feel like this should indicate something, but my brain just isn't functioning (5am and all that)
Progress does not consist in replacing a theory that is wrong with one that is right. It consists in replacing a theory that is wrong with one that is more subtly wrong.- Hawkins' Theory of Progress
Quote:Original post by phase_v
Thanks for the tips, and I'll check out that book. Considering my color problem, though, it seems to only occur in ceartain parts of the image. This can best be seen when I scale it down to about 1/3 the original size. Here is an image. I feel like this should indicate something, but my brain just isn't functioning (5am and all that)


sr += source[(int)j] * f.compute(x-j);
sg += source[(int)j+1] * f.compute(x-j);
sb += source[(int)j+2] * f.compute(x-j);
sa += source[(int)j+3] * f.compute(x-j);

should be

sr += source[(int)(j*4)] * f.compute(x-j);
sg += source[(int)(j*4)+1] * f.compute(x-j);
sb += source[(int)(j*4)+2] * f.compute(x-j);
sa += source[(int)(j*4)+3] * f.compute(x-j);

This topic is closed to new replies.

Advertisement