OpenCV 4.0.0和Python 3.7无法获取图像注册

2024-04-28 22:03:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python3.7中opencv4.0.0中的图像注册模块。我定义了一个普通图像m1和一个移位版本m2。然后,我尝试用cv2.reg\umappergradshift()和cv2.regmapperPyramid()和cv2.regmapperGradshift()来找到偏移量

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 )

结果是

^{pr2}$

我怎么才能找到合适的班次?在

另外,当我尝试对两个不同大小的图像运行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) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

难道不能注册不同尺寸的图像(在较大的图像中找到较小的图像)?在

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

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参数)有关?在


Tags: 图像numpyzerosregcv2t1t3calculate
1条回答
网友
1楼 · 发布于 2024-04-28 22:03:13

您应该使用matchTemplate,它允许您在图像中找到模板。例如,如果模板是

template

你的形象是

entire image。在

结果是

matching result

(可以在OpenCV文档中找到这些图像)

根据您的需要,您可以选择一种或另一种方法,例如,TM_CCORR_NORMED是健壮的,而TM_SQDIFF is则需要较少的计算。在

更新

它很容易用于翻译,但是对于更复杂的转换,您应该使用Features2D and homography来查找模板。下面是OpenCV文档示例:

Features2D_Homography

算法思想是:

  1. 提取每个图像的关键点
  2. 计算每个关键点的描述符(例如SIFT或SURF)
  3. 根据描述词匹配关键点
  4. 从匹配项计算转换参数

提取每个图像中的关键点,然后匹配描述符。在

或许this也应该有帮助。在

相关问题 更多 >