2017-09-04 27 views
0

我非常確定我的圖像是灰度,這應該是單通道,但我得到這個錯誤,不知道如何解決它。錯誤:(-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) 
+1

轉換變量'warp'鍵入'np.float32'然後繼續 –

回答

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 
+0

我可以問你關於函數findtransformECC嗎?我在網上看到很多關於cc的例子,像這樣「(cc,warp)= cv2.findTransformECC(im_gray,im1_gray,warp,warp_mode,criteria)」我沒有得到什麼是cc指的實際上.... 。是否有必要把這個用於transformECC函數? *對不起,我知道這是一些與帖子無關的問題* – SacreD

+1

該函數返回兩個元素的元組。您不必這樣做,但您需要訪問元組的第二個元素,這是描述圖像之間關係的估計扭曲矩陣。所以你可以像現在這樣離開它,但是確保你在獲得矩陣之後執行'warp [1]'。元組的第一個元素是返回代碼,用於確定估計的成功程度,但如果圖像定義得當,則可以忽略代碼。但是,對函數的輸入初始估計在返回時也會發生變化,所以您可以簡單地將它設置爲等於任何值。 – rayryeng

相關問題