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
|
void CreateGaussianPyramid(const Mat& img, int num_levels, vector<Mat>& pyr) { pyr.clear(); Mat gp_img = img; pyr.push_back(gp_img); for (size_t i = 0; i < num_levels; i++) { Mat down; pyrDown(gp_img, down); pyr.push_back(down); gp_img = down; } }
void CreateLaplacianPyramid(const Mat& img, int num_levels, vector<Mat>& pyr, Mat& highest_level) { pyr.clear(); Mat gp_img = img; for (size_t i = 0; i < num_levels; i++) { Mat down, up; pyrDown(gp_img, down); pyrUp(down, up, gp_img.size()); Mat lap; subtract(gp_img, up, lap); pyr.push_back(lap); gp_img = down; } gp_img.copyTo(highest_level); }
void FusionPyramidImage(vector<Mat>& pyr_img1, Mat& img1, vector<Mat>& pyr_img2, Mat& img2, vector<Mat>& pyr_mask, int num_levels, vector<Mat>& pyr, Mat& result_highest_level) { pyr.clear(); result_highest_level = img1.mul(pyr_mask.back()) + img2.mul(Scalar(1.0, 1.0, 1.0) - pyr_mask.back()); for (size_t i = 0; i < num_levels; i++) { pyr.push_back(pyr_img1[i].mul(pyr_mask[i]) + pyr_img2[i].mul(Scalar(1.0, 1.0, 1.0) - pyr_mask[i])); } }
Mat ReconstructImg(vector<Mat>& pyr, Mat& img, int num_levels) { Mat current_img = img; for (int i = num_levels - 1; i >= 0; i--) { Mat up; pyrUp(current_img, up, pyr[i].size()); add(up, pyr[i], current_img); } return current_img; }
int main() { Mat img_apple = imread("apple.jpg"); Mat img_orange = imread("orange.jpg"); Mat img_mask = imread("mask.jpg");
int num_bands_ = 5;
img_apple.convertTo(img_apple, CV_32F); img_orange.convertTo(img_orange, CV_32F); img_mask.convertTo(img_mask, CV_32F, 1.0 / 255.0);
vector<Mat> pyr_apple; Mat highest_level_apple; CreateLaplacianPyramid(img_apple, num_bands_, pyr_apple, highest_level_apple); vector<Mat> pyr_orange; Mat highest_level_orange; CreateLaplacianPyramid(img_orange, num_bands_, pyr_orange, highest_level_orange); vector<Mat> pyr_mask; CreateGaussianPyramid(img_mask, num_bands_, pyr_mask);
vector<Mat> pyr_result; Mat result_highest_level;
FusionPyramidImage(pyr_orange, highest_level_orange, pyr_apple, highest_level_apple, pyr_mask, num_bands_, pyr_result, result_highest_level);
Mat result = ReconstructImg(pyr_result, result_highest_level, num_bands_); result.convertTo(result, CV_8UC3);
imshow("result", result); }
|