目录
图像泊松融合原理:
使用方法
需要三张图片:前景图、背景图、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();
}