学无先后,达者为师

网站首页 编程语言 正文

imgwarp.cpp:3143: error: (-215:Assertion failed) _src.total() > 0 in function ‘warpPerspective‘

作者:修炼之路 更新时间: 2022-02-13 编程语言

错误缘由

在使用opencv做透视变换的时候报error: (-215:Assertion failed) _src.total() > 0 in function 'warpPerspective',错误的详细信息如下

cv2.error: OpenCV(4.4.0) /tmp/pip-req-build-dglzv4yn/opencv/modules/imgproc/src/imgwarp.cpp:3143: error: (-215:Assertion failed) _src.total() > 0 in function ‘warpPerspective’

透视变化的代码如下:

def clip_image(img,points):
    if isinstance(points,list):
        points = np.array(points,dtype=np.float32)
    img_crop_width = max(np.linalg.norm(points[0] - points[1]),
                         np.linalg.norm(points[2] - points[3]))
    img_crop_height = max(np.linalg.norm(points[1] - points[2]),
                          np.linalg.norm(points[0] - points[3]))
    pts_std = np.float32([[0,0],[img_crop_width,0],[img_crop_width,img_crop_height],[0,img_crop_height]])
    M = cv2.getPerspectiveTransform(points,pts_std)
    dst_img = cv2.warpPerspective(img, M, (img_crop_width, img_crop_height), borderMode=cv2.BORDER_REPLICATE,
                                  flags=cv2.INTER_CUBIC)
    dst_img_height, dst_img_width = dst_img.shape[0:2]
    if dst_img_height * 1.0 / dst_img_width >= 1.5:
        dst_img = np.rot90(dst_img)
    return dst_img

错误原因分析

出现这种错误的原因可能有三种:

  • 检查img是否为None
  • 检查points坐标中是否出现负数
  • 检查pointspts_std点的顺序是否一致

原文链接:https://xiulian.blog.csdn.net/article/details/121666964

栏目分类
最近更新