错误:( - 210)warpMatrix必须是函数cv :: findTransformECC中的单通道浮点矩阵

时间:2017-09-04 16:21:00

标签: python opencv image-processing computer-vision

我很确定我的图像是灰度级的,应该是单通道但是我收到此错误并且不知道如何解决它。

>>> 
=============== RESTART: C:/Users/310293649/Desktop/resize.py ===============
Traceback (most recent call last):
  File "C:/Users/310293649/Desktop/resize.py", line 64, in <module>
    alignment(criteria, warp_mode, warp, nol)
  File "C:/Users/310293649/Desktop/resize.py", line 47, in alignment
    warp = cv2.findTransformECC(im_gray, im1_gray, warp, warp_mode, criteria)
cv2.error: D:\Build\OpenCV\opencv-3.3.0\modules\video\src\ecc.cpp:347: error: (-210) warpMatrix must be single-channel floating-point matrix in function cv::findTransformECC

>>> 

以下是我的代码:我正在努力通过为每个图像创建图像金字塔来加速我的代码。将图像缩放到最小值得到粗略估计并进行扩展。

import cv2
import numpy as np


path = "R:\\ProcessedPhoto_in_PNG\\"
path1 = "R:\\AlignedPhoto_in_PNG_EUCLIDEAN\\"

nol = 3
warp_mode = cv2.MOTION_EUCLIDEAN
if warp_mode == cv2.MOTION_HOMOGRAPHY :
    warp = np.eye(3, 3, dtype=np.float32)
else :
    warp = np.eye(2, 3, dtype=np.float32)


warp = np.dot(warp, np.array([[1, 1, 2], [1, 1, 2], [1/2, 1/2, 1]])**(1-nol))


# Specify the number of iterations.
number_of_iterations = 5000;

# Specify the threshold of the increment
# in the correlation coefficient between two iterations
termination_eps = 1e-10;

# Define termination criteria
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations,  termination_eps)

def alignment(criteria, warp_mode, warp, nol):

    for i in range(1770,1869):
        for level in range(nol):
            im = cv2.imread(path + 'IMG_1770.png')
            im1 = cv2.imread(path + 'IMG_%d.png'%(i))

            sz = im1.shape


            scale = 1/2**(nol-1-level)

            im_1 = cv2.resize(im, None, fx= scale, fy = scale, interpolation=cv2.INTER_AREA)
            im_2 = cv2.resize(im1, None, fx= scale, fy= scale, interpolation=cv2.INTER_AREA)

            im_gray = cv2.cvtColor(im_1, cv2.COLOR_BGR2GRAY)
            im1_gray = cv2.cvtColor(im_2, cv2.COLOR_BGR2GRAY)

            # Run the ECC algorithm. The results are stored in warp_matrix.
            warp = cv2.findTransformECC(im_gray, im1_gray, warp, warp_mode, criteria)

            if level != nol-1:
            # might want some error catching here to reset initial guess
            # if your algorithm fails at some level of the pyramid

            # scale up for the next pyramid level
                warp = warp * np.array([[1, 1, 2], [1, 1, 2], [1/2, 1/2, 1]])

            if warp_mode == cv2.MOTION_HOMOGRAPHY :
                # Use warpPerspective for Homography 
                im1_aligned = cv2.warpPerspective (im1, warp, (sz[1],sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
            else :
                # Use warpAffine for Translation, Euclidean and Affine
                im1_aligned = cv2.warpAffine(im1, warp, (sz[1],sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP);
            print(i)
            cv2.imwrite(path1 + "AlignedEU_IMG_%d.png"%i , im1_aligned )

alignment(criteria, warp_mode, warp, nol)

1 个答案:

答案 0 :(得分:1)

看起来您的warp矩阵最初属于np.float32类型,但随后您使用np.dot进行了另一个矩阵乘法,而另一个矩阵不是np.float32 - 未指定类型默认为np.float64 - 因此结果会提升为np.float64类型。您需要确保第二个矩阵也是np.float32类型。这就是findTransformECC抱怨的原因,因为它希望warp矩阵的类型为np.float32,因此会出现错误消息。解决此问题的最简单方法是首先创建第二个矩阵以确保使用np.float64保持精度,然后在将其传递给np.float32时在乘法之前转换为np.dot

# ....
# ....
nol = 3
warp_mode = cv2.MOTION_EUCLIDEAN
if warp_mode == cv2.MOTION_HOMOGRAPHY :
    warp = np.eye(3, 3, dtype=np.float32)
else :
    warp = np.eye(2, 3, dtype=np.float32)

# New - Create temporary placeholder for new matrix
tmp = np.array([[1, 1, 2], [1, 1, 2], [1/2, 1/2, 1]])**(1-nol)

# Matrix multiply with it but ensuring it's of type np.float32
warp = np.dot(warp, tmp.astype(np.float32))

# ....
# ....
# Rest of your code follows