OpenCV 图像拼接错误: cv2.error: OpenCV(4.7.0) D:/a/opencv-python/opencv-python/opencv/modules/core/src/matrix_operations.cpp:67: error: (-215:Assertion failed) src[i].dims <= 2 && src[i].rows == src[0].rows && src[i].type() == src[0].type() in function 'cv::hconcat'
使用 OpenCV 的 cv2.hconcat() 函数拼接图像时,遇到错误 'cv2.error: OpenCV(4.7.0) D:/a/opencv-python/opencv-python/opencv/modules/core/src/matrix_operations.cpp:67: error: (-215:Assertion failed) src[i].dims <= 2 && src[i].rows == src[0].rows && src[i].type() == src[0].type() in function 'cv::hconcat'' 是因为传入的图像数组维度不一致或者类型不一致,导致无法拼接。/n/n解决方法:/n/n1. 检查传入的四张图像是否大小、类型一致,并且确保维度一致。 可以使用 cv2.imshow() 函数分别显示四张图像,查看是否有异常。/n2. 如果图像大小不一致,可以使用 cv2.resize() 函数将图像大小统一。 例如:/npython/nimg_matches12 = cv2.resize(img_matches12, (width, height)) # 调整图像大小/n/n3. 如果图像类型不一致,可以使用 cv2.cvtColor() 函数将图像类型转换一致。 例如:/npython/nimg_matches12 = cv2.cvtColor(img_matches12, cv2.COLOR_BGR2GRAY) # 转换为灰度图像/n/n/n示例代码:/n/npython/nimport cv2/n/n# 读取图像/nimg1 = cv2.imread('1.png')/nimg2 = cv2.imread('2.png')/nimg3 = cv2.imread('3.png')/nimg4 = cv2.imread('4.png')/n/n# ... (其他代码)/n/n# 调整图像大小/nwidth, height = img_matches12.shape[:2]/nimg_matches23 = cv2.resize(img_matches23, (width, height))/nimg_matches34 = cv2.resize(img_matches34, (width, height))/nimg_matches14 = cv2.resize(img_matches14, (width, height))/n/n# 将四张图像拼接起来/nimg_concat = cv2.hconcat([img_matches12, img_matches23, img_matches34, img_matches14])/n/n# 显示拼接后的图像/ncv2.imshow('Matches', img_concat)/ncv2.waitKey(0)/n/n/n注意: 在使用 cv2.resize() 函数时,可以根据实际情况选择不同的插值方法,例如 cv2.INTER_AREA、cv2.INTER_LINEAR、cv2.INTER_CUBIC 等。
原文地址: https://www.cveoy.top/t/topic/koN0 著作权归作者所有。请勿转载和采集!