资讯详情

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

📅 2026/10/23 5:51:47 | 华诺云谱 👁 阅读
【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
📝

华诺云谱内容团队

资深建站顾问 · 行业研究员

10年+企业数字化服务经验,专注智能建站、SEO优化与品牌营销,持续输出建站技巧、行业洞察与营销干货,已帮助5000+企业实现数字化增长。

你可能需要的服务

订阅华诺云谱资讯周报

每周一封,精选建站技巧、SEO与营销干货,直达邮箱。已有 8,000+ 企业主订阅,助你少走弯路。

↑