Image scaling and rotating in C/C++

前端 未结 10 1613
情深已故
情深已故 2020-11-29 07:55

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 fro

相关标签:
10条回答
  • 2020-11-29 08:22

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

    0 讨论(0)
  • 2020-11-29 08:23

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

    0 讨论(0)
  • 2020-11-29 08:28

    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.

    0 讨论(0)
  • 2020-11-29 08:30

    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.

    0 讨论(0)
提交回复
热议问题