读取图片,并在新图片上生成轮廓图

给定一张图片,求出这个图片的轮廓图,然后把轮廓图在另外单独图片上显示轮廓图。

def get_contours2(path):
    # 读取并显示原始图
    image_raw=cv2.imread(path,0)
    ret, image_raw = cv2.threshold(image_raw, 100, 255, cv2.THRESH_BINARY_INV)
    cv2.imshow('image_raw',image_raw)

    # 得到轮廓
    _, contours, hierarchy = cv2.findContours(image_raw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    # 生成同等尺寸的空白图
    height, width = image_raw.shape
    image_c = np.zeros((height, width), np.uint8)
    cv2.imshow('image_new',image_c)

    # 在新生成图上画轮廓
    cv2.drawContours(image_c, contours, -1, (255), 1)
    cv2.imshow('image_c',image_c)
    
    cv2.waitKey()

需要注意的是,cv2.threshold函数找到的是白色边缘的轮廓图

原文地址:https://www.cnblogs.com/shixisheng/p/9481463.html