原文地址https://medium.com/itberrios6/how-to-make-a-test-image-in-python-1a6c2d41b6ab学习如何制作测试图像在计算机视觉和图像处理中创建测试图像以更好地了解算法或滤波器将如何执行通常是有用的。测试图像是一个基准可以将多种算法相互比较。让我们开始吧代码位于GitHub上。简单测试图像通过skimage可以轻松获得许多测试图像但它们通常不适合描述算法和滤波器。相反我们的目标是描述更简单的图像上的算法我们将重点使用numpy和opencv创建旋转对称的测试图像。首先我们将制作一个简单的测试图像然后我们将使用正弦函数制作一个更复杂的图像。import numpy as np import cv2 import matplotlib.pyplot as plt %matplotlib inline # size of NxN image n 512 # get test image of a circle test_image np.zeros((n, n)) cv2.circle(test_image, center(n//2, n//2), radiusn//3, color(255,), thickness-1) # OPTIONAL: blurr test image cv2.GaussianBlur(test_image, ksize(9,9), sigmaX11, dsttest_image) # 0-1 normalize and cast to float32 cv2.normalize(test_image, test_image, 0, 1, cv2.NORM_MINMAX, dtypecv2.CV_32FC1) # display plt.imshow(test_image) plt.title(Test Image);Figure 1.在暗背景上测试一个圆的图像你可能想知道为什么我们要使用模糊原因是让圆的边界逐渐归零而不是粗略地下降。Figure 2.简单测试图像的截面。你可以使用opencv的形状函数尝试更简单的测试图像现在让我们实现一个更复杂的测试图像。复杂测试图像这是一个快速的复杂测试函数我们将使用正弦函数创建一个半径为r的圆对称波纹函数。其中半径定义为到原点的距离。def f(r): r - is the Radius from the center outputs r.copy().astype(np.float32) index (r 56) (r 256) outputs[r 56] 127 outputs[index] 127*np.sin(r[index]/np.pi) outputs[r 256] 127 return outputs # get images indexes as column vectors x_index, y_index np.meshgrid(np.arange(0, N), np.arange(0, N)) x_index x_index.reshape((-1, 1)) y_index y_index.reshape((-1, 1)) # get radius r r np.round(np.sqrt((x_index - N//2)**2 (y_index - N//2)**2)).astype(int).squeeze() test_image f(r).reshape((N, N))Figure 3. 正弦测试图像Figure 4. 正弦图像的横截面简单的正弦测试图像为我们提供了更有趣的特征但实际上我们可以生成更鲁棒的东西。代码如下:g lambda r : np.sin(((112*np.pi)/(np.log(2))*((2**(-r/56))) - 2**(-256/56))) def f(r): r - is the Radius from the center outputs r.copy() index_1 (r 56) (r 64) index_2 (r 64) (r 224) index_3 (r 224) (r 256) outputs[r 56] 127 outputs[index_1] 127*(1 (g(r[index_1]) * np.cos((np.pi*r[index_1]/16) - 4*np.pi)**2)) outputs[index_2] 127*(1 g(r[index_2])) outputs[index_3] 127*(1 (g(r[index_3]) * np.sin((np.pi*r[index_3]/64) - 4*np.pi)**2)) outputs[r 256] 127 return outputs def get_test_image(N): Obtains an NxN test image # get image indexes as column vectors x_index, y_index np.meshgrid(np.arange(0, N), np.arange(0, N)) x_index x_index.reshape((-1, 1)) y_index y_index.reshape((-1, 1)) # get radius r r np.round(np.sqrt((x_index - N//2)**2 (y_index - N//2)**2)).astype(np.float32).squeeze() return f(r).reshape((N, N)) # get size n 512 # get sinusoidal test image test_image get_test_image(n).astype(np.float32) # OPTIONAL blurr test image # cv2.GaussianBlur(test_image, ksize(5,5), sigmaX3, dsttest_image) # 0-1 normalize cv2.normalize(test_image, test_image, 0, 1, cv2.NORM_MINMAX, dtypecv2.CV_32FC1) # display plt.imshow(test_image) plt.title(Test Image);Figure 5.复正弦测试图像Figure 6. Cross Section of Complex Sinusoidal Test Image.现在我们有了一个具有不同波峰和波谷的测试图像用于比较我们的算法或滤波器的性能。这些并不是随机的峰值和低谷它们代表了图像中受控的频率变化。中心平坦无频率变化(也称为直流)。当我们从中心出去时我们参与了一个非常高的频率区域在频率上逐渐减少一直到边缘。为什么控制频率变化很重要?它允许我们测量滤波器的一般频率响应而无需明确进入频域。具有已知频率变化的图像允许我们在不转换到频率空间的情况下测量频率响应除了正弦函数f和g我们还可以使用模糊来控制峰值和谷值的强度。希望这能让你对创建自己的测试函数有一些了解以便将来使用。References[1] Granlund, G. H., Knutsson, H. (2011).Signal Processing for Computer Vision. Springer.