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 45 #include "seamless_cloning.hpp" 46 47 using namespace std; 48 using namespace cv; 49 50 void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point p, OutputArray _blend, int flags) 51 { 52 const Mat src = _src.getMat(); 53 const Mat dest = _dst.getMat(); 54 const Mat mask = _mask.getMat(); 55 _blend.create(dest.size(), CV_8UC3); 56 Mat blend = _blend.getMat(); 57 58 int minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN; 59 int h = mask.size().height; 60 int w = mask.size().width; 61 62 Mat gray = Mat(mask.size(),CV_8UC1); 63 Mat dst_mask = Mat::zeros(dest.size(),CV_8UC1); 64 Mat cs_mask = Mat::zeros(src.size(),CV_8UC3); 65 Mat cd_mask = Mat::zeros(dest.size(),CV_8UC3); 66 67 if(mask.channels() == 3) 68 cvtColor(mask, gray, COLOR_BGR2GRAY ); 69 else 70 gray = mask; 71 72 for(int i=0;i<h;i++) 73 { 74 for(int j=0;j<w;j++) 75 { 76 if(gray.at<uchar>(i,j) == 255) 77 { 78 minx = std::min(minx,i); 79 maxx = std::max(maxx,i); 80 miny = std::min(miny,j); 81 maxy = std::max(maxy,j); 82 } 83 } 84 } 85 86 int lenx = maxx - minx; 87 int leny = maxy - miny; 88 89 Mat patch = Mat::zeros(Size(leny, lenx), CV_8UC3); 90 91 int minxd = p.y - lenx/2; 92 int maxxd = p.y + lenx/2; 93 int minyd = p.x - leny/2; 94 int maxyd = p.x + leny/2; 95 96 CV_Assert(minxd >= 0 && minyd >= 0 && maxxd <= dest.rows && maxyd <= dest.cols); 97 98 Rect roi_d(minyd,minxd,leny,lenx); 99 Rect roi_s(miny,minx,leny,lenx); 100 101 Mat destinationROI = dst_mask(roi_d); 102 Mat sourceROI = cs_mask(roi_s); 103 104 gray(roi_s).copyTo(destinationROI); 105 src(roi_s).copyTo(sourceROI,gray(roi_s)); 106 src(roi_s).copyTo(patch, gray(roi_s)); 107 108 destinationROI = cd_mask(roi_d); 109 cs_mask(roi_s).copyTo(destinationROI); 110 111 112 Cloning obj; 113 obj.normalClone(dest,cd_mask,dst_mask,blend,flags); 114 115 } 116 117 void cv::colorChange(InputArray _src, InputArray _mask, OutputArray _dst, float r, float g, float b) 118 { 119 Mat src = _src.getMat(); 120 Mat mask = _mask.getMat(); 121 _dst.create(src.size(), src.type()); 122 Mat blend = _dst.getMat(); 123 124 float red = r; 125 float green = g; 126 float blue = b; 127 128 Mat gray = Mat::zeros(mask.size(),CV_8UC1); 129 130 if(mask.channels() == 3) 131 cvtColor(mask, gray, COLOR_BGR2GRAY ); 132 else 133 gray = mask; 134 135 Mat cs_mask = Mat::zeros(src.size(),CV_8UC3); 136 137 src.copyTo(cs_mask,gray); 138 139 Cloning obj; 140 obj.localColorChange(src,cs_mask,gray,blend,red,green,blue); 141 } 142 143 void cv::illuminationChange(InputArray _src, InputArray _mask, OutputArray _dst, float a, float b) 144 { 145 146 Mat src = _src.getMat(); 147 Mat mask = _mask.getMat(); 148 _dst.create(src.size(), src.type()); 149 Mat blend = _dst.getMat(); 150 float alpha = a; 151 float beta = b; 152 153 Mat gray = Mat::zeros(mask.size(),CV_8UC1); 154 155 if(mask.channels() == 3) 156 cvtColor(mask, gray, COLOR_BGR2GRAY ); 157 else 158 gray = mask; 159 160 Mat cs_mask = Mat::zeros(src.size(),CV_8UC3); 161 162 src.copyTo(cs_mask,gray); 163 164 Cloning obj; 165 obj.illuminationChange(src,cs_mask,gray,blend,alpha,beta); 166 167 } 168 169 void cv::textureFlattening(InputArray _src, InputArray _mask, OutputArray _dst, 170 float low_threshold, float high_threshold, int kernel_size) 171 { 172 173 Mat src = _src.getMat(); 174 Mat mask = _mask.getMat(); 175 _dst.create(src.size(), src.type()); 176 Mat blend = _dst.getMat(); 177 178 Mat gray = Mat::zeros(mask.size(),CV_8UC1); 179 180 if(mask.channels() == 3) 181 cvtColor(mask, gray, COLOR_BGR2GRAY ); 182 else 183 gray = mask; 184 185 Mat cs_mask = Mat::zeros(src.size(),CV_8UC3); 186 187 src.copyTo(cs_mask,gray); 188 189 Cloning obj; 190 obj.textureFlatten(src,cs_mask,gray,low_threshold,high_threshold,kernel_size,blend); 191 } 192