views:

5936

answers:

9

What is the best way to scale a 2D image array? For instance, suppose I have an image of 1024 x 2048 bytes, with each byte being a pixel. Each pixel is a grayscale level from 0 to 255. I would like to be able to scale this image by an arbitrary factor and get a new image. So, if I scale the image by a factor of 0.68, I should get a new image of size 0.68*1024 x 0.68*2048. some pixels will be collapsed onto each other. And, if I scale by a factor of say 3.15, I would get a larger image with pixels being duplicated. So, what's the best way to accomplish this?

Next, I would like to be able to rotate an image by an arbitrary angle, in the range of 0 to 360 degrees (0 - 2Pi). Cropping of the image after rotating isn't an issue. What would be the best way to do this?

+4  A: 

Do you want to do the dirty work yourself or can ImageMagick do it for you?

divideandconquer.se
+6  A: 

There is no "simple" way of acommplish that. Both scaling and rotating are not "trivial" procesis.

Google it for a 2d imaging library. Magick++ can be an idea as divideandconquer.se points, but there are others.

pabloh84
+6  A: 

There are many ways to scale and rotate images. The simplest way to scale is:

dest[dx,dy] = src[dx*src_width/dest_width,dy*src_height/dest_height]

but this produces blocky effects when increasing the size and loss of detail when reducing the size. There are ways to produce better looking results, for example, bilinear filtering.

For rotating, the src pixel location can be calculated using a rotation matrix:

sx,sy = M(dx,dy)

where M is a matrix that maps destination pixels to the source image. Again, you'll need to do interpolation to produce non-blocky results.

But there are plenty of libraries available if you don't want to get into the mathematics of image processing.

Skizz

Skizz
+1  A: 

Duplicating or discarding pixels is not the best method or image resizing, as the results will show pixelation and jagginess. For the best results, you should resample the image, which will give the resulting image a much smoother look. There are lots of methods for resampling, like bilinear, bicubic, lanczos etc.

Take a look at the http://ftp.osuosl.org/pub/nslu2/sources/wxWidgets-2.8.0.tar.bz2&cs_f=wxWidgets-2.8.0/src/common/image.cpp#l627">BicubicResample function from wxWidgets. It works will all kinds of images, not only greyscale, but you should be able to adapt it to your needs. Then there's also resampling code http://www.makayama.com/avimerger.zip&cs_f=sources/VirtualDub/Kasumi/source/resample.cpp">from VirtualDub. Google Codesearch may reveal more related code.

EDIT: the links look fine in the preview, but are broken when posted. This is strange. Go to google codesearch and query for "wxwidgets resamplebicubic" and "virtualdub resample" respectively to get the same results.

+1  A: 

What you're doing is mapping a set of input points to a set of output points. The first part of the problem is to determine the mapping for your resizing or rotation; the second part is to handle points that don't lie exactly on a pixel boundary.

Mapping for a resize is easy:

x' = x * (width' / width)
y' = y * (height' / height)

Mapping for rotation is only a little bit harder.

x' = x * cos(a) + y * sin(a)
y' = y * cos(a) - x * sin(a)

The technique for determining the value of pixels that lie off the grid is called interpolation. There are many such algorithms, ranging widely in speed and final image quality. A few of them in increasing order of quality/time are nearest neighbor, bilinear, bicubic, and Sinc filter.

Mark Ransom
+1  A: 

CxImage is a free library for handling images, which can do what you want. I haven't personally used it except for trivial stuff, but I've seen it recommended repeatedly.

peterchen
A: 

In the earlier posting, it said 'There are many ways to scale and rotate images. The simplest way to scale is:

dest[dx,dy] = src[dx*src_width/dest_width,dy*src_height/dest_height] but this produces blocky effects when increasing the size and loss of detail when reducing the size. There are ways to produce better looking results, for example, bilinear filtering.'

My question is i have an array of pixel values. And I want to scale DOWN the data before I convert to PNG. Can I do the above with lossing quality?

Thank you.

lucius
If you have a question, you shouldn't be writing it in an answer.
Michael Myers
A: 

point scaling(point p,float sx,float sy) { point s; int c[1][3]; int a[1][3]={p.x,p.y,1}; int b[3][3]={sx,0,0,0,sy,0,0,0,1}; multmat(a,b,c); s.x=c[0][0]; s.y=c[0][1]; return s; }

A: 

CxImage resizing methods produce strange result. I used Resample and Resample2 functions with all available variations of interpolation methods with same result. For example, try to resize 1024 x 768 image filled with white color to size 802 x 582. You'll find that there are pixels on the image that have color different to white! You can check this: open resized image in Windows Paint and try to fill it with black color. Result will surely amuse you.