vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)

vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)

#define EIGEN_USE_MKL_ALL
#define EIGEN_VECTORIZE_SSE4_2
#include   
#include "core/core.hpp"    
#include "highgui/highgui.hpp"    
#include "imgproc/imgproc.hpp"    

#include 
#include 
using namespace std;
using namespace cv;
using namespace Eigen;

int main()
{
	int t = time(NULL);
	const string File = "5.jpg";
	Mat imageSource = imread(File, 0);
	for (unsigned int i = 0; i < imageSource.rows; i++)
		for (unsigned j = 0; j < imageSource.cols; j++)
			if (imageSource.at(i, j) != 0)imageSource.at(i, j) = 255;
	namedWindow("Source Image");
	imshow("Source Image", imageSource);

	Mat image;
	GaussianBlur(imageSource, image, Size(15, 15), 0);
	Canny(image, image, 100, 250);
	vector> contours;
	vector hierarchy;
	findContours(image, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE, Point());
	Mat imageContours = Mat::zeros(image.size(), CV_8UC1);
	Mat Contours = Mat::zeros(image.size(), CV_8UC1);  //绘制  
													   //contours[i]代表的是第i个轮廓,contours[i].size()代表的是第i个轮廓上所有的像素点数  
	int cont_area_M = 0;
	for (int i = 0; i < contours.size(); i++) {
		if (contourArea(contours[i]) > cont_area_M)
			cont_area_M = contourArea(contours[i]);
		for (int j = 0; j < contours[i].size(); j++)
		{
			//绘制出contours向量内所有的像素点  
			Point P = Point(contours[i][j].x, contours[i][j].y);
			Contours.at(P) = 255;

		}
	}
	//绘制轮廓  


	auto itc = contours.begin();

	while (itc != contours.end())
	{
		if (contourArea(*itc) < cont_area_M - 1)
			itc = contours.erase(itc);
		else
			++itc;

	}

	drawContours(imageContours, contours, 0, Scalar(255), 1, 8, hierarchy, 0);

	Rect rect = boundingRect(contours[0]);

	Mat ima = imread("55.jpg", 1);
	Mat ima1 = imread("555.jpg", 1); Point p0(50, 215);
	Point p1(0, 0);

	int n = 0;


	SparseMatrix< double, ColMajor> src1((rect.height + 2)*(rect.width + 2), (rect.height + 2)*(rect.width + 2));
	VectorXd src20((rect.height + 2)*(rect.width + 2));
	VectorXd src21((rect.height + 2)*(rect.width + 2));
	VectorXd src22((rect.height + 2)*(rect.width + 2));


	for (int i = rect.y - 1; i < rect.height + rect.y + 1; i++)
		for (int j = rect.x - 1; j < rect.width + rect.x + 1; j++)
		{
			p1.x = j; p1.y = i;
			if (pointPolygonTest(contours[0], p1, false) >= 0)
			{
				src1.insert(n, n) = 4;
				src1.insert(n, n - 1) = -1;
				src1.insert(n, n + 1) = -1;
				src1.insert(n, n - rect.width - 2) = -1;
				src1.insert(n, n + rect.width + 2) = -1;
				src20(n) = 4*ima.at(p1)[0]- ima.at(p1+Point(0,1))[0] - ima.at(p1+Point(1, 0))[0] - ima.at(p1-Point(0,1))[0]- ima.at(p1 - Point(1, 0))[0];
				src21(n) =  4 * ima.at(p1)[1] - ima.at(p1 + Point(0, 1))[1] - ima.at(p1 + Point(1, 0))[1] - ima.at(p1 - Point(0, 1))[1] - ima.at(p1 - Point(1, 0))[1];
				src22(n) = 4 * ima.at(p1)[2] - ima.at(p1 + Point(0, 1))[2] - ima.at(p1 + Point(1, 0))[2] - ima.at(p1 - Point(0, 1))[2] - ima.at(p1 - Point(1, 0))[2];
			}
			else {
				src1.insert(n, n) = 1;
				src20(n) = ima1.at(p1+p0)[0];
				src21(n) = ima1.at(p1 + p0)[1];
				src22(n) = ima1.at(p1 + p0)[2];

			};
			++n;
		}
	//	cout << src1;


	VectorXd dst00; VectorXd dst01; VectorXd dst02;
	SparseLU , COLAMDOrdering< int >>  solver;
	src1.makeCompressed();
	solver.compute(src1);
	//	solver.analyzePattern(src1);
	//	solver.factorize(src1);
	dst00 = solver.solve(src20);
	dst01 = solver.solve(src21);
	dst02 = solver.solve(src22);


	n = 0;





	
	for (int i = rect.y - 1; i < rect.height + rect.y + 1; i++)
		for (int j = rect.x - 1; j < rect.width + rect.x + 1; j++) {
			p1.x = j; p1.y = i;
			if (pointPolygonTest(contours[0], p1, false) > 0) {
				ima1.at(i + p0.y, j + p0.x)[0] =  dst00(n) < 0?0: dst00(n);
				ima1.at(i + p0.y, j + p0.x)[1] = dst01(n)< 0 ? 0 : dst01(n);
				ima1.at(i + p0.y, j + p0.x)[2] = dst02(n)< 0 ? 0 : dst02(n);
			}

			n++;
		}


		
	imshow("Contours Image", imageContours); //轮廓  
	imshow("Point of Contours", Contours);   //向量contours内保存的所有轮廓点集  
	imshow("Poin", ima1);
	cout<<(time(NULL) -t );
	waitKey(0);

	system("pause");
	return 0;
}

  5.jpg

vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)_第1张图片

  55.jpg

vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)_第2张图片

  555.jpg

vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)_第3张图片

  生成的图片

vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)_第4张图片

 

posted @ 2018-01-10 17:01 ff_d 阅读( ...) 评论( ...) 编辑 收藏

你可能感兴趣的:(vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合))