Home | History | Annotate | Download | only in src
      1 /*M///////////////////////////////////////////////////////////////////////////////////////
      2 //
      3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
      4 //
      5 //  By downloading, copying, installing or using the software you agree to this license.
      6 //  If you do not agree to this license, do not download, install,
      7 //  copy or use the software.
      8 //
      9 //
     10 //                           License Agreement
     11 //                For Open Source Computer Vision Library
     12 //
     13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
     14 // Third party copyrights are property of their respective owners.
     15 //
     16 // Redistribution and use in source and binary forms, with or without modification,
     17 // are permitted provided that the following conditions are met:
     18 //
     19 //   * Redistribution's of source code must retain the above copyright notice,
     20 //     this list of conditions and the following disclaimer.
     21 //
     22 //   * Redistribution's in binary form must reproduce the above copyright notice,
     23 //     this list of conditions and the following disclaimer in the documentation
     24 //     and/or other materials provided with the distribution.
     25 //
     26 //   * The name of the copyright holders may not be used to endorse or promote products
     27 //     derived from this software without specific prior written permission.
     28 //
     29 // This software is provided by the copyright holders and contributors "as is" and
     30 // any express or implied warranties, including, but not limited to, the implied
     31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     32 // In no event shall the Intel Corporation or contributors be liable for any direct,
     33 // indirect, incidental, special, exemplary, or consequential damages
     34 // (including, but not limited to, procurement of substitute goods or services;
     35 // loss of use, data, or profits; or business interruption) however caused
     36 // and on any theory of liability, whether in contract, strict liability,
     37 // or tort (including negligence or otherwise) arising in any way out of
     38 // the use of this software, even if advised of the possibility of such damage.
     39 //
     40 //M*/
     41 
     42 #include "precomp.hpp"
     43 #include "opencv2/photo.hpp"
     44 #include "opencv2/imgproc.hpp"
     45 #include "hdr_common.hpp"
     46 
     47 namespace cv
     48 {
     49 
     50 class MergeDebevecImpl : public MergeDebevec
     51 {
     52 public:
     53     MergeDebevecImpl() :
     54         name("MergeDebevec"),
     55         weights(tringleWeights())
     56     {
     57     }
     58 
     59     void process(InputArrayOfArrays src, OutputArray dst, InputArray _times, InputArray input_response)
     60     {
     61         std::vector<Mat> images;
     62         src.getMatVector(images);
     63         Mat times = _times.getMat();
     64 
     65         CV_Assert(images.size() == times.total());
     66         checkImageDimensions(images);
     67         CV_Assert(images[0].depth() == CV_8U);
     68 
     69         int channels = images[0].channels();
     70         Size size = images[0].size();
     71         int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
     72 
     73         dst.create(images[0].size(), CV_32FCC);
     74         Mat result = dst.getMat();
     75 
     76         Mat response = input_response.getMat();
     77 
     78         if(response.empty()) {
     79             response = linearResponse(channels);
     80             response.at<Vec3f>(0) = response.at<Vec3f>(1);
     81         }
     82         log(response, response);
     83         CV_Assert(response.rows == LDR_SIZE && response.cols == 1 &&
     84                   response.channels() == channels);
     85 
     86         Mat exp_values(times);
     87         log(exp_values, exp_values);
     88 
     89         result = Mat::zeros(size, CV_32FCC);
     90         std::vector<Mat> result_split;
     91         split(result, result_split);
     92         Mat weight_sum = Mat::zeros(size, CV_32F);
     93 
     94         for(size_t i = 0; i < images.size(); i++) {
     95             std::vector<Mat> splitted;
     96             split(images[i], splitted);
     97 
     98             Mat w = Mat::zeros(size, CV_32F);
     99             for(int c = 0; c < channels; c++) {
    100                 LUT(splitted[c], weights, splitted[c]);
    101                 w += splitted[c];
    102             }
    103             w /= channels;
    104 
    105             Mat response_img;
    106             LUT(images[i], response, response_img);
    107             split(response_img, splitted);
    108             for(int c = 0; c < channels; c++) {
    109                 result_split[c] += w.mul(splitted[c] - exp_values.at<float>((int)i));
    110             }
    111             weight_sum += w;
    112         }
    113         weight_sum = 1.0f / weight_sum;
    114         for(int c = 0; c < channels; c++) {
    115             result_split[c] = result_split[c].mul(weight_sum);
    116         }
    117         merge(result_split, result);
    118         exp(result, result);
    119     }
    120 
    121     void process(InputArrayOfArrays src, OutputArray dst, InputArray times)
    122     {
    123         process(src, dst, times, Mat());
    124     }
    125 
    126 protected:
    127     String name;
    128     Mat weights;
    129 };
    130 
    131 Ptr<MergeDebevec> createMergeDebevec()
    132 {
    133     return makePtr<MergeDebevecImpl>();
    134 }
    135 
    136 class MergeMertensImpl : public MergeMertens
    137 {
    138 public:
    139     MergeMertensImpl(float _wcon, float _wsat, float _wexp) :
    140         name("MergeMertens"),
    141         wcon(_wcon),
    142         wsat(_wsat),
    143         wexp(_wexp)
    144     {
    145     }
    146 
    147     void process(InputArrayOfArrays src, OutputArrayOfArrays dst, InputArray, InputArray)
    148     {
    149         process(src, dst);
    150     }
    151 
    152     void process(InputArrayOfArrays src, OutputArray dst)
    153     {
    154         std::vector<Mat> images;
    155         src.getMatVector(images);
    156         checkImageDimensions(images);
    157 
    158         int channels = images[0].channels();
    159         CV_Assert(channels == 1 || channels == 3);
    160         Size size = images[0].size();
    161         int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
    162 
    163         std::vector<Mat> weights(images.size());
    164         Mat weight_sum = Mat::zeros(size, CV_32F);
    165 
    166         for(size_t i = 0; i < images.size(); i++) {
    167             Mat img, gray, contrast, saturation, wellexp;
    168             std::vector<Mat> splitted(channels);
    169 
    170             images[i].convertTo(img, CV_32F, 1.0f/255.0f);
    171             if(channels == 3) {
    172                 cvtColor(img, gray, COLOR_RGB2GRAY);
    173             } else {
    174                 img.copyTo(gray);
    175             }
    176             split(img, splitted);
    177 
    178             Laplacian(gray, contrast, CV_32F);
    179             contrast = abs(contrast);
    180 
    181             Mat mean = Mat::zeros(size, CV_32F);
    182             for(int c = 0; c < channels; c++) {
    183                 mean += splitted[c];
    184             }
    185             mean /= channels;
    186 
    187             saturation = Mat::zeros(size, CV_32F);
    188             for(int c = 0; c < channels;  c++) {
    189                 Mat deviation = splitted[c] - mean;
    190                 pow(deviation, 2.0f, deviation);
    191                 saturation += deviation;
    192             }
    193             sqrt(saturation, saturation);
    194 
    195             wellexp = Mat::ones(size, CV_32F);
    196             for(int c = 0; c < channels; c++) {
    197                 Mat exp = splitted[c] - 0.5f;
    198                 pow(exp, 2.0f, exp);
    199                 exp = -exp / 0.08f;
    200                 wellexp = wellexp.mul(exp);
    201             }
    202 
    203             pow(contrast, wcon, contrast);
    204             pow(saturation, wsat, saturation);
    205             pow(wellexp, wexp, wellexp);
    206 
    207             weights[i] = contrast;
    208             if(channels == 3) {
    209                 weights[i] = weights[i].mul(saturation);
    210             }
    211             weights[i] = weights[i].mul(wellexp) + 1e-12f;
    212             weight_sum += weights[i];
    213         }
    214         int maxlevel = static_cast<int>(logf(static_cast<float>(min(size.width, size.height))) / logf(2.0f));
    215         std::vector<Mat> res_pyr(maxlevel + 1);
    216 
    217         for(size_t i = 0; i < images.size(); i++) {
    218             weights[i] /= weight_sum;
    219             Mat img;
    220             images[i].convertTo(img, CV_32F, 1.0f/255.0f);
    221 
    222             std::vector<Mat> img_pyr, weight_pyr;
    223             buildPyramid(img, img_pyr, maxlevel);
    224             buildPyramid(weights[i], weight_pyr, maxlevel);
    225 
    226             for(int lvl = 0; lvl < maxlevel; lvl++) {
    227                 Mat up;
    228                 pyrUp(img_pyr[lvl + 1], up, img_pyr[lvl].size());
    229                 img_pyr[lvl] -= up;
    230             }
    231             for(int lvl = 0; lvl <= maxlevel; lvl++) {
    232                 std::vector<Mat> splitted(channels);
    233                 split(img_pyr[lvl], splitted);
    234                 for(int c = 0; c < channels; c++) {
    235                     splitted[c] = splitted[c].mul(weight_pyr[lvl]);
    236                 }
    237                 merge(splitted, img_pyr[lvl]);
    238                 if(res_pyr[lvl].empty()) {
    239                     res_pyr[lvl] = img_pyr[lvl];
    240                 } else {
    241                     res_pyr[lvl] += img_pyr[lvl];
    242                 }
    243             }
    244         }
    245         for(int lvl = maxlevel; lvl > 0; lvl--) {
    246             Mat up;
    247             pyrUp(res_pyr[lvl], up, res_pyr[lvl - 1].size());
    248             res_pyr[lvl - 1] += up;
    249         }
    250         dst.create(size, CV_32FCC);
    251         res_pyr[0].copyTo(dst.getMat());
    252     }
    253 
    254     float getContrastWeight() const { return wcon; }
    255     void setContrastWeight(float val) { wcon = val; }
    256 
    257     float getSaturationWeight() const { return wsat; }
    258     void setSaturationWeight(float val) { wsat = val; }
    259 
    260     float getExposureWeight() const { return wexp; }
    261     void setExposureWeight(float val) { wexp = val; }
    262 
    263     void write(FileStorage& fs) const
    264     {
    265         fs << "name" << name
    266            << "contrast_weight" << wcon
    267            << "saturation_weight" << wsat
    268            << "exposure_weight" << wexp;
    269     }
    270 
    271     void read(const FileNode& fn)
    272     {
    273         FileNode n = fn["name"];
    274         CV_Assert(n.isString() && String(n) == name);
    275         wcon = fn["contrast_weight"];
    276         wsat = fn["saturation_weight"];
    277         wexp = fn["exposure_weight"];
    278     }
    279 
    280 protected:
    281     String name;
    282     float wcon, wsat, wexp;
    283 };
    284 
    285 Ptr<MergeMertens> createMergeMertens(float wcon, float wsat, float wexp)
    286 {
    287     return makePtr<MergeMertensImpl>(wcon, wsat, wexp);
    288 }
    289 
    290 class MergeRobertsonImpl : public MergeRobertson
    291 {
    292 public:
    293     MergeRobertsonImpl() :
    294         name("MergeRobertson"),
    295         weight(RobertsonWeights())
    296     {
    297     }
    298 
    299     void process(InputArrayOfArrays src, OutputArray dst, InputArray _times, InputArray input_response)
    300     {
    301         std::vector<Mat> images;
    302         src.getMatVector(images);
    303         Mat times = _times.getMat();
    304 
    305         CV_Assert(images.size() == times.total());
    306         checkImageDimensions(images);
    307         CV_Assert(images[0].depth() == CV_8U);
    308 
    309         int channels = images[0].channels();
    310         int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
    311 
    312         dst.create(images[0].size(), CV_32FCC);
    313         Mat result = dst.getMat();
    314 
    315         Mat response = input_response.getMat();
    316         if(response.empty()) {
    317             float middle = LDR_SIZE / 2.0f;
    318             response = linearResponse(channels) / middle;
    319         }
    320         CV_Assert(response.rows == LDR_SIZE && response.cols == 1 &&
    321                   response.channels() == channels);
    322 
    323         result = Mat::zeros(images[0].size(), CV_32FCC);
    324         Mat wsum = Mat::zeros(images[0].size(), CV_32FCC);
    325         for(size_t i = 0; i < images.size(); i++) {
    326             Mat im, w;
    327             LUT(images[i], weight, w);
    328             LUT(images[i], response, im);
    329 
    330             result += times.at<float>((int)i) * w.mul(im);
    331             wsum += times.at<float>((int)i) * times.at<float>((int)i) * w;
    332         }
    333         result = result.mul(1 / wsum);
    334     }
    335 
    336     void process(InputArrayOfArrays src, OutputArray dst, InputArray times)
    337     {
    338         process(src, dst, times, Mat());
    339     }
    340 
    341 protected:
    342     String name;
    343     Mat weight;
    344 };
    345 
    346 Ptr<MergeRobertson> createMergeRobertson()
    347 {
    348     return makePtr<MergeRobertsonImpl>();
    349 }
    350 
    351 }
    352