vs2015+opencv3.3.1 +Eigen 3.3.4 c++实现 薄膜插值 泊松图像编辑(v=0||Δf=0)
#include "core/core.hpp" #include "highgui/highgui.hpp" #include "imgproc/imgproc.hpp" #include "iostream" #include <Eigen/Sparse> using namespace std; using namespace cv; using namespace Eigen; int main() { const string File = "789.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<uchar>(i, j) != 0)imageSource.at<uchar>(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<vector<Point>> contours; vector<Vec4i> 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<uchar>(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("7892.jpg", 1); 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) = 0; src21(n) = 0; src22(n) = 0; } else { src1.insert(n, n) = 1; src20(n) = ima.at<Vec3b>(p1)[0]; src21(n) = ima.at<Vec3b>(p1)[1]; src22(n) = ima.at<Vec3b>(p1)[2]; }; ++n; } // cout << src1; VectorXd dst00; VectorXd dst01; VectorXd dst02; SparseLU <SparseMatrix <double, ColMajor>> solver; 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++) { ima.at<Vec3b>(i, j)[0] = dst00(n); ima.at<Vec3b>(i, j)[1] = dst01(n); ima.at<Vec3b>(i, j)[2] = dst02(n); n++; } imshow("Contours Image", imageContours); //轮廓 imshow("Point of Contours", Contours); //向量contours内保存的所有轮廓点集 imshow("Poin", ima); waitKey(0); system("pause"); return 0; }
789.jpg
7892.jpg
结果
大概十几s到40s出结果。