私の画像はグレースケールでシングルチャンネルでなければならないと確信していますが、それを回避する。エラー:(-210)warpMatrixは関数内でシングルチャンネルの浮動小数点型でなければなりませんcv :: findTransformECC
>>>
=============== 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'を実行してから進んでください –