用OpenCV实现简单的泊松融合

原理:就不介绍了,因为我也不是很懂_。网上有很多大佬都介绍了。

opencv收集了泊松融合算法

 void seamlessClone(InputArray src, InputArray dst, InputArray mask, Point p, OutputArray blend, int flags);

参数介绍:
@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 one of the following:

  • NORMAL_CLONE The power of the method is fully expressed when inserting objects with
    complex outlines into a new background
  • 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.
    简单调用的代码:
  #include <time.h>
  #include <opencv2\opencv.hpp>
  void main(){
    clock_t start1, end1;
	clock_t start2, end2;

	cv::Mat src = cv::imread("src.jpg");
	cv::Mat dst = cv::imread("back.jpg ");
	cv::Mat src_mask = cv::imread("mask1.jpg");
	cv::Point center(dst.cols / 2 , dst.rows / 2);

	cv::Mat normal_clone;
	cv::Mat mixed_clone;

	start1 = clock();
	//参数:src原图,dst背景图,src_mask是mask,center是原图放到背景图哪个地方,
	//normal_clone是融合后的图,NORMAL_CLONE是融合的方式。
	seamlessClone(src, dst, src_mask, center, normal_clone, cv::NORMAL_CLONE);
	end1 = clock();

	start2 = clock();
	seamlessClone(src, dst, src_mask, center, mixed_clone, cv::MIXED_CLONE);//MIXED_CLONE更节省时间,大概是NORMAL_CLONE的一半时间就可以
	end2 = clock();

	printf("NORMAL_CLONE use time: %d\n", end1 - start1);
	printf("MIXED_CLONE use time: %d\n", end2 - start2);

	cv::imshow("normal_clone.jpg", normal_clone);
	cv::imshow("mixed_clone.jpg", mixed_clone);
	cv::waitKey(0);
}

原图:
在这里插入图片描述
背景图:在这里插入图片描述
mask:
在这里插入图片描述
normal_clone:
在这里插入图片描述

mixed_clone:在这里插入图片描述

NORMAL_CLONE与MIXED_CLONE的对比:
在这里插入图片描述
MIXED_CLONE更节省时间。效果好像都不是很好,飞机与背景图融合后,飞机的颜色都变了,参入了背景的颜色,速度也不够快,估计不能用在时时的环境下。

猜你喜欢

转载自blog.csdn.net/weixin_43081805/article/details/86006592