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
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 28/08/2021
 * Description : Image Quality Parser - Compression detection basic factor
 *
 * SPDX-FileCopyrightText: 2021-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2021-2022 by Phuoc Khanh Le <phuockhanhnk94 at gmail dot com>
 *
 * References  : http://www.arpnjournals.org/jeas/research_papers/rp_2016/jeas_1216_5505.pdf // krazy:exclude=insecurenet
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "compression_detector.h"

// Qt includes

#include <QtMath>

// Local includes

#include "digikam_debug.h"

namespace Digikam
{

class Q_DECL_HIDDEN CompressionDetector::Private
{
public:

    Private() = default;

public:

    int   threshold_edges_block = 2;
    float weight_edges_block    = 120.0F;

    float weight_mono_color     = 10.0F;
    float threshold_mono_color  = 0.1F;

    float alpha                 = 0.026F;
    float beta                  = -0.002F;
};

CompressionDetector::CompressionDetector()
    :  AbstractDetector(),
       d               (new Private)
{
}

CompressionDetector::~CompressionDetector()
{
    delete d;
}

auto accessRow = [](const cv::Mat& mat)
{
    return [mat](int index)
    {
        return mat.row(index);
    };
};

auto accessCol = [](const cv::Mat& mat)
{
    return [mat](int index)
    {
        return mat.col(index);
    };
};

float CompressionDetector::detect(const cv::Mat& image) const
{
    try
    {
        cv::Mat gray_image;

        cv::cvtColor(image, gray_image, cv::COLOR_BGR2GRAY);

        cv::Mat verticalBlock    = checkEdgesBlock(gray_image, gray_image.cols, accessCol);
        cv::Mat horizontalBlock  = checkEdgesBlock(gray_image, gray_image.rows, accessRow);
        cv::Mat mono_color_map   = detectMonoColorRegion(image);
        cv::Mat block_map        = mono_color_map.mul(verticalBlock + horizontalBlock);

        int nb_pixels_edge_block = cv::countNonZero(block_map);
        int nb_pixels_mono_color = cv::countNonZero(mono_color_map);
        int nb_pixels_normal     = image.total() - nb_pixels_edge_block - nb_pixels_edge_block;

        float res                = static_cast<float>(
                                                      (nb_pixels_mono_color * d->weight_mono_color +
                                                       nb_pixels_edge_block * d->threshold_edges_block) /
                                                      (nb_pixels_mono_color * d->weight_mono_color +
                                                       nb_pixels_edge_block * d->threshold_edges_block + nb_pixels_normal)
                                                     );

        return res;
    }
    catch (cv::Exception& e)
    {
        qCCritical(DIGIKAM_DETECTOR_LOG) << "CompressionDetector::detect: cv::Exception:" << e.what();
    }
    catch (...)
    {
        qCCritical(DIGIKAM_DETECTOR_LOG) << "CompressionDetector::detect: Default exception from OpenCV";
    }

    return 1.0F;
}

template <typename Function>
cv::Mat CompressionDetector::checkEdgesBlock(const cv::Mat& gray_image, int blockSize, Function accessEdges) const
{
    try
    {
        cv::Mat res            = cv::Mat::zeros(gray_image.size(), CV_8UC1);

        auto accessGrayImageAt = accessEdges(gray_image);
        auto accessResAt       = accessEdges(res);

        for (int i = 2 ; i < blockSize - 1 ; ++i)
        {
            cv::Mat a      = (accessGrayImageAt(i) - accessGrayImageAt(i + 1)) - (accessGrayImageAt(i - 1) - accessGrayImageAt(i));
            cv::Mat b      = (accessGrayImageAt(i) - accessGrayImageAt(i + 1)) - (accessGrayImageAt(i + 1) - accessGrayImageAt(i - 2));
            accessResAt(i) = (a >= d->threshold_edges_block) & (b >= d->threshold_edges_block);<--- Boolean expression 'a>=d->threshold_edges_block' is used in bitwise operation. Did you mean '&&'?
        }

        return res;
    }
    catch (cv::Exception& e)
    {
        qCCritical(DIGIKAM_DETECTOR_LOG) << "CompressionDetector::checkEdgesBlock: cv::Exception:" << e.what();
    }
    catch (...)
    {
        qCCritical(DIGIKAM_DETECTOR_LOG) << "CompressionDetector::checkEdgesBlock: Default exception from OpenCV";
    }

    return cv::Mat();
}

cv::Mat CompressionDetector::detectMonoColorRegion(const cv::Mat& image) const
{
    try
    {
        cv::Mat median_image    = cv::Mat();
        cv::medianBlur(image, median_image, 5);
        cv::Mat mat_subtraction = cv::abs(image - median_image);
        std::vector<cv::Mat> rgbChannels(3);

        cv::split(mat_subtraction, rgbChannels);

        cv::Mat res             = rgbChannels[0] + rgbChannels[1] + rgbChannels[2];

        cv::threshold(res, res, d->threshold_mono_color, 1, cv::THRESH_BINARY_INV);

        return res;
    }
    catch (cv::Exception& e)
    {
        qCCritical(DIGIKAM_DETECTOR_LOG) << "CompressionDetector::detectMonoColorRegion: cv::Exception:" << e.what();
    }
    catch (...)
    {
        qCCritical(DIGIKAM_DETECTOR_LOG) << "CompressionDetector::detectMonoColorRegion: Default exception from OpenCV";
    }

    return cv::Mat();
}

float CompressionDetector::normalize(const float number)
{
    return (1.0F / (1.0F + qExp(-(number - d->alpha) / d->beta)));
}

} // namspace Digikam

#include "moc_compression_detector.cpp"