# 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.

## 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 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 on other sites
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 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 on other sites
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 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 on other sites
Actually, I lied. My second implementation juts cuts off the side of the image, performing no scaling...

##### Share on other sites
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 on other sites
Wild guess:
	float deltaX = oldWidth/newWidth;

should be
	float deltaX = oldWidth/(float)newWidth;

##### 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.

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

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009803
• ### Who's Online (See full list)

There are no registered users currently online

×