2013-09-03 23:42:05 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2013-08-27 16:08:38 +08:00
|
|
|
#include "precomp.hpp"
|
|
|
|
#include "opencv2/photo.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "npr.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
2013-09-03 23:42:05 +08:00
|
|
|
void cv::edgePreservingFilter(InputArray _src, OutputArray _dst, int flags, float sigma_s, float sigma_r)
|
2013-08-27 16:08:38 +08:00
|
|
|
{
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat I = _src.getMat();
|
|
|
|
_dst.create(I.size(), CV_8UC3);
|
|
|
|
Mat dst = _dst.getMat();
|
2013-08-27 16:08:38 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
int h = I.size().height;
|
|
|
|
int w = I.size().width;
|
2013-08-27 16:08:38 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat res = Mat(h,w,CV_32FC3);
|
|
|
|
dst.convertTo(res,CV_32FC3,1.0/255.0);
|
2013-08-27 16:08:38 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Domain_Filter obj;
|
2013-08-27 16:08:38 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat img = Mat(I.size(),CV_32FC3);
|
|
|
|
I.convertTo(img,CV_32FC3,1.0/255.0);
|
2013-08-27 16:08:38 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
obj.filter(img, res, sigma_s, sigma_r, flags);
|
2013-08-27 16:08:38 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
convertScaleAbs(res, dst, 255,0);
|
2013-08-27 16:08:38 +08:00
|
|
|
}
|
2013-09-03 23:42:05 +08:00
|
|
|
|
|
|
|
void cv::detailEnhance(InputArray _src, OutputArray _dst, float sigma_s, float sigma_r)
|
|
|
|
{
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat I = _src.getMat();
|
|
|
|
_dst.create(I.size(), CV_8UC3);
|
|
|
|
Mat dst = _dst.getMat();
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
int h = I.size().height;
|
|
|
|
int w = I.size().width;
|
|
|
|
float factor = 3.0;
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat img = Mat(I.size(),CV_32FC3);
|
|
|
|
I.convertTo(img,CV_32FC3,1.0/255.0);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-10-17 23:44:31 +08:00
|
|
|
Mat res = Mat(h,w,CV_32FC1);
|
2013-09-14 21:42:25 +08:00
|
|
|
dst.convertTo(res,CV_32FC3,1.0/255.0);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat result = Mat(img.size(),CV_32FC3);
|
|
|
|
Mat lab = Mat(img.size(),CV_32FC3);
|
2013-10-17 23:44:31 +08:00
|
|
|
vector <Mat> lab_channel;
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
cvtColor(img,lab,COLOR_BGR2Lab);
|
2013-10-17 23:44:31 +08:00
|
|
|
split(lab,lab_channel);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat L = Mat(img.size(),CV_32FC1);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-10-17 23:44:31 +08:00
|
|
|
lab_channel[0].convertTo(L,CV_32FC1,1.0/255.0);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Domain_Filter obj;
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
obj.filter(L, res, sigma_s, sigma_r, 1);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat detail = Mat(h,w,CV_32FC1);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-10-17 23:44:31 +08:00
|
|
|
detail = L - res;
|
|
|
|
multiply(detail,factor,detail);
|
|
|
|
L = res + detail;
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-10-17 23:44:31 +08:00
|
|
|
L.convertTo(lab_channel[0],CV_32FC1,255);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-10-17 23:44:31 +08:00
|
|
|
merge(lab_channel,lab);
|
2013-09-14 21:42:25 +08:00
|
|
|
|
|
|
|
cvtColor(lab,result,COLOR_Lab2BGR);
|
|
|
|
result.convertTo(dst,CV_8UC3,255);
|
2013-09-03 23:42:05 +08:00
|
|
|
}
|
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
void cv::pencilSketch(InputArray _src, OutputArray _dst1, OutputArray _dst2, float sigma_s, float sigma_r, float shade_factor)
|
2013-09-03 23:42:05 +08:00
|
|
|
{
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat I = _src.getMat();
|
|
|
|
_dst1.create(I.size(), CV_8UC1);
|
|
|
|
Mat dst1 = _dst1.getMat();
|
|
|
|
|
|
|
|
_dst2.create(I.size(), CV_8UC3);
|
|
|
|
Mat dst2 = _dst2.getMat();
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat img = Mat(I.size(),CV_32FC3);
|
|
|
|
I.convertTo(img,CV_32FC3,1.0/255.0);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Domain_Filter obj;
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat sketch = Mat(I.size(),CV_32FC1);
|
|
|
|
Mat color_sketch = Mat(I.size(),CV_32FC3);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
obj.pencil_sketch(img, sketch, color_sketch, sigma_s, sigma_r, shade_factor);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
sketch.convertTo(dst1,CV_8UC1,255);
|
|
|
|
color_sketch.convertTo(dst2,CV_8UC3,255);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::stylization(InputArray _src, OutputArray _dst, float sigma_s, float sigma_r)
|
|
|
|
{
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat I = _src.getMat();
|
|
|
|
_dst.create(I.size(), CV_8UC3);
|
|
|
|
Mat dst = _dst.getMat();
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat img = Mat(I.size(),CV_32FC3);
|
|
|
|
I.convertTo(img,CV_32FC3,1.0/255.0);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
int h = img.size().height;
|
|
|
|
int w = img.size().width;
|
|
|
|
int channel = img.channels();
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat res = Mat(h,w,CV_32FC3);
|
2013-10-17 23:44:31 +08:00
|
|
|
Mat magnitude = Mat(h,w,CV_32FC1);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Domain_Filter obj;
|
|
|
|
obj.filter(img, res, sigma_s, sigma_r, NORMCONV_FILTER);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-10-17 23:44:31 +08:00
|
|
|
obj.find_magnitude(res,magnitude,2);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat stylized = Mat(h,w,CV_32FC3);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-10-17 23:44:31 +08:00
|
|
|
vector <Mat> temp;
|
|
|
|
split(res,temp);
|
|
|
|
multiply(temp[0],magnitude,temp[0]);
|
|
|
|
multiply(temp[1],magnitude,temp[1]);
|
|
|
|
multiply(temp[2],magnitude,temp[2]);
|
|
|
|
merge(temp,stylized);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
stylized.convertTo(dst,CV_8UC3,255);
|
2013-09-03 23:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::edgeEnhance(InputArray _src, OutputArray _dst, float sigma_s, float sigma_r)
|
|
|
|
{
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat I = _src.getMat();
|
|
|
|
_dst.create(I.size(), CV_8UC1);
|
|
|
|
Mat dst = _dst.getMat();
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat img = Mat(I.size(),CV_32FC3);
|
|
|
|
I.convertTo(img,CV_32FC3,1.0/255.0);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat orig = img.clone();
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
int h = img.size().height;
|
|
|
|
int w = img.size().width;
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat res = Mat(h,w,CV_32FC3);
|
|
|
|
Mat magnitude = Mat(h,w,CV_32FC1);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Mat mag8 = Mat(h,w,CV_32FC1);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
Domain_Filter obj;
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
obj.filter(img, res, sigma_s, sigma_r, NORMCONV_FILTER);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-10-17 23:44:31 +08:00
|
|
|
obj.find_magnitude(res,magnitude,1);
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-09-14 21:42:25 +08:00
|
|
|
magnitude.convertTo(dst,CV_8UC1,255);
|
2013-09-03 23:42:05 +08:00
|
|
|
}
|