Skip to content

Commit

Permalink
Apply normalisation before flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed May 21, 2022
1 parent b4c7a1d commit cb1df11
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions test/api/similar_image.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include "similar_image.h"

#include <array>
#include <iomanip>

VImage SimilarImage::normalize(const VImage &image) const {
// Get original colorspace
VipsInterpretation type_before_normalize = image.interpretation();
if (type_before_normalize == VIPS_INTERPRETATION_RGB) {
type_before_normalize = VIPS_INTERPRETATION_sRGB;
}

// Convert to LAB colorspace
VImage lab = image.colourspace(VIPS_INTERPRETATION_LAB);
Expand Down Expand Up @@ -57,6 +55,9 @@ std::string SimilarImage::dhash(const VImage &image) const {
VImage thumbnail =
image.thumbnail_image(9, thumbnail_options).copy_memory();

// Apply normalisation - stretch luminance to cover full dynamic range.
thumbnail = normalize(thumbnail);

// Flatten it to mid-gray before generating a fingerprint.
if (image.has_alpha()) {
std::vector<double> background{128, 128, 128}; // #808080
Expand All @@ -65,9 +66,9 @@ std::string SimilarImage::dhash(const VImage &image) const {
thumbnail.flatten(VImage::option()->set("background", background));
}

thumbnail = normalize(thumbnail).colourspace(VIPS_INTERPRETATION_B_W)[0];
thumbnail = thumbnail.colourspace(VIPS_INTERPRETATION_B_W)[0];

auto dhash_image =
auto *dhash_image =
static_cast<uint8_t *>(thumbnail.write_to_memory(nullptr));

// Calculate dHash
Expand Down

0 comments on commit cb1df11

Please sign in to comment.