From e50b12efbd2e8d980ee3106881b70ec88265671a Mon Sep 17 00:00:00 2001 From: arnjunmo <51722592+arnjunmo@users.noreply.github.com> Date: Mon, 23 Nov 2020 02:00:19 +0900 Subject: [PATCH] Add files via upload --- blending.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 blending.cpp diff --git a/blending.cpp b/blending.cpp new file mode 100644 index 0000000..76fe7ad --- /dev/null +++ b/blending.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include + +#define DEBUG 1 + +using namespace std; +using namespace cv; + +#define HISTMATCH_EPSILON 0.000001 +#define BTM_DEBUG +// Compute histogram and CDF for an image with mask +void do1ChnHist(const Mat_& img, const Mat_& mask, Mat_& h, Mat_& cdf) +{ + for (size_t p = 0; p < img.total(); p++) + { + if (mask(p) > 0) + { + uchar c = img(p); + h(c) += 1.0; + } + } + + normalize(h, h, 1, 0, NORM_MINMAX); + + cdf(0) = h(0); + for (int j = 1; j < 256; j++) + { + cdf(j) = cdf(j - 1) + h(j); + } + + normalize(cdf, cdf, 1, 0, NORM_MINMAX); +} + +// match histograms of 'src' to that of 'dst', according to both masks +void histMatchRGB(Mat& src, const Mat& src_mask, const Mat& dst, const Mat& dst_mask) +{ +#ifdef BTM_DEBUG + namedWindow("original source", CV_WINDOW_AUTOSIZE); + imshow("original source", src); + namedWindow("original query", CV_WINDOW_AUTOSIZE); + imshow("original query", dst); +#endif + + vector> chns, chns1; + split(src, chns); + split(dst, chns1); + + for (int i = 0; i < 3; i++) + { + Mat_ src_hist = Mat_::zeros(1, 256); + Mat_ dst_hist = Mat_::zeros(1, 256); + Mat_ src_cdf = Mat_::zeros(1, 256); + Mat_ dst_cdf = Mat_::zeros(1, 256); + + do1ChnHist(chns[i], src_mask, src_hist, src_cdf); + do1ChnHist(chns1[i], dst_mask, dst_hist, dst_cdf); + + uchar last = 0; + + Mat_ lut(1, 256); + for (int j = 0; j < src_cdf.cols; j++) + { + double F1j = src_cdf(j); + + for (uchar k = last; k < dst_cdf.cols; k++) + { + double F2k = dst_cdf(k); + if (abs(F2k - F1j) < HISTMATCH_EPSILON || F2k > F1j) + { + lut(j) = k; + last = k; + break; + } + } + } + + LUT(chns[i], lut, chns[i]); + } + + Mat res; + merge(chns, res); + + + res.copyTo(src); +} + + +int _tmain(int argc, _TCHAR* argv[]) +{ + vector images; + Mat matLeftImage; + Mat matRightImage; + + matLeftImage = imread("3.jpg", IMREAD_COLOR); + matRightImage = imread("6.jpg", IMREAD_COLOR); + + Mat mask = Mat(matLeftImage.size(), CV_8U, Scalar(255)); + + histMatchRGB(matRightImage, mask, matLeftImage, mask); + + images.push_back(matLeftImage); + images.push_back(matRightImage); + + Mat result; + + Stitcher stitcher = Stitcher::createDefault(); + + int ok = stitcher.stitch(images, result); + + cout << ok << " " << stitcher.OK << endl; + if (ok == stitcher.OK) + { + imshow("Result", result); + waitKey(0); + } + else + { + cout << "Fail Stitching" << endl; + } + + return 0; + +} \ No newline at end of file