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

标签 python opencv image-processing computer-vision

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

>>> 
=============== 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)

最佳答案

看起来您的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

关于python - 错误:(-210) warpMatrix 必须是函数 cv::findTransformECC 中的单 channel 浮点矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46040987/

相关文章:

python - 从复数中删除括号 - pandas

Python + Pandas + Spark - 如何将数据框导入 Pandas 数据框并将其转换为字典?

android - 在 Android 中使用 OpenCV 时遇到问题

opencv - 我们可以使用 Lucas Kanade 光流(opencv)进行基于颜色的检测或轮廓对象跟踪吗?

python - 如何生成 ROI 的高斯分布强度?

python - 随机字典排序

python - 为什么 pprofile 没有输出?

c++ - 有没有办法在opencv c++中检查矩阵中的数据

java - OpenCV物体检测轮廓位置

c++ - 如何找到特定像素数对应的像素值?