Image scaling and rotating in C/C++

前端 未结 10 1635
情深已故
情深已故 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:18

    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.

提交回复
热议问题