Abi*_*n K 22 python opencv wolfram-mathematica computer-vision
这个问题与这个问题有关: How to remove convexity defects in sudoku square
我是想实现nikie's answer在Mathematica to OpenCV-Python.但我陷入了程序的最后一步.
即我得到了正方形的所有交叉点,如下所示:

现在,我想将其转换为一个完美的大小正方形(450,450),如下所示:

(不要介意两个图像的亮度差异).
问题:
如何在OpenCV-Python中执行此操作?我正在使用cv2版本.
fir*_*ant 34
除了etarion的建议,您还可以使用重映射功能.我写了一个快速的脚本来展示你如何做到这一点.在编辑时,这在Python中非常简单.这是测试图像:

这是翘曲后的结果:

以下是代码:
import cv2
from scipy.interpolate import griddata
import numpy as np
grid_x, grid_y = np.mgrid[0:149:150j, 0:149:150j]
destination = np.array([[0,0], [0,49], [0,99], [0,149],
[49,0],[49,49],[49,99],[49,149],
[99,0],[99,49],[99,99],[99,149],
[149,0],[149,49],[149,99],[149,149]])
source = np.array([[22,22], [24,68], [26,116], [25,162],
[64,19],[65,64],[65,114],[64,159],
[107,16],[108,62],[108,111],[107,157],
[151,11],[151,58],[151,107],[151,156]])
grid_z = griddata(destination, source, (grid_x, grid_y), method='cubic')
map_x = np.append([], [ar[:,1] for ar in grid_z]).reshape(150,150)
map_y = np.append([], [ar[:,0] for ar in grid_z]).reshape(150,150)
map_x_32 = map_x.astype('float32')
map_y_32 = map_y.astype('float32')
orig = cv2.imread("tmp.png")
warped = cv2.remap(orig, map_x_32, map_y_32, cv2.INTER_CUBIC)
cv2.imwrite("warped.png", warped)
Run Code Online (Sandbox Code Playgroud)
我想你可以google并找到griddata的功能.简而言之,它进行插值,在这里我们使用它将稀疏映射转换为密集映射,因为cv2.remap需要密集映射.我们只需要将值转换为float32,因为OpenCV会抱怨float64类型.请告诉我它是怎么回事.
更新:如果您不想依赖Scipy,一种方法是在代码中实现2d插值函数,例如,请参阅Scipy中的griddata源代码或更简单的源代码http://inasafe.readthedocs .org/en/latest/_modules/engine/interpolation2d.html,它只取决于numpy.虽然,我建议使用Scipy或其他库,但我知道为什么只需要CV2和numpy可能更适合这样的情况.我想听听你的最终代码如何解决Sudokus.