本文主要是介绍Opencv python: seamlessClone泊松融合 (我把Lena变成了彼岸花怪/(ㄒoㄒ)/~~),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
为什么要进行融合呢?原因是LZ在进行贴图操作的时候,经常会出现很明显的边界效应,在各种查找资料的情况下,找到了一种比较适合图像融合的方法,并且OpenCV有对应的接口,所以就网上下了图片,做了一些尝试,当然最后并没有使用这个函数是因为贴图效果太不明显了,LZ甚至以为是自己代码写错了,所以这个函数因人而异。
首先这个函数的用法:
def seamlessClone(src, dst, mask, p, flags, blend=None): # real signature unknown; restored from __doc__"""seamlessClone(src, dst, mask, p, flags[, blend]) -> blend. @brief Image editing tasks concern either global changes (color/intensity corrections, filters,. deformations) or local changes concerned to a selection. Here we are interested in achieving local. changes, ones that are restricted to a region manually selected (ROI), in a seamless and effortless. manner. The extent of the changes ranges from slight distortions to complete replacement by novel. content @cite PM03 .. . @param src Input 8-bit 3-channel image.. @param dst Input 8-bit 3-channel image.. @param mask Input 8-bit 1 or 3-channel image.. @param p Point in dst image where object is placed.. @param blend Output image with the same size and type as dst.. @param flags Cloning method that could be cv::NORMAL_CLONE, cv::MIXED_CLONE or cv::MONOCHROME_TRANSFER"""pass
NORMAL_CLONE
Python: cv.NORMAL_CLONE
The power of the method is fully expressed when inserting objects with complex outlines into a new background
将具有复杂轮廓的对象插入新背景,也就是说不保留dst 图像的texture细节,目标区域的梯度只由源图像决定。
MIXED_CLONE
Python: cv.MIXED_CLONE
The classic method, color-based selection and alpha masking might be time consuming and often leaves an undesirable halo. Seamless cloning, even averaged with the original image, is not effective. Mixed seamless cloning based on a loose selection proves effective.
基于宽松选择的混合无缝克隆,保留des图像的texture 细节。目标区域的梯度是由原图像和目的图像的组合计算出来(计算dominat gradient)。
MONOCHROME_TRANSFER
Python: cv.MONOCHROME_TRANSFER
Monochrome transfer allows the user to easily replace certain features of one object by alternative features.
不保留src图像的颜色细节,只有src图像的质地,颜色和目标图像一样,可以用来进行皮肤质地填充(美颜是不是可以用呢?)
import cv2
import numpy as np# read our test imges
img1 = cv2.imread("../test_imgs/lena_standard.jpg")
img2 = cv2.imread("../test_imgs/beiji.jpeg")
cv2.imshow("img1", img1)
cv2.imshow("img2", img2)
cv2.waitKey()
cv2.destroyAllWindows()
# create an white mask
mask = 255*np.ones(img1.shape, img1.dtype)
# the location of the src in the dst
width, height, channel = img2.shape
center = (int(height/2), int(width/2))
normal_clone = cv2.seamlessClone(img1, img2, mask, center, cv2.NORMAL_CLONE)
cv2.imshow("normal_clone", normal_clone)
cv2.waitKey()
mixed_clone = cv2.seamlessClone(img1, img2, mask, center, cv2.MIXED_CLONE)
cv2.imshow("mixed_clone", mixed_clone)
cv2.waitKey()
# cv2.destroyAllWindows()
mono_clone = cv2.seamlessClone(img1, img2, mask, center, cv2.MONOCHROME_TRANSFER)
cv2.imshow("mono_clone", mono_clone)
cv2.waitKey()
cv2.destroyAllWindows()
额,测试图片感觉没选好,融合起来感觉哪里怪怪的/(ㄒoㄒ)/~~
#这段代码写的可随意了,就是生成一个mask,做一个融合
img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(img1_gray, 100, 255, cv2.THRESH_BINARY | cv2.THRESH_TRIANGLE)contours, _ = cv2.findContours(binary,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# print("contour: ", contour)
src_mask = np.zeros(img1.shape, img1.dtype)area = []
want_area = []
for i in range(len(contours)):area.append(cv2.contourArea(contours[i]))
area.sort()
for i in range(len(contours)):if (cv2.contourArea(contours[i]) < area[-1]):cv2.fillConvexPoly(src_mask, contours[i], 0)else:cv2.fillConvexPoly(src_mask, contours[i], (255,255, 255))
test_mask = src_mask[:,:,1]# print("mask_shape: ", mask_new.shape)
normal_clone_bin = cv2.seamlessClone(img1, img2, test_mask, center, cv2.NORMAL_CLONE)cv2.imshow("normal_clone_bin", normal_clone_bin)
cv2.waitKey()
cv2.destroyAllWindows()
参考地址:
- https://www.jianshu.com/p/49adfbe4b804
- https://docs.opencv.org/3.4/df/da0/group__photo__clone.html
这篇关于Opencv python: seamlessClone泊松融合 (我把Lena变成了彼岸花怪/(ㄒoㄒ)/~~)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!