Which kind of interpolation best for resizing image?

后端 未结 3 1921
故里飘歌
故里飘歌 2021-02-02 10:14

I have a numpy array that I wish to resize using opencv. Its values range from 0 to 255. If I opt to use cv2.INTER_CUBIC, I may get values outside this range. This is undesirabl

相关标签:
3条回答
  • 2021-02-02 10:43

    If you are enlarging the image, you should prefer to use INTER_LINEAR or INTER_CUBIC interpolation. If you are shrinking the image, you should prefer to use INTER_AREA interpolation.

    Cubic interpolation is computationally more complex, and hence slower than linear interpolation. However, the quality of the resulting image will be higher.

    0 讨论(0)
  • 2021-02-02 10:52

    To overcome such problem you should find out the new size of the given image where the interpolation can be made. And copy interpolated sampled image on the target image like:

    # create target image and copy sample image into it
    (wt, ht) = imgSize # target image size
    (h, w) = img.shape # given image size
    fx = w / wt
    fy = h / ht
    f = max(fx, fy)
    newSize = (max(min(wt, int(w / f)), 1),
               max(min(ht, int(h / f)), 1))  # scale according to f (result at least 1 and at most wt or ht)
    img = cv2.resize(img, newSize, interpolation=cv2.INTER_CUBIC) #INTER_CUBIC interpolation
    target = np.ones([ht, wt]) * 255  # shape=(64,800)
    target[0:newSize[1], 0:newSize[0]] = img
    

    Some of the possible interpolation in openCV are:

    • INTER_NEAREST – a nearest-neighbor interpolation
    • INTER_LINEAR – a bilinear interpolation (used by default)
    • INTER_AREA – resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
    • INTER_CUBIC – a bicubic interpolation over 4×4 pixel neighborhood
    • INTER_LANCZOS4 – a Lanczos interpolation over 8×8 pixel neighborhood

    See here for results in each interpolation.

    0 讨论(0)
  • 2021-02-02 10:58

    I think you should start with INTER_LINEAR which is the default option for resize() function. It combines sufficiently good visual results with sufficiently good time performance (although it is not as fast as INTER_NEAREST). And it won't create those out-of-range values.

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