我很确定我的图像是灰度级的,应该是单通道但是我收到这个错误并且不知道如何解决它。
>>>
=============== 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