1 /*
2 * cv_image_deblurring.cpp - iterative blind deblurring
3 *
4 * Copyright (c) 2016-2017 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Andrey Parfenov <a1994ndrey@gmail.com>
19 * Author: Wind Yuan <feng.yuan@intel.com>
20 */
21
22 #include "cv_wiener_filter.h"
23
24 namespace XCam {
25
26
CVWienerFilter()27 CVWienerFilter::CVWienerFilter ()
28 : CVBaseClass ()
29 {
30 _helpers = new CVImageProcessHelper ();
31 }
32
33 void
wiener_filter(const cv::Mat & blurred_image,const cv::Mat & known,cv::Mat & unknown,float noise_power)34 CVWienerFilter::wiener_filter (const cv::Mat &blurred_image, const cv::Mat &known, cv::Mat &unknown, float noise_power)
35 {
36 int image_w = blurred_image.size ().width;
37 int image_h = blurred_image.size ().height;
38 cv::Mat y_ft;
39 _helpers->compute_dft (blurred_image, y_ft);
40
41 cv::Mat padded = cv::Mat::zeros (image_h, image_w, CV_32FC1);
42 int padx = padded.cols - known.cols;
43 int pady = padded.rows - known.rows;
44 cv::copyMakeBorder (known, padded, 0, pady, 0, padx, cv::BORDER_CONSTANT, cv::Scalar::all(0));
45 cv::Mat padded_ft;
46 _helpers->compute_dft (padded, padded_ft);
47
48 cv::Mat temp_unknown;
49 cv::Mat unknown_ft[2];
50 unknown_ft[0] = cv::Mat::zeros (image_h, image_w, CV_32FC1);
51 unknown_ft[1] = cv::Mat::zeros (image_h, image_w, CV_32FC1);
52
53 cv::Mat denominator;
54 cv::Mat denominator_splitted[] = {cv::Mat::zeros (blurred_image.size (), CV_32FC1), cv::Mat::zeros (blurred_image.size (), CV_32FC1)};
55 cv::mulSpectrums (padded_ft, padded_ft, denominator, 0, true);
56 cv::split (denominator, denominator_splitted);
57 denominator_splitted[0] = denominator_splitted[0] (cv::Rect (0, 0, blurred_image.cols, blurred_image.rows));
58 denominator_splitted[0] += cv::Scalar (noise_power);
59
60 cv::Mat numerator;
61 cv::Mat numerator_splitted[] = {cv::Mat::zeros (blurred_image.size (), CV_32FC1), cv::Mat::zeros (blurred_image.size (), CV_32FC1)};
62 cv::mulSpectrums (y_ft, padded_ft, numerator, 0, true);
63 cv::split (numerator, numerator_splitted);
64 numerator_splitted[0] = numerator_splitted[0] (cv::Rect (0, 0, blurred_image.cols, blurred_image.rows));
65 numerator_splitted[1] = numerator_splitted[1] (cv::Rect (0, 0, blurred_image.cols, blurred_image.rows));
66 cv::divide (numerator_splitted[0], denominator_splitted[0], unknown_ft[0]);
67 cv::divide (numerator_splitted[1], denominator_splitted[0], unknown_ft[1]);
68 _helpers->compute_idft (unknown_ft, temp_unknown);
69 unknown = temp_unknown.clone();
70 }
71
72 }
73