This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathSmartCrop.cpp
327 lines (287 loc) · 10.8 KB
/
SmartCrop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "SmartCrop.h"
#include <QDebug>
#include <QtMath>
#include <QElapsedTimer>
namespace SmartCrop {
static inline qreal cie(qreal r, qreal g, qreal b) {
return 0.5126 * b + 0.7152 * g + 0.0722 * r;
}
static inline qreal sample(const uchar *id, int p) {
return cie(id[p], id[p + 1], id[p + 2]);
}
static void edgeDetect(const QImage &i, QImage &o) {
const int bpl = i.bytesPerLine();//: width();
const uchar *id = i.scanLine(0);
const int h = i.height();
for (int y = 0; y < h; y++) {
uchar *od = o.scanLine(y);
for (int x = 0; x < i.width(); x++) {
int p = y * bpl + x * 4;
qreal lightness;
if (x == 0 || x >= i.width() - 1 || y == 0 || y >= h - 1) {
lightness = sample(id, p);
} else {
lightness = sample(id, p) * 4. -
sample(id, p - bpl) -
sample(id, p - 4) -
sample(id, p + 4) -
sample(id, p + bpl);
}
od[x * 4 + 1] = qMax<int>(lightness, 0);
}
}
}
static qreal skinColor(const CropOptions &options, qreal r,qreal g, qreal b) {
qreal mag = sqrt(r * r + g * g + b * b);
if (Q_UNLIKELY(mag == 0.)) {
return 0.;
}
qreal rd = r / mag - options.skinColor[0];
qreal gd = g / mag - options.skinColor[1];
qreal bd = b / mag - options.skinColor[2];
qreal d = sqrt(rd * rd + gd * gd + bd * bd);
return 1 - d;
}
static void skinDetect(const CropOptions &options, const QImage &i, QImage &o) {
int w = i.width();
int h = i.height();
for (int y = 0; y < h; y++) {
uchar *od = o.scanLine(y);
const uchar *id = i.scanLine(y) ;
for (int x = 0; x < w; x++) {
int p = x * 4;
qreal lightness = cie(id[p], id[p + 1], id[p + 2]) / 255.;
qreal skin = skinColor(options, id[p], id[p + 1], id[p + 2]);
bool isSkinColor = skin > options.skinThreshold;
bool isSkinBrightness =
lightness >= options.skinBrightnessMin &&
lightness <= options.skinBrightnessMax;
if (isSkinColor && isSkinBrightness) {
od[p] =
(skin - options.skinThreshold) *
(255. / (1. - options.skinThreshold));
} else {
od[p] = 0;
}
}
}
}
static qreal saturation(qreal r, qreal g, qreal b) {
qreal maximum = qMax(r / 255., qMax(g / 255., b / 255.));
qreal minimum = qMin(r / 255., qMin(g / 255., b / 255.));
if (qFuzzyCompare(maximum, minimum)) {
return 0;
}
qreal l = (maximum + minimum) / 2.;
qreal d = maximum - minimum;
return l > 0.5 ? d / (2. - maximum - minimum) : d / (maximum + minimum);
}
static void saturationDetect(const CropOptions &options, const QImage &i, QImage &o) {
int w = i.width();
int h = i.height();
for (int y = 0; y < h; y++) {
uchar *od = o.scanLine(y);
const uchar *id = i.scanLine(y);
for (int x = 0; x < w; x++) {
int p = x * 4;
qreal lightness = cie(id[p], id[p + 1], id[p + 2]) / 255.;
qreal sat = saturation(id[p], id[p + 1], id[p + 2]);
bool acceptableSaturation = sat > options.saturationThreshold;
bool acceptableLightness =
lightness >= options.saturationBrightnessMin &&
lightness <= options.saturationBrightnessMax;
if (acceptableLightness && acceptableSaturation) {
od[p + 2] =
(sat - options.saturationThreshold) *
(255. / (1 - options.saturationThreshold));
} else {
od[p + 2] = 0;
}
}
}
}
static QImage downSample(QImage &input, qreal factor) {
int width = qFloor(input.width() / factor);
int height = qFloor(input.height() / factor);
QImage output(width, height, QImage::Format_RGBA8888);
qreal ifactor2 = 1. / (factor * factor);
for (int y = 0; y < height; y++) {
uchar *data = output.scanLine(y);
for (int x = 0; x < width; x++) {
int i = x * 4;
qreal r = 0;
qreal g = 0;
qreal b = 0;
qreal a = 0;
qreal mr = 0;
qreal mg = 0;
for (int v = 0; v < factor; v++) {
uchar *idata = input.scanLine(y * factor + v);
for (int u = 0; u < factor; u++) {
int j = (x * factor + u) * 4;
r += idata[j];
g += idata[j + 1];
b += idata[j + 2];
a += idata[j + 3];
mr = qMax(mr, qreal(idata[j]));
mg = qMax(mg, qreal(idata[j + 1]));
}
}
// this is some funky magic to preserve detail a bit more for
// skin (r) and detail (g). Saturation (b) does not get this boost.
data[i] = r * ifactor2 * 0.5 + mr * 0.5;
data[i + 1] = g * ifactor2 * 0.7 + mg * 0.3;
data[i + 2] = b * ifactor2;
data[i + 3] = a * ifactor2;
}
}
return output;
}
struct ScoreResult {
};
// Gets value in the range of [0, 1] where 0 is the center of the pictures
// returns weight of rule of thirds [0, 1]
static qreal thirds(qreal x) {
x = ((int(x - 1 / 3 + 1.0) % 2) * 0.5 - 0.5) * 16;
return qMax(1.0 - x * x, 0.0);
}
static inline qreal importance(const CropOptions &options, const QRectF &crop, qreal x, qreal y) {
if (
crop.x() > x ||
x >= crop.x() + crop.width() ||
crop.y() > y ||
y >= crop.y() + crop.height()
) {
return options.outsideImportance;
}
x = (x - crop.x()) / qreal(crop.width());
y = (y - crop.y()) / qreal(crop.height());
qreal px = abs(0.5 - x) * 2.;
qreal py = abs(0.5 - y) * 2.;
// Distance from edge
qreal dx = qMax<qreal>(px - 1.0 + options.edgeRadius, 0);
qreal dy = qMax<qreal>(py - 1.0 + options.edgeRadius, 0);
qreal d = (dx * dx + dy * dy) * options.edgeWeight;
qreal s = 1.41 - sqrt(px * px + py * py);
if (options.ruleOfThirds) {
s += qMax(0., s + d + 0.5) * 1.2 * (thirds(px) + thirds(py));
}
return s + d;
}
static QList<QRectF> generateCrops(const CropOptions &options, qreal width, qreal height) {
QList<QRectF> results;
int minDimension = qMin(width, height);
qreal cropWidth = options.cropWidth > 0 ? options.cropWidth : minDimension;
qreal cropHeight = options.cropHeight > 0 ? options.cropHeight : minDimension;
for (
qreal scale = options.maxScale;
scale >= options.minScale;
scale -= options.scaleStep
) {
for (qreal y = 0; y + cropHeight * scale <= height; y += options.step) {
for (qreal x = 0; x + cropWidth * scale <= width; x += options.step) {
results.append({x, y, cropWidth * scale, cropHeight * scale});
}
}
}
return results;
}
static float score(const CropOptions &options, const QImage &output, const QRectF &crop) {
qreal detail = 0;
qreal saturation = 0;
qreal skin = 0;
qreal boost = 0;
qreal downSample = options.scoreDownSample;
qreal invDownSample = 1. / downSample;
qreal outputHeightDownSample = output.height() * downSample;
qreal outputWidthDownSample = output.width() * downSample;
for (int y = 0; y < outputHeightDownSample; y += downSample) {
const uchar *od = output.scanLine(qFloor(y * invDownSample));
for (int x = 0; x < outputWidthDownSample; x += downSample) {
int p = (qFloor(x * invDownSample)) * 4;
qreal i = importance(options, crop, x, y);
qreal dtl = od[p + 1] / 255.;
skin += (od[p] / 255.) * (dtl + options.skinBias) * i;
detail += dtl * i;
saturation +=
(od[p + 2] / 255) * (dtl + options.saturationBias) * i;
boost += (od[p + 3] / 255.) * i;
}
}
return
(detail * options.detailWeight +
skin * options.skinWeight +
saturation * options.saturationWeight +
boost * options.boostWeight) /
(crop.width() * crop.height());
}
QRect smartCropRect(const QImage &input, CropOptions options)
{
if (input.isNull()) {
qWarning() << "Invalid image";
return input.rect();
}
if (options.aspect) {
options.width = options.aspect;
options.height = 1;
}
QImage image = input;
qreal scale = 1;
qreal prescale = 1;
if (options.width && options.height) {
scale = qMin(
image.width() / options.width,
image.height() / options.height
);
options.cropWidth = qFloor(options.width * scale);
options.cropHeight = qFloor(options.height * scale);
// Img = 100x100, width = 95x95, scale = 100/95, 1/scale > min
// don't set minscale smaller than 1/scale
// -> don't pick crops that need upscaling
options.minScale = qMin(
options.maxScale,
qMax(1 / scale, options.minScale)
);
// prescale if possible
if (options.prescale != false) {
prescale = qMin(qMax(256. / image.width(), 256. / image.height()), 1.);
if (prescale < 1) {
image = image.scaled(image.width() * prescale, image.height () * prescale);
options.cropWidth = qFloor(options.cropWidth * prescale);
options.cropHeight = qFloor(options.cropHeight * prescale);
} else {
prescale = 1;
}
}
}
image = image.convertToFormat(QImage::Format_RGBA8888);
QImage filtered = image.copy();
if (filtered.isNull()) {
qWarning() << "Failed to copy image!";
return input.rect();
}
edgeDetect(image, filtered);
skinDetect(options, image, filtered);
saturationDetect(options, image, filtered);
QImage toScore = downSample(filtered, options.scoreDownSample);
QList<QRectF> crops = generateCrops(options, image.width(), image.height());
QRectF topCrop;
qreal topScore = -1;
for (const QRectF &crop : crops) {
float scr = score(options, toScore, crop);
if (scr > topScore || topCrop.isEmpty()) {
topCrop = crop;
topScore = scr;
}
}
return QRectF(topCrop.x() / prescale, topCrop.y() / prescale, topCrop.width() / prescale, topCrop.height() / prescale).toAlignedRect();
}
QImage crop(const QImage &input, CropOptions options)
{
if (input.isNull()) {
qWarning() << "Invalid image";
return input;
}
return input.copy(smartCropRect(input, options));
}
} // namespace SmartCrop