首页 > 解决方案 > 无法使用 OpenCV 4.0.0 和 Python 3.7 进行图像注册工作

问题描述

我正在尝试使用 Python 3.7 中的 OpenCV 4.0.0 中的图像注册模块。我定义了一个普通图像 m1 和一个移位版本 m2。然后我尝试使用 cv2.reg_MapperGradShift( ) 以及 cv2.reg_MapperPyramid( ) 和 cv2.reg_MapperGradShift( ) 的组合来查找移位

m1 = numpy.zeros( ( 10, 10 ) )
m1[ 3, 3 ] = 1
m2 = numpy.zeros( ( 10, 10 ) )
m2[ 3, 6 ] = 1

t1 = cv2.reg_MapperGradShift( )
t2 = t1.calculate( m1, m2 )
t3 = cv2.reg.MapTypeCaster_toShift( t2 )
t4 = t3.getShift( )
print( t4 )

t5 = cv2.reg_MapperPyramid( t1 )
t6 = t5.calculate( m1, m2 )
t7 = cv2.reg.MapTypeCaster_toShift( t6 )
t8 = t7.getShift( )
print( t8 )

结果是

[[0.]
 [0.]]
[[9.11379962]
 [6.47128924]]

我怎样才能让它找到正确的转变?

此外,当我尝试在 2 张不同大小的图像上运行 calculate() 时,我得到

cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:663: error: (-209:Sizes of input arguments do not match) 操作是既不是'array op array'(其中数组具有相同的大小和相同数量的通道),也不是'array op scalar',也不是函数'cv::arithm_op'中的'scalar op array'

不应该可以注册不同尺寸的图像(在较大的图像中找到较小的图像)吗?

编辑:我又玩了一些,我发现当我定义更大的图像并在中间某处设置一个像素时,我用 cv2.reg_MapperPyramid() 得到了正确的答案,但是当我在边界附近设置一个像素时,我仍然得到错误的答案。例如

m1 = numpy.zeros( ( 100, 100 ) )
m1[ 30, 30 ] = 1
m2 = numpy.zeros( ( 100, 100 ) )
m2[ 30, 33 ] = 1

t1 = cv2.reg_MapperGradShift( )
t2 = t1.calculate( m1, m2 )
t3 = cv2.reg.MapTypeCaster_toShift( t2 )
t4 = t3.getShift( )
print( t4 )

t5 = cv2.reg_MapperPyramid( t1 )
t6 = t5.calculate( m1, m2 )
t7 = cv2.reg.MapTypeCaster_toShift( t6 )
t8 = t7.getShift( )
print( t8 )

结果是

[[0.]
 [0.]]
[[3.02103606]
 [0.        ]]

它是否与注册模块如何推断图像(其他函数中使用的borderMode或borderType参数)有关?

标签: pythonopencv

解决方案


您需要将图像的数据类型从 int 更改为 float32 或 float64。如果你不这样做,它就不起作用。


推荐阅读