Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/photo/src/npr.cpp
16354 views
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 <iostream>
45
#include <stdlib.h>
46
47
#include "npr.hpp"
48
49
using namespace std;
50
using namespace cv;
51
52
void cv::edgePreservingFilter(InputArray _src, OutputArray dst, int flags, float sigma_s, float sigma_r)
53
{
54
CV_INSTRUMENT_REGION();
55
56
Mat I = _src.getMat();
57
58
Domain_Filter obj;
59
60
Mat img;
61
I.convertTo(img,CV_32FC3,1.0/255.0);
62
63
Mat res;
64
obj.filter(img, res, sigma_s, sigma_r, flags);
65
66
convertScaleAbs(res, dst, 255,0);
67
}
68
69
void cv::detailEnhance(InputArray _src, OutputArray dst, float sigma_s, float sigma_r)
70
{
71
CV_INSTRUMENT_REGION();
72
73
Mat I = _src.getMat();
74
75
float factor = 3.0f;
76
77
Mat img;
78
I.convertTo(img,CV_32FC3,1.0/255.0);
79
80
Mat lab;
81
vector <Mat> lab_channel;
82
cvtColor(img,lab,COLOR_BGR2Lab);
83
split(lab,lab_channel);
84
85
Mat L;
86
lab_channel[0].convertTo(L,CV_32FC1,1.0/255.0);
87
88
Domain_Filter obj;
89
90
Mat res;
91
obj.filter(L, res, sigma_s, sigma_r, 1);
92
93
Mat detail = L - res;
94
multiply(detail,factor,detail);
95
L = res + detail;
96
97
L.convertTo(lab_channel[0],CV_32FC1,255);
98
99
merge(lab_channel,lab);
100
101
cvtColor(lab,res,COLOR_Lab2BGR);
102
res.convertTo(dst,CV_8UC3,255);
103
}
104
105
void cv::pencilSketch(InputArray _src, OutputArray _dst1, OutputArray _dst2, float sigma_s, float sigma_r, float shade_factor)
106
{
107
CV_INSTRUMENT_REGION();
108
109
Mat I = _src.getMat();
110
_dst1.create(I.size(), CV_8UC1);
111
Mat dst1 = _dst1.getMat();
112
113
_dst2.create(I.size(), CV_8UC3);
114
Mat dst2 = _dst2.getMat();
115
116
Mat img = Mat(I.size(),CV_32FC3);
117
I.convertTo(img,CV_32FC3,1.0/255.0);
118
119
Domain_Filter obj;
120
121
Mat sketch = Mat(I.size(),CV_32FC1);
122
Mat color_sketch = Mat(I.size(),CV_32FC3);
123
124
obj.pencil_sketch(img, sketch, color_sketch, sigma_s, sigma_r, shade_factor);
125
126
sketch.convertTo(dst1,CV_8UC1,255);
127
color_sketch.convertTo(dst2,CV_8UC3,255);
128
129
}
130
131
void cv::stylization(InputArray _src, OutputArray _dst, float sigma_s, float sigma_r)
132
{
133
CV_INSTRUMENT_REGION();
134
135
Mat I = _src.getMat();
136
_dst.create(I.size(), CV_8UC3);
137
Mat dst = _dst.getMat();
138
139
Mat img;
140
I.convertTo(img,CV_32FC3,1.0/255.0);
141
142
int h = img.size().height;
143
int w = img.size().width;
144
145
Mat res;
146
Mat magnitude = Mat(h,w,CV_32FC1);
147
148
Domain_Filter obj;
149
obj.filter(img, res, sigma_s, sigma_r, NORMCONV_FILTER);
150
151
obj.find_magnitude(res,magnitude);
152
153
Mat stylized;
154
155
vector <Mat> temp;
156
split(res,temp);
157
multiply(temp[0],magnitude,temp[0]);
158
multiply(temp[1],magnitude,temp[1]);
159
multiply(temp[2],magnitude,temp[2]);
160
merge(temp,stylized);
161
162
stylized.convertTo(dst,CV_8UC3,255);
163
}
164
165