• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * cv_image_process_helper.cpp - OpenCV image processing helpers functions
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_image_process_helper.h"
23 
24 namespace XCam {
25 
26 
CVImageProcessHelper()27 CVImageProcessHelper::CVImageProcessHelper ()
28     : CVBaseClass ()
29 {
30 
31 }
32 
33 cv::Mat
erosion(const cv::Mat & image,int erosion_size,int erosion_type)34 CVImageProcessHelper::erosion (const cv::Mat &image, int erosion_size, int erosion_type)
35 {
36     cv::Mat element = cv::getStructuringElement (erosion_type,
37                       cv::Size (2 * erosion_size + 1, 2 * erosion_size + 1),
38                       cv::Point (erosion_size, erosion_size));
39     cv::Mat eroded;
40     cv::erode (image, eroded, element);
41     return eroded.clone ();
42 }
43 
44 float
get_snr(const cv::Mat & noisy,const cv::Mat & noiseless)45 CVImageProcessHelper::get_snr (const cv::Mat &noisy, const cv::Mat &noiseless)
46 {
47     cv::Mat temp_noisy, temp_noiseless;
48     noisy.convertTo (temp_noisy, CV_32FC1);
49     noiseless.convertTo (temp_noiseless, CV_32FC1);
50     cv::Mat numerator, denominator;
51     cv::pow (temp_noisy, 2, numerator);
52     cv::pow (temp_noisy - temp_noiseless, 2, denominator);
53     float res = cv::sum (numerator)[0] / cv::sum (denominator)[0];
54     res = sqrt (res);
55     return res;
56 }
57 
58 void
compute_dft(const cv::Mat & image,cv::Mat & result)59 CVImageProcessHelper::compute_dft (const cv::Mat &image, cv::Mat &result)
60 {
61     cv::Mat padded;
62     int m = cv::getOptimalDFTSize (image.rows);
63     int n = cv::getOptimalDFTSize (image.cols);
64     cv::copyMakeBorder (image, padded, 0, m - image.rows, 0, n - image.cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));
65     cv::Mat planes[] = {cv::Mat_<float> (padded), cv::Mat::zeros (padded.size (), CV_32FC1)};
66     cv::merge (planes, 2, result);
67     cv::dft (result, result);
68 }
69 
70 void
compute_idft(cv::Mat * input,cv::Mat & result)71 CVImageProcessHelper::compute_idft (cv::Mat *input, cv::Mat &result)
72 {
73     cv::Mat fimg;
74     cv::merge (input, 2, fimg);
75     cv::idft (fimg, result, cv::DFT_REAL_OUTPUT + cv::DFT_SCALE);
76 }
77 
78 void
apply_constraints(cv::Mat & image,float threshold_min_value,float threshold_max_value,float min_value,float max_value)79 CVImageProcessHelper::apply_constraints (cv::Mat &image, float threshold_min_value, float threshold_max_value, float min_value, float max_value)
80 {
81     for (int i = 0; i < image.rows; i++) {
82         for (int j = 0; j < image.cols; j++) {
83             if (image.at<float>(i, j) < threshold_min_value)
84             {
85                 image.at<float>(i, j) = min_value;
86             }
87             if (image.at<float>(i, j) > threshold_max_value)
88             {
89                 image.at<float>(i, j) = max_value;
90             }
91         }
92     }
93 }
94 
95 void
normalize_weights(cv::Mat & weights)96 CVImageProcessHelper::normalize_weights (cv::Mat &weights)
97 {
98     weights.convertTo (weights, CV_32FC1);
99     float sum = cv::sum (weights)[0];
100     weights /= sum;
101 }
102 
103 }
104