1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include "opencv2/opencv.hpp"
using namespace cv; using namespace std;
void getLaplacianPyramid(Mat& guassianPyramid, Mat& laplacianPyramid){ Mat downSampled; pyrDown(guassianPyramid,downSampled);
Mat blurred; pyrUp(downSampled,blurred);
subtract(guassianPyramid, blurred, laplacianPyramid);
}
void combineImages(Mat& A, Mat& B, Mat& mask, Mat& destination){ destination = Mat::zeros(A.rows, A.cols, CV_32FC3); for(int y = 0; y < A.rows; y++) { for(int x = 0; x < A.cols; x++) { Vec3f a = A.at<Vec3f>(Point(x,y)); Vec3f b = B.at<Vec3f>(Point(x,y)); Vec3f m = mask.at<Vec3f>(Point(x,y)); float b_ = a[0]*m[0]+(1-m[0])*b[0]; float g_ = a[1]*m[1]+(1-m[1])*b[1]; float r_ = a[2]*m[2]+(1-m[2])*b[2];
destination.at<Vec3f>(y,x)[0] = b_; destination.at<Vec3f>(y,x)[1] = g_; destination.at<Vec3f>(y,x)[2] = r_; } } }
int main( int argc, char** argv ) { Mat A = imread("images/man.jpg"); Mat B = imread("images/woman.jpg");
A.convertTo(A, CV_32FC3, 1/255.0); B.convertTo(B, CV_32FC3, 1/255.0);
Mat mask = Mat::zeros(A.rows, A.cols, CV_8UC3);
Point points[11]; points[0] = Point(164,226); points[1] = Point(209,225); points[2] = Point(238,188); points[3] = Point(252,133); points[4] = Point(248,75); points[5] = Point(240,29); points[6] = Point(192,15); points[7] = Point(150,15); points[8] = Point(100,70); points[9] = Point(106,133); points[10] = Point(123,194);
const Point* polygon[1] = {points}; int npt[] = {11}; fillPoly(mask, polygon, npt, 1, Scalar(255, 255, 255));
mask.convertTo(mask, CV_32FC3, 1/255.0);
mask = mask * 0.7;
resize(A, A, Size(384,352));
resize(B, B, A.size()); resize(mask, mask, A.size());
Mat guassianA = A.clone(); Mat guassianB = B.clone(); Mat guassianMask = mask.clone();
int maxIterations = 2;
vector<Mat> combinedLaplacianPyramids;
for (int i = 0; i < maxIterations; i++){ Mat laplacianA; getLaplacianPyramid(guassianA,laplacianA);
Mat laplacianB; getLaplacianPyramid(guassianB,laplacianB);
Mat combinedLaplacian; combineImages(laplacianA, laplacianB, guassianMask, combinedLaplacian); combinedLaplacianPyramids.insert(combinedLaplacianPyramids.begin(),combinedLaplacian);
pyrDown(guassianA,guassianA); pyrDown(guassianB,guassianB); pyrDown(guassianMask,guassianMask);
}
Mat lastCombined; combineImages(guassianA, guassianB, guassianMask, lastCombined);
combinedLaplacianPyramids.insert(combinedLaplacianPyramids.begin(),lastCombined);
Mat blendedImage = combinedLaplacianPyramids[0];
for (int i = 1; i < combinedLaplacianPyramids.size(); i++){ pyrUp(blendedImage,blendedImage); add(blendedImage, combinedLaplacianPyramids[i],blendedImage); }
imshow("blended",blendedImage);
Mat directCombined; combineImages(A, B, mask, directCombined); imshow("directCombined",directCombined); waitKey(0);
return 0; }
|