具有非整数缩放功能的OpenCV双线性降采样

时间:2019-04-25 21:09:08

标签: opencv image-processing computer-vision bilinear-interpolation

在确定OpenCV用于非线性缩放因子的图像下采样的算法/实现时,我需要帮助。

我知道这个问题已经问过几次了,但是大多数答案似乎都不符合OpenCV的实现(例如,使用OpenCV时,此答案不正确:https://math.stackexchange.com/questions/48903/2d-array-downsampling-and-upsampling-using-bilinear-interpolation)。

最小问题表述:

我想使用双线性插值将4x4分辨率的图像降采样为3x3分辨率的图像。我对插值系数感兴趣。

python示例:

img = np.asarray([[ 1,  2,  3,  4],
                  [ 5,  6,  7,  8],
                  [ 9, 10, 11, 12],
                  [13, 14, 15, 16]]).astype(np.float32)

img_resized = cv2.resize(img, (3, 3), 0, 0, cv2.INTER_LINEAR).astype(np.float32)

print(img)
# [[ 1.  2.  3.  4.]
#  [ 5.  6.  7.  8.]
#  [ 9. 10. 11. 12.]
#  [13. 14. 15. 16.]]

print(img_resized)
# [[ 1.8333333  3.1666667  4.5      ]
#  [ 7.166667   8.5        9.833333 ]
#  [12.5       13.833333  15.166666 ]] 

插值系数:

经过反复试验,我弄清楚了OpenCV用于此特定情况的插值系数。

对于3x3图像的拐角点:

 1.8333333 = 25/36 *  1 + 5/36 *  2 + 5/36 *  5 + 1/36 *  6
 4.5000000 = 25/36 *  4 + 5/36 *  3 + 5/36 *  8 + 1/36 *  7
12.5000000 = 25/36 * 13 + 5/36 *  9 + 5/36 * 14 + 1/36 * 10
15.1666666 = 25/36 * 16 + 5/36 * 15 + 5/36 * 12 + 1/36 * 11

对于3x3图像的中间点:

8.5 = 1/4 * 6 + 1/4 * 7 + 1/4 * 10 + 1/4 * 11

对于3x3图像的其余4个点:

 3.1666667 = 5/12 *  2 + 5/12 *  3 + 1/12 *  6 + 1/12 *  7
 7.1666667 = 5/12 *  5 + 5/12 *  9 + 1/12 *  6 + 1/12 * 10
 9.8333333 = 5/12 *  8 + 5/12 * 12 + 1/12 *  7 + 1/12 * 11
13.833333  = 5/12 * 14 + 5/12 * 15 + 1/12 * 10 + 1/12 * 11

问题:

有人可以帮助我理解这些插值系数吗?如何计算?我试图阅读cv :: resize()函数的源代码,但它对我没有太大帮助:S

1 个答案:

答案 0 :(得分:1)

在研究了各种测试用例之后,我想我知道OpenCV如何选择采样点位置的答案。正如@ChrisLuengo在评论中指出的那样,OpenCV似乎没有在下采样之前应用低通滤波器,而是仅使用(双)线性插值。

(可能)解决方案:

让我们假设有一张5x5的图像,其像素位置在下图中用蓝色圆圈表示。现在,我们要将其降采样为3x3或4x4图像,并且需要在原始图像网格中找到新的降采样图像的采样位置。

似乎OpenCV对原始图像网格使用了1的像素距离,对新图像网格使用了(OLD_SIZE / NEW_SIZE)的像素距离,因此这里是5/3和5/4。而且,它将两个网格都对准中心点。因此,OpenCV的确定性采样算法可以如下所示:

可视化5x5至3x3

Sampling points when downsampling a 5x5 image to a 3x3 image using OpenCv resize with bilinear interpolation

可视化5x5至4x4

Sampling points when downsampling a 5x5 image to a 4x4 image using OpenCv resize with bilinear interpolation

示例代码(Python 2.7):

import numpy as np
import cv2


# 1. H_W is the height & width of the original image, using uniform H/W for this example
#    resized_H_W is the height & width of the resized image, using uniform H/W for this example

H_W = 5
resized_H_W = 4



# 2. Create original image & Get OpenCV resized image:

img = np.zeros((H_W, H_W)).astype(np.float32)

counter = 1

for i in range(0, H_W):
    for j in range(0, H_W):
        img[i, j] = counter
        counter += 1

img_resized_opencv = cv2.resize(img, (resized_H_W, resized_H_W), 0, 0, cv2.INTER_LINEAR).astype(np.float32)



# 3. Get own resized image:

img_resized_own = np.zeros((resized_H_W, resized_H_W)).astype(np.float32)

for i in range(0, resized_H_W):
    for j in range(0, resized_H_W):
        sample_x = (1.0 * H_W) / 2.0 - 0.50 + (i - (1.0 * resized_H_W - 1.0) / 2.0) * (1.0 * H_W) / (1.0 * resized_H_W)
        sample_y = (1.0 * H_W) / 2.0 - 0.50 + (j - (1.0 * resized_H_W - 1.0) / 2.0) * (1.0 * H_W) / (1.0 * resized_H_W)

        pixel_top_left  = img[int(np.floor(sample_x)), int(np.floor(sample_y))]
        pixel_top_right = img[int(np.floor(sample_x)), int(np.ceil(sample_y))]
        pixel_bot_left  = img[int(np.ceil(sample_x)),  int(np.floor(sample_y))]
        pixel_bot_right = img[int(np.ceil(sample_x)),  int(np.ceil(sample_y))]

        img_resized_own[i, j] = (1.0 - (sample_x - np.floor(sample_x))) * (1.0 - (sample_y - np.floor(sample_y))) * pixel_top_left  + \
                                (1.0 - (sample_x - np.floor(sample_x))) * (sample_y - np.floor(sample_y))         * pixel_top_right + \
                                (sample_x - np.floor(sample_x))         * (1.0 - (sample_y - np.floor(sample_y))) * pixel_bot_left  + \
                                (sample_x - np.floor(sample_x))         * (sample_y - np.floor(sample_y))         * pixel_bot_right



# 4. Print results:

print "\n"
print "Org. image: \n", img
print "\n"
print "Resized image (OpenCV): \n", img_resized_opencv
print "\n"
print "Resized image (own): \n", img_resized_own
print "\n"
print "MSE between OpenCV <-> Own: ", np.mean(np.square(img_resized_opencv - img_resized_own))
print "\n"

免责声明:

这只是我通过大约10个测试用例进行测试的理论。我并不是说这是100%正确。