Jump to content
  • Advertisement
Sign in to follow this  
phase_v

Image resampling and resizing

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I have a question about resizing and resampling an image. I am currently working only in the x direction (scaling the width). My resampling function is as follows:
unsigned char* resampleX2(unsigned char* source, int newWidth, int oldWidth, int height) {
	int numPixels = int(newWidth * height * 4 + newWidth); // number of pixels in resized image
	unsigned char* output = new unsigned char[numPixels];  
	
	double deltaX = double(oldWidth /newWidth);    
	double radius = 2;  // filter radius
	double x0 = double(-0.5 +deltaX/2);  // first sample point (in original image)
	cubicFilter f;   // a mitchell-netravali cubic filter, takes in and spits out a double

	for(int i =0; i < numPixels - 1; i+=4) {    // iterate through all pixels, increment by 4 for rgba
		double x = x0 + i*deltaX;				// determine current sample point
		double sr = 0,sg=0,sb=0,sa=0;           // sum values for rgba - use for convolution
		for(double j = x - radius; j < x + radius; j++) {   // convolve
				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);
		}
		
		// set output image to new resized values
		output = sr;
		output[i+1] = sg;
		output[i+2] = sb;
		output[i+3] = sa;
	}

    return output;
}
The odd thing about it is that if I scale by half, I get perfect results. Anything besdies that (and 10% i think) gives me results that are either skewed or have major color issues. Any help would be appreciated. This is for a project due tomorrow and has been driving me crazy. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Well after a little debugging, it seems part of my problem is with overlapping 'j' values. In other words my filter is overlapping the pixels it convolves. (if that makes any sense) This thing is due in 6 hours and I'm starting to get desperate. If anyone has an idea, throw it out there.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Do your own homework.

Share this post


Link to post
Share on other sites
Lol, yeah I wish I could. I've been pounding away at this thing for too much of the day though, and thought I could at least see if anyone here (who happens to be up at this absurd hour) could offer some help. Anyway...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It's the WORLD wide web. It's 9:30am here.

Anyway, haven't looked at your code too much but I'd take a look at your numpixels calculation.

If your getting skewed images I'd imagine you are getting the wrong line width in there somewhere.

Personally I'd use two loops (for clarity), One for X and one for Y.

Share this post


Link to post
Share on other sites
Well I programmed it with two loops (iterating through rows and columns). Here is that function.

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/newWidth;
float x0 = -0.5 + deltaX/2;
cubicFilter f;

int i = 0,iPrime = 0; // i is result image index, iprime is source 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);
iPrime = index(col,row,oldWidth);
int x = x0 + iPrime * 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;

}


Also, since you mentioned it, my index function is as follows:
int index(int x, int y, int w) {
// returns an index for an rgba image
return (y*w+x)*4;
}

It is pretty basic, just converts from x,y to row major indicie. With this implementation, I get about the exact same result as before.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Take a look at what happens when your old width=100 and new =98.

Step though it with a debugger and look at the values of j, particularly at the beginning and end of each row.

What do you get when you index an array with an index of -2?

Share this post


Link to post
Share on other sites
Wild guess:

float deltaX = oldWidth/newWidth;

should be

float deltaX = oldWidth/(float)newWidth;

Share this post


Link to post
Share on other sites
I'll check it, but I really don't think it is the cause of my problem. Here is an image of what I get when I try to scale an image by 70%. Anyway, I might hit the sack for a couple of hours and then see if any amazing epiphany comes to me. If that image shines any light on what is going on let me know, otherwise thanks for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!