图像叠加cv2.seamlessClone

opencv python用一个函数将两个图像进行叠加:cv2.seamlessClone

im = cv2.imread('E:/bglearn/pic/90.jpg')
    obj = cv2.imread('E:/bglearn/pic/90.jpg')

    mask = 255 * np.ones(obj.shape, obj.dtype)

    # The location of the center of the src in the dst
    width, height, channels = im.shape
    center = (height // 2, width // 2)

    # Seamlessly clone src into dst and put the results in output
    normal_clone = cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
    mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)


    # Clone seamlessly.


    # 保存结果
    cv_show('normal_clone',normal_clone)
    #cv_show('mixed_clone',mixed_clone)
    new_bottle = cv2.absdiff(im, normal_clone)
    # cv_show('new_bottle', new_bottle)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
    new_bottle_gray = cv2.cvtColor(new_bottle, cv2.COLOR_BGR2GRAY)
    new_bottle_thresh = cv2.threshold(new_bottle_gray, 10, 255, cv2.THRESH_BINARY)[1]
    # 得到新瓶子的mask图
    new_bottle_mask = cv2.morphologyEx(new_bottle_thresh, cv2.MORPH_CLOSE, kernel)
    # cv_show('new_bottle_mask', new_bottle_mask)
    _, new_bottle_mask_contours, _ = cv2.findContours(new_bottle_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(normal_clone, new_bottle_mask_contours, -1,
                         (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), 2)

    cv_show('normal_clone',normal_clone)

你可能感兴趣的:(opencv+python)