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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2025-08-24
 * Description : image editor plugin to blur the background of an image
 *
 * SPDX-FileCopyrightText: 2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "backgroundblurfilter.h"

// Qt includes

#include <QtMath>

// Local includes

#include "digikam_debug.h"
#include "digikam_globals_p.h"      // For KF6::Ki18n deprecated
#include "digikam_opencv.h"
#include "qtopencvimg.h"

namespace DigikamEditorBackgroundBlurToolPlugin
{

class Q_DECL_HIDDEN BackgroundBlurFilter::Private
{
public:

    Private() = default;

public:

    int    radius     = 3;   ///< Blur effect radius.
    int    transition = 0;   ///< Number of blur transitions.
    int    iterations = 10;  ///< GrabCut iterations to isolate more and less the subject.
    QRectF selection;        ///< suject area previously selected by the user.
};

BackgroundBlurFilter::BackgroundBlurFilter(QObject* const parent)
    : DImgThreadedFilter(parent),
      d                 (new Private)
{
    initFilter();
}

BackgroundBlurFilter::BackgroundBlurFilter(DImg* const orgImage,
                                           const QRectF& selection,
                                           int radius,
                                           int transition,
                                           int iterations,
                                           QObject* const parent)
    : DImgThreadedFilter(orgImage, parent, QLatin1String("BackgroundBlur")),
      d                 (new Private)
{
    d->selection  = selection;
    d->radius     = radius;
    d->transition = transition;
    d->iterations = iterations;

    initFilter();
}

BackgroundBlurFilter::BackgroundBlurFilter(DImgThreadedFilter* const parentFilter,
                                           const DImg& orgImage,
                                           const DImg& destImage,
                                           const QRectF& selection,
                                           int radius,
                                           int transition,
                                           int iterations,
                                           int progressBegin,
                                           int progressEnd)
    : DImgThreadedFilter(parentFilter,
                         orgImage,
                         destImage,
                         progressBegin,
                         progressEnd,
                         parentFilter->filterName() + QLatin1String(": BackgroundBlur")),
      d(new Private)
{
    d->selection  = selection;
    d->radius     = radius;
    d->transition = transition;
    d->iterations = iterations;

    this->filterImage();
}

BackgroundBlurFilter::~BackgroundBlurFilter()
{
    cancelFilter();

    delete d;
}

QString BackgroundBlurFilter::DisplayableName()
{
    return QString::fromUtf8(I18N_NOOP("Background Blur Filter"));
}

void BackgroundBlurFilter::filterImage()
{
    if (d->radius < 1)
    {
        qCDebug(DIGIKAM_DIMG_LOG) << "Radius out of range...";
        m_destImage = m_orgImage;

        return;
    }

    if (d->selection.isEmpty())
    {
        qCDebug(DIGIKAM_DIMG_LOG) << "Selection is empty...";
        m_destImage = m_orgImage;

        return;
    }

    bool is16Bit = m_orgImage.sixteenBit();
    cv::Mat input;

    try
    {
        if (is16Bit)
        {
            // Convert image from 16 bits to 8 bits for grabCut as this one do not support 16 bits.

            DImg img8Bit = m_orgImage;
            img8Bit.convertToEightBit();
            input        = QtOpenCVImg::image2Mat(img8Bit);
        }
        else
        {
            input        = QtOpenCVImg::image2Mat(m_orgImage);
        }

        if (input.empty())
        {
            qCWarning(DIGIKAM_DIMG_LOG) << "Failed to convert image to cv::Mat";
            m_destImage = m_orgImage;

            return;
        }

        cv::Mat output;
        postProgress(10);

        if (!runningFlag())
        {
            return;
        }

        // Convert image to CV_8UC3 (BGR) if necessary.

        cv::Mat inputBGR;

        if (input.type() != CV_8UC3)
        {
            if      (input.channels() == 1)
            {
                cv::cvtColor(input, inputBGR, cv::COLOR_GRAY2BGR);
            }
            else if (input.channels() == 4)
            {
                cv::cvtColor(input, inputBGR, cv::COLOR_BGRA2BGR);
            }
            else
            {
                qCWarning(DIGIKAM_DIMG_LOG) << "Unsupported image format";
                m_destImage = m_orgImage;

                return;
            }
        }
        else
        {
            inputBGR = input;
        }

        postProgress(20);

        if (!runningFlag())
        {
            return;
        }

        // Init the mask for GrabCut.

        cv::Rect roi(d->selection.x(), d->selection.y(), d->selection.width(), d->selection.height());
        cv::Mat mask(input.rows, input.cols, CV_8UC1, cv::GC_PR_BGD);
        mask(roi) = cv::GC_PR_FGD;
        postProgress(30);

        if (!runningFlag())
        {
            return;
        }

        // Apply GrabCut with more iterations for better accuracy.

        cv::Mat bgModel, fgModel;
        cv::grabCut(inputBGR, mask, roi, bgModel, fgModel, d->iterations, cv::GC_INIT_WITH_RECT);

        if (!runningFlag())
        {
            return;
        }

        // Refine the mask: GC_PR_FGD and GC_FGD are considered foreground.

        cv::compare(mask, cv::GC_PR_FGD, mask, cv::CMP_GE);
        postProgress(40);

        if (!runningFlag())<--- Assuming that condition '!runningFlag()' is not redundant<--- Assuming that condition '!runningFlag()' is not redundant<--- Assuming that condition '!runningFlag()' is not redundant<--- Assuming that condition '!runningFlag()' is not redundant<--- Assuming that condition '!runningFlag()' is not redundant<--- Assuming that condition '!runningFlag()' is not redundant<--- Assuming that condition '!runningFlag()' is not redundant<--- Assuming that condition '!runningFlag()' is not redundant
        {
            return;
        }

        // Smooth the mask edges.

        cv::Mat kernelClose = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3, 3));
        cv::morphologyEx(mask, mask, cv::MORPH_CLOSE, kernelClose);

