OPENCV错误:(-215:断言失败)(m0.type()== cv_32f || m0.type()== cv_64f)&&& m0.Rows == 3&& m0.cols == 3功能中的' warpperspective'
我正在尝试为两个大小128x128的RGB肺面膜图像执行图像注册。当我学习图像注册时,这对其他图像很好地工作了,但是现在以某种方式引发了这种错误。我是一个新手学习的,任何帮助都将受到赞赏。
我已经附上了我要在下面尝试执行的操作的代码,在该代码下,我通过关注Geeksforgeeks并传递了我要注册的图像来创建寄存器函数。
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'
I am trying to perform image registration for two RGB lung mask images of size 128x128. This had worked fine for other images when I was learning image registration but now somehow it throws such error. I am a newbie learning this, any help is appreciated.
I have attached the code of what I am trying to do below, where I have created a registerImage function by following GeeksForGeeks and passed images which I want to register.
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您遇到错误的地方向后走,然后追溯到它,以找到给您错误的原因。
您在此语句中会遇到错误:
我建议的是检查被输入功能的变量本身。这是一种粗略但简单的方法。
还值得注意的是,当其他人正在搜索时是否出现。对于类似的代码,我在
matches.sort(..)
线上遇到了一个错误,因为该类型是元组而不是列表。为了解决它,我将其更改为以下内容:
调试是您随着时间的推移您会提取的一项技能,在这里提出/回答问题。如果您是Python的新手,那个错误并不容易理解,这首先是我发现的。随着时间的流逝,文档也将变得更容易理解。
Go backwards from where you got the error and trace it back to find what is giving you the error.
You're getting errors at this statement:
What I'd suggest is to check the variables themselves that are fed into the function. Here's a crude but simple way to do it.
Also worth noting if it comes up while others are searching. For similar code I got an error at the
matches.sort(..)
line due to the type being Tuple and not a list.To fix it I changed it to the following:
Debugging is a skill you'll pickup over time, as are asking/answering questions here. That error isn't simple to understand if you're new to python, which is how I found this in the first place. The documentation will become easier to understand over time also.