Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/dnn/colorization.cpp
16337 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html
4
5
#include <opencv2/dnn.hpp>
6
#include <opencv2/imgproc.hpp>
7
#include <opencv2/highgui.hpp>
8
#include <iostream>
9
10
using namespace cv;
11
using namespace cv::dnn;
12
using namespace std;
13
14
// the 313 ab cluster centers from pts_in_hull.npy (already transposed)
15
static float hull_pts[] = {
16
-90., -90., -90., -90., -90., -80., -80., -80., -80., -80., -80., -80., -80., -70., -70., -70., -70., -70., -70., -70., -70.,
17
-70., -70., -60., -60., -60., -60., -60., -60., -60., -60., -60., -60., -60., -60., -50., -50., -50., -50., -50., -50., -50., -50.,
18
-50., -50., -50., -50., -50., -50., -40., -40., -40., -40., -40., -40., -40., -40., -40., -40., -40., -40., -40., -40., -40., -30.,
19
-30., -30., -30., -30., -30., -30., -30., -30., -30., -30., -30., -30., -30., -30., -30., -20., -20., -20., -20., -20., -20., -20.,
20
-20., -20., -20., -20., -20., -20., -20., -20., -20., -10., -10., -10., -10., -10., -10., -10., -10., -10., -10., -10., -10., -10.,
21
-10., -10., -10., -10., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 10., 10., 10., 10., 10., 10., 10.,
22
10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20.,
23
20., 20., 20., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 30., 40., 40., 40., 40.,
24
40., 40., 40., 40., 40., 40., 40., 40., 40., 40., 40., 40., 40., 40., 40., 40., 50., 50., 50., 50., 50., 50., 50., 50., 50., 50.,
25
50., 50., 50., 50., 50., 50., 50., 50., 50., 60., 60., 60., 60., 60., 60., 60., 60., 60., 60., 60., 60., 60., 60., 60., 60., 60.,
26
60., 60., 60., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 70., 80., 80., 80.,
27
80., 80., 80., 80., 80., 80., 80., 80., 80., 80., 80., 80., 80., 80., 80., 80., 90., 90., 90., 90., 90., 90., 90., 90., 90., 90.,
28
90., 90., 90., 90., 90., 90., 90., 90., 90., 100., 100., 100., 100., 100., 100., 100., 100., 100., 100., 50., 60., 70., 80., 90.,
29
20., 30., 40., 50., 60., 70., 80., 90., 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., -20., -10., 0., 10., 20., 30., 40., 50.,
30
60., 70., 80., 90., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100., -40., -30., -20., -10., 0., 10., 20.,
31
30., 40., 50., 60., 70., 80., 90., 100., -50., -40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100., -50.,
32
-40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100., -60., -50., -40., -30., -20., -10., 0., 10., 20.,
33
30., 40., 50., 60., 70., 80., 90., 100., -70., -60., -50., -40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., 80., 90.,
34
100., -80., -70., -60., -50., -40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., -80., -70., -60., -50.,
35
-40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., -90., -80., -70., -60., -50., -40., -30., -20., -10.,
36
0., 10., 20., 30., 40., 50., 60., 70., 80., 90., -100., -90., -80., -70., -60., -50., -40., -30., -20., -10., 0., 10., 20., 30.,
37
40., 50., 60., 70., 80., 90., -100., -90., -80., -70., -60., -50., -40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70.,
38
80., -110., -100., -90., -80., -70., -60., -50., -40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., 80., -110., -100.,
39
-90., -80., -70., -60., -50., -40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., 80., -110., -100., -90., -80., -70.,
40
-60., -50., -40., -30., -20., -10., 0., 10., 20., 30., 40., 50., 60., 70., -110., -100., -90., -80., -70., -60., -50., -40., -30.,
41
-20., -10., 0., 10., 20., 30., 40., 50., 60., 70., -90., -80., -70., -60., -50., -40., -30., -20., -10., 0.
42
};
43
44
int main(int argc, char **argv)
45
{
46
const string about =
47
"This sample demonstrates recoloring grayscale images with dnn.\n"
48
"This program is based on:\n"
49
" http://richzhang.github.io/colorization\n"
50
" https://github.com/richzhang/colorization\n"
51
"Download caffemodel and prototxt files:\n"
52
" http://eecs.berkeley.edu/~rich.zhang/projects/2016_colorization/files/demo_v2/colorization_release_v2.caffemodel\n"
53
" https://raw.githubusercontent.com/richzhang/colorization/master/colorization/models/colorization_deploy_v2.prototxt\n";
54
const string keys =
55
"{ h help | | print this help message }"
56
"{ proto | colorization_deploy_v2.prototxt | model configuration }"
57
"{ model | colorization_release_v2.caffemodel | model weights }"
58
"{ image | space_shuttle.jpg | path to image file }"
59
"{ opencl | | enable OpenCL }";
60
CommandLineParser parser(argc, argv, keys);
61
parser.about(about);
62
if (parser.has("help"))
63
{
64
parser.printMessage();
65
return 0;
66
}
67
string modelTxt = parser.get<string>("proto");
68
string modelBin = parser.get<string>("model");
69
string imageFile = parser.get<string>("image");
70
bool useOpenCL = parser.has("opencl");
71
if (!parser.check())
72
{
73
parser.printErrors();
74
return 1;
75
}
76
77
Mat img = imread(imageFile);
78
if (img.empty())
79
{
80
cout << "Can't read image from file: " << imageFile << endl;
81
return 2;
82
}
83
84
// fixed input size for the pretrained network
85
const int W_in = 224;
86
const int H_in = 224;
87
Net net = dnn::readNetFromCaffe(modelTxt, modelBin);
88
if (useOpenCL)
89
net.setPreferableTarget(DNN_TARGET_OPENCL);
90
91
// setup additional layers:
92
int sz[] = {2, 313, 1, 1};
93
const Mat pts_in_hull(4, sz, CV_32F, hull_pts);
94
Ptr<dnn::Layer> class8_ab = net.getLayer("class8_ab");
95
class8_ab->blobs.push_back(pts_in_hull);
96
Ptr<dnn::Layer> conv8_313_rh = net.getLayer("conv8_313_rh");
97
conv8_313_rh->blobs.push_back(Mat(1, 313, CV_32F, Scalar(2.606)));
98
99
// extract L channel and subtract mean
100
Mat lab, L, input;
101
img.convertTo(img, CV_32F, 1.0/255);
102
cvtColor(img, lab, COLOR_BGR2Lab);
103
extractChannel(lab, L, 0);
104
resize(L, input, Size(W_in, H_in));
105
input -= 50;
106
107
// run the L channel through the network
108
Mat inputBlob = blobFromImage(input);
109
net.setInput(inputBlob);
110
Mat result = net.forward();
111
112
// retrieve the calculated a,b channels from the network output
113
Size siz(result.size[2], result.size[3]);
114
Mat a = Mat(siz, CV_32F, result.ptr(0,0));
115
Mat b = Mat(siz, CV_32F, result.ptr(0,1));
116
resize(a, a, img.size());
117
resize(b, b, img.size());
118
119
// merge, and convert back to BGR
120
Mat color, chn[] = {L, a, b};
121
merge(chn, 3, lab);
122
cvtColor(lab, color, COLOR_Lab2BGR);
123
124
imshow("color", color);
125
imshow("original", img);
126
waitKey();
127
return 0;
128
}
129
130