【opencv】Opencv python: seamlessClone泊松融合 (我把Lena变成了彼岸花怪/(ㄒoㄒ)/~~)

发布时间:2026/10/23 5:51:42
【opencv】Opencv python: seamlessClone泊松融合 (我把Lena变成了彼岸花怪/(ㄒoㄒ)/~~) 为什么要进行融合呢原因是LZ在进行贴图操作的时候经常会出现很明显的边界效应在各种查找资料的情况下找到了一种比较适合图像融合的方法并且OpenCV有对应的接口所以就网上下了图片做了一些尝试当然最后并没有使用这个函数是因为贴图效果太不明显了LZ甚至以为是自己代码写错了所以这个函数因人而异。首先这个函数的用法defseamlessClone(src,dst,mask,p,flags,blendNone):# 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 passNORMAL_CLONEPython: cv.NORMAL_CLONEThe power of the method is fully expressed when inserting objects with complex outlines into a new background将具有复杂轮廓的对象插入新背景也就是说不保留dst 图像的texture细节目标区域的梯度只由源图像决定。MIXED_CLONEPython: cv.MIXED_CLONEThe 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_TRANSFERPython: cv.MONOCHROME_TRANSFERMonochrome transfer allows the user to easily replace certain features of one object by alternative features.不保留src图像的颜色细节只有src图像的质地颜色和目标图像一样可以用来进行皮肤质地填充美颜是不是可以用呢importcv2importnumpyasnp# read our test imgesimg1cv2.imread(../test_imgs/lena_standard.jpg)img2cv2.imread(../test_imgs/beiji.jpeg)cv2.imshow(img1,img1)cv2.imshow(img2,img2)cv2.waitKey()cv2.destroyAllWindows()# create an white maskmask255*np.ones(img1.shape,img1.dtype)# the location of the src in the dstwidth,height,channelimg2.shape center(int(height/2),int(width/2))normal_clonecv2.seamlessClone(img1,img2,mask,center,cv2.NORMAL_CLONE)cv2.imshow(normal_clone,normal_clone)cv2.waitKey()mixed_clonecv2.seamlessClone(img1,img2,mask,center,cv2.MIXED_CLONE)cv2.imshow(mixed_clone,mixed_clone)cv2.waitKey()# cv2.destroyAllWindows()mono_clonecv2.seamlessClone(img1,img2,mask,center,cv2.MONOCHROME_TRANSFER)cv2.imshow(mono_clone,mono_clone)cv2.waitKey()cv2.destroyAllWindows()额测试图片感觉没选好融合起来感觉哪里怪怪的/(ㄒoㄒ)/~~#这段代码写的可随意了就是生成一个mask做一个融合img1_graycv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)ret,binarycv2.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_masknp.zeros(img1.shape,img1.dtype)area[]want_area[]foriinrange(len(contours)):area.append(cv2.contourArea(contours[i]))area.sort()foriinrange(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_masksrc_mask[:,:,1]# print(mask_shape: , mask_new.shape)normal_clone_bincv2.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/49adfbe4b804https://docs.opencv.org/3.4/df/da0/group__photo__clone.html

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询