        if (!runningFlag())<--- Condition '!runningFlag()' is always false
        {
            return;
        }

        // Dilate the mask slightly to include more pixels near the subject.

        cv::Mat kernelDilate = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2, 2));
        cv::dilate(mask, mask, kernelDilate);

        if (!runningFlag())<--- Condition '!runningFlag()' is always false
        {
            return;
        }

        // Convert the mask to BGR to be suitable for the preview.

        cv::Mat maskDisplay;
        mask.convertTo(maskDisplay, CV_8U, 255);

        if (!runningFlag())<--- Condition '!runningFlag()' is always false
        {
            return;
        }

        // Create a copy of original for preview.

        cv::Mat overlay = inputBGR.clone();

        if (!runningFlag())<--- Condition '!runningFlag()' is always false
        {
            return;
        }

        // Create an image from the mask using semi-transparent green.

        cv::Mat coloredMask;
        cv::cvtColor(maskDisplay, coloredMask, cv::COLOR_GRAY2BGR);
        coloredMask.setTo(cv::Scalar(0, 255, 0), maskDisplay);

        if (!runningFlag())<--- Condition '!runningFlag()' is always false
        {
            return;
        }

        // Apply a transparency to the mask.

        cv::Mat alphaMask;
        maskDisplay.convertTo(alphaMask, CV_32F, 1.0/255.0);

        if (!runningFlag())<--- Condition '!runningFlag()' is always false
        {
            return;
        }

        // Merge the semi-transparent mask over original image.

        for (int y = 0 ; runningFlag() && (y < inputBGR.rows) ; y++)<--- Condition 'runningFlag()' is always true
        {
            for (int x = 0 ; runningFlag() && (x < inputBGR.cols) ; x++)<--- Condition 'runningFlag()' is always true
            {
                float alpha                = alphaMask.at<float>(y, x);
                cv::Vec3b& pixel           = overlay.at<cv::Vec3b>(y, x);
                const cv::Vec3b& maskPixel = coloredMask.at<cv::Vec3b>(y, x);

                pixel[0] = static_cast<uchar>(alpha * maskPixel[0] + (1.0f - alpha) * pixel[0]);
                pixel[1] = static_cast<uchar>(alpha * maskPixel[1] + (1.0f - alpha) * pixel[1]);
                pixel[2] = static_cast<uchar>(alpha * maskPixel[2] + (1.0f - alpha) * pixel[2]);
            }
        }

        // Send the signal to render the mask preview in the GUI.

        QImage maskQImage = QtOpenCVImg::mat2Image(overlay);

        Q_EMIT signalSegmentedMask(maskQImage.convertToFormat(QImage::Format_ARGB32));

        postProgress(50);

        if (!runningFlag())
        {
            return;
        }

        // Blur the background.

        cv::Mat blurred;
        cv::GaussianBlur(inputBGR, blurred, cv::Size(0, 0), d->radius);
        postProgress(60);

        if (!runningFlag())<--- Assuming that condition '!runningFlag()' is not redundant<--- Assuming that condition '!runningFlag()' is not redundant
        {
            return;
        }

        if (d->transition == 0)
        {
            // Uniform blur.

            inputBGR.copyTo(output, mask);
            blurred.copyTo(output, ~mask);
        }
        else
        {
            // Normalize transition parameter between 0.1 and 2.0 for better control.

            float transition = 0.1F + (d->transition / 100.0F) * 1.9F;

            // Progressive blur.

            cv::Mat distanceMap;
            cv::distanceTransform(~mask, distanceMap, cv::DIST_L2, cv::DIST_MASK_5);
            cv::normalize(distanceMap, distanceMap, 0, 1, cv::NORM_MINMAX);

            // Apply a non-linear transformation to the distance map based on transition parameter.

            for (int y = 0 ; runningFlag() && (y < distanceMap.rows) ; y++)<--- Condition 'runningFlag()' is always true
            {
                for (int x = 0 ; runningFlag() && (x < distanceMap.cols) ; x++)<--- Condition 'runningFlag()' is always true
                {
                    float dist = distanceMap.at<float>(y, x);
                    distanceMap.at<float>(y, x) = std::pow(dist, 1.0F / transition);
                }
            }

            // Create the result with the progressive blur.

            output = inputBGR.clone();
            postProgress(70);

            for (int y = 0 ; runningFlag() && (y < inputBGR.rows) ; y++)
            {
                for (int x = 0 ; runningFlag() && (x < inputBGR.cols) ; x++)
                {
                    float alpha                = distanceMap.at<float>(y, x);
                    output.at<cv::Vec3b>(y, x) = alpha * blurred.at<cv::Vec3b>(y, x) +
                                                 (1.0F - alpha) * inputBGR.at<cv::Vec3b>(y, x);
                }
            }
        }

        postProgress(80);

        if (!runningFlag())
        {
            return;
        }

        // Convert back to the original format if necessary.

        if (is16Bit)
        {
            DImg output8Bit(QtOpenCVImg::mat2Image(output));
            m_destImage = output8Bit;
            m_destImage.convertToSixteenBit();
        }
        else
        {
            m_destImage = DImg(QtOpenCVImg::mat2Image(output));
        }

        if (!m_orgImage.hasAlpha())
        {
            m_destImage.removeAlphaChannel();
        }

        postProgress(90);

        if (!runningFlag())
        {
            return;
        }
    }
    catch (cv::Exception& e)
    {
        qCWarning(DIGIKAM_DIMG_LOG) << "BackgroundBlurFilter::filterImage: cv::Exception:" << e.what();

        Q_EMIT signalSegmentedMask(QImage());

        m_destImage = m_orgImage;
    }
    catch (...)
    {
        qCWarning(DIGIKAM_DIMG_LOG) << "BackgroundBlurFilter::filterImage: Default exception from OpenCV";

        Q_EMIT signalSegmentedMask(QImage());

        m_destImage = m_orgImage;
    }
}

FilterAction BackgroundBlurFilter::filterAction()
{
    FilterAction action(FilterIdentifier(), CurrentVersion());
    action.setDisplayableName(DisplayableName());

    action.addParameter(QLatin1String("radius"),     d->radius);
    action.addParameter(QLatin1String("transition"), d->transition);

    return action;
}

void BackgroundBlurFilter::readParameters(const FilterAction& action)
{
    d->radius     = action.parameter(QLatin1String("radius")).toInt();
    d->transition = action.parameter(QLatin1String("transition")).toInt();
}

} // namespace DigikamEditorBackgroundBlurToolPlugin

#include "moc_backgroundblurfilter.cpp"