图像泊松融合学习笔记

目录

图像泊松融合原理:

使用方法

python opencv 泊松融合例子

报错并解决:

泊松融合c++版:


图像泊松融合原理:

从泊松方程的解法,聊到泊松图像融合 - 知乎

使用方法

需要三张图片:前景图、背景图、mask图

mask图(指明前景图中需要融合的区域,最简单的就是直接等于前景图大小的 mask,待融合区域是白色,其余位置黑色)。

center是背景图的融合中心坐标。

python opencv 泊松融合例子

不需要png格式,

# 注意修改路径!
import cv2
import numpy as np

# Read images : src image will be cloned into dst
im = cv2.imread("images/wood-texture.jpg")
obj= cv2.imread("images/iloveyouticket.jpg")

# Create an all white mask
mask = 255 * np.ones(obj.shape, obj.dtype)

# The location of the center of the src in the dst
height, width,channels = im.shape
center = (width/2,height/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)

# Write results
cv2.imwrite("images/opencv-normal-clone-example.jpg", normal_clone)
cv2.imwrite("images/opencv-mixed-clone-example.jpg", mixed_clone)

转效果:

前景图:

背景图: 

效果图:

简单总结,前景图的背景图,几乎纯色的时候,会自动被去掉。

例子2:


import cv2
import numpy as np

if __name__ == '__main__':

    # Read images : src image will be cloned into dst
    dst = cv2.imread("zimu2.jpg")
    obj= cv2.imread("zimu.webp")

    # Create an all white mask
    mask = 255 * np.ones(obj.shape, obj.dtype)

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

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

    # Write results
    cv2.imshow("obj", obj)
    cv2.imshow("normal_clone", normal_clone)
    cv2.imshow("mixed_clone", mixed_clone)

    cv2.waitKey()

 

报错并解决:

Traceback (most recent call last):
  File "F:/biadu_down/yolov5-face-master/data/ronghe/bosong.py", line 20, in <module>
    mixed_clone = cv2.seamlessClone(obj, dst, mask, center, cv2.MIXED_CLONE)
cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'seamlessClone'
> Overload resolution failed:
>  - Can't parse 'p'. Sequence item with index 0 has a wrong type
>  - Can't parse 'p'. Sequence item with index 0 has a wrong type

原因:数据类型不对,

center = (height/2, width/2)

center应该是int类型,修改方法:

center = (height//2, width//2)

修改后完整代码:


import cv2
import numpy as np

if __name__ == '__main__':

    # Read images : src image will be cloned into dst
    dst = cv2.imread("diban.jpg")
    obj= cv2.imread("zimu.jpg")

    # Create an all white mask
    mask = 255 * np.ones(obj.shape, obj.dtype)

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

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

    # Write results
    cv2.imshow("opencv-normal-clone-example.jpg", normal_clone)
    cv2.imshow("opencv-mixed-clone-example.jpg", mixed_clone)

    cv2.waitKey()

泊松融合c++版:

#include <opencv2\opencv.hpp>
 #include <iostream>
 #include <string>
 
 using namespace std;
 using namespace cv;
 
 void main()
 {
     Mat imgL = imread("data/apple.jpg");
     Mat imgR = imread("data/orange.jpg");
 
     int imgH = imgR.rows;
     int imgW = imgR.cols;
     Mat mask = Mat::zeros(imgL.size(), CV_8UC1);
     mask(Rect(,, imgW*0.5, imgH)).setTo();
     cv::imshow("mask", mask);
     Point center(imgW*0.25, imgH*0.5);
 
     Mat blendImg;
     seamlessClone(imgL, imgR, mask, center, blendImg, NORMAL_CLONE);
 
     cv::imshow("blendimg", blendImg);
     waitKey();
 }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值