OpenCV错误:(-215:Assert失败)(M0.type()== CV_32F||在函数“warpPerspective”中的M0.type()== CV_64F)和M0.rows == 3和M0.cols == 3

envsm3lx  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(1068)

我正在尝试对两个大小为128x128的RGB肺罩图像进行图像配准。当我学习图像配准时,这对其他图像工作正常,但现在不知何故,它抛出了这样的错误。我是一个新手学习这一点,任何帮助都是感激不尽的。
我在下面附上了我要做的代码,在这里我通过下面的GeeksForGeeks创建了一个registerImage函数,并传递了我想要注册的图像。

import cv2
import numpy as np

def registerImage(img1,img2):
  # Open the image files.
  img1_color = img1  # Image to be aligned.
  img2_color = img2   # Reference image.
  
  # Convert to grayscale.
  img1 = cv2.cvtColor(img1_color, cv2.COLOR_BGR2GRAY)
  img2 = cv2.cvtColor(img2_color, cv2.COLOR_BGR2GRAY)
  height, width = img2.shape
  
  # Create ORB detector with 5000 features.
  ## used to creates keypoints on the reference image
  orb_detector = cv2.ORB_create(5000)
  
  # Find keypoints and descriptors.
  # The first arg is the image, second arg is the mask
  #  (which is not required in this case).
  kp1, d1 = orb_detector.detectAndCompute(img1, None)
  kp2, d2 = orb_detector.detectAndCompute(img2, None)
  
  # Match features between the two images.
  # We create a Brute Force matcher with
  # Hamming distance as measurement mode.

  #Brute-Force matcher is simple. 
  #It takes the descriptor of one feature in first set and is matched with all other features in second set using some distance calculation. And the closest one is returned.
  matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)
  
  # Match the two sets of descriptors.
  matches = matcher.match(d1, d2)
  
  # Sort matches on the basis of their Hamming distance.
  matches.sort(key = lambda x: x.distance)
  
  # Take the top 90 % matches forward.
  matches = matches[:int(len(matches)*0.9)]
  no_of_matches = len(matches)
  
  # Define empty matrices of shape no_of_matches * 2.
  p1 = np.zeros((no_of_matches, 2))
  p2 = np.zeros((no_of_matches, 2))
  
  for i in range(len(matches)):
    p1[i, :] = kp1[matches[i].queryIdx].pt
    p2[i, :] = kp2[matches[i].trainIdx].pt
  
  # Find the homography matrix.
  homography, mask = cv2.findHomography(p1, p2, cv2.RANSAC)
  
  # Use this matrix to transform the
  # colored image wrt the reference image.
  transformed_img = cv2.warpPerspective(img1_color,
                      homography, (width, height))
  
  # Save the output.
  # cv2.imwrite('output.jpg', transformed_img)

  img1_show = cv2.resize(img1_color,(320,320))
  img2_show = cv2.resize(img2_color,(320,320))
  img3_show = cv2.resize(transformed_img,(320,320))
  img = np.concatenate((img1_show,img2_show,img3_show), axis=1)
  cv2_imshow(img)


ref_path = path + "/mask_0.png"
test_path = path + "/mask_8.png"

from google.colab.patches import cv2_imshow
ref_mask = cv2.imread(ref_path)
cv2_imshow(ref_mask)

test_mask = cv2.imread(test_path)
cv2_imshow(test_mask)

registerImage(ref_mask,test_mask)

############################################################################
Error:
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-18-b7a8933e693e> in <module>()
----> 1 registerImage(ref_mask,test_mask)

<ipython-input-2-3a703c66a8e0> in registerImage(img1, img2)
     54   # colored image wrt the reference image.
     55   transformed_img = cv2.warpPerspective(img1_color,
---> 56                       homography, (width, height))
     57 
     58   # Save the output.

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/imgwarp.cpp:3167: error: (-215:Assertion failed) (M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 3 && M0.cols == 3 in function 'warpPerspective'
gblwokeq

gblwokeq1#

从错误出现的位置向后追溯,找出导致错误的原因。
您在此语句中遇到错误:

transformed_img = cv2.warpPerspective(img1_color,
                      homography, (width, height))

我的建议是检查输入函数的变量本身,这里有一个粗略但简单的方法。

# Place these statements prior to the error and check their values
print("hemography:",hemography) # In my case this was None, which is why I got an error
print("h:",height,"w:", width) # Check width/height seems correct
cv2_imshow(img1_color) # Check image is loaded properly

同样值得注意的是,如果它出现在其他人搜索的时候。对于类似的代码,我在matches.sort(..)行得到了一个错误,因为类型是元组而不是列表。
为了修复它,我将其更改为以下内容:

matches = sorted(matches,key=lambda x:x.distance, reverse=False)

调试是一项你会慢慢学会的技能,就像在这里提问/回答问题一样。如果你是python新手,这个错误并不容易理解,这也是我最初发现这个错误的原因。随着时间的推移,文档也会变得更容易理解。

相关问题