Skip to content

Commit

Permalink
Searching of similar maps was improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
dudaerich committed Aug 28, 2015
1 parent 7a0e8a3 commit f497e61
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 117 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ COPY readers_count /shared/readers_count
COPY can_read /shared/can_read
RUN touch /shared/blockhash
RUN touch /shared/dhash
RUN touch /shared/cannydhash
RUN touch /shared/gaussdhash
RUN touch /shared/gauss2dhash
RUN touch /shared/gaussblockhash
RUN chmod -R a+w /shared

COPY lighttpd-imagesearch.conf /etc/lighttpd/conf-available/30-imagesearch.conf
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: run

run: build
docker rm imagesearch.mzk.cz
-docker rm imagesearch.mzk.cz
docker run -i -t -p 8080:80 -v /var/imagesearch:/data --name imagesearch.mzk.cz imagesearch.mzk.cz

build:
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ Uploading is done via POST request, which has following structure:
"image_base64": "...",
"blockHash": "...",
"dHash": "...",
"cannyDHash": "..."
"gaussDHash": "...",
"gauss2DHash": "...",
"gaussBlockHash": "..."
},
{
"id": "example2",
Expand All @@ -216,7 +218,9 @@ Uploading is done via POST request, which has following structure:
"image_base64": "...",
"blockHash": "...",
"dHash": "...",
"cannyDHash": "..."
"gaussDHash": "...",
"gauss2DHash": "...",
"gaussBlockHash": "..."
}
]
```
Expand All @@ -226,7 +230,7 @@ Uploading is done via POST request, which has following structure:
* Image data can be send by several ways:
- image_url: You can specify url, where the image can be downloaded.
- image_base64: You can send image data in base64 format.
- blockHash, dHash, cannyDHash: You can compute hashes by yourselves and send their hexadecimal representations.
- blockHash, dHash, gaussDHash, gauss2DHash, gaussBlockHash: You can compute hashes by yourselves and send their hexadecimal representations.
* From listed methods how to upload image data you should choose only one. If you use several methods at once
the behaviour is not defined.

Expand Down Expand Up @@ -329,7 +333,7 @@ You can seach similar images using both GET or POST requests.
##### GET

```
/v1/searchSimilar?cannyDHash=0f13332927217ada&count=10
/v1/searchSimilar?gaussDHash=0f13332927217ada&count=10
```

* By parameter count you can specify how many the most similar images will be returned.
Expand Down
27 changes: 0 additions & 27 deletions fcgi/CannyDHashAlgorithm.h

This file was deleted.

30 changes: 24 additions & 6 deletions fcgi/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,14 @@ void Controller::ingestRequest(const Poco::Dynamic::Var& request) {
if (json->has("dHash")) {
record.getHashes()[DHash] = str2bin(json->getValue<string>("dHash"));
}
if (json->has("cannyDHash")) {
record.getHashes()[CannyDHash] = str2bin(json->getValue<string>("cannyDHash"));
if (json->has("gaussDHash")) {
record.getHashes()[GaussDHash] = str2bin(json->getValue<string>("gaussDHash"));
}
if (json->has("gauss2DHash")) {
record.getHashes()[Gauss2DHash] = str2bin(json->getValue<string>("gauss2DHash"));
}
if (json->has("gaussBlockHash")) {
record.getHashes()[GaussBlockHash] = str2bin(json->getValue<string>("gaussBlockHash"));
}
if (json->has("image_base64")) {
image_data = Image::fromBase64(json->getValue<string>("image_base64"));
Expand Down Expand Up @@ -106,8 +112,14 @@ void Controller::searchIdenticalRequest(std::map<std::string, std::string>& para
if (params.count("dHash")) {
record.getHashes()[DHash] = str2bin(params["dHash"]);
}
if (params.count("cannyDHash")) {
record.getHashes()[CannyDHash] = str2bin(params["cannyDHash"]);
if (params.count("gaussDHash")) {
record.getHashes()[GaussDHash] = str2bin(params["gaussDHash"]);
}
if (params.count("gauss2DHash")) {
record.getHashes()[Gauss2DHash] = str2bin(params["gauss2DHash"]);
}
if (params.count("gaussBlockHash")) {
record.getHashes()[GaussBlockHash] = str2bin(params["gaussBlockHash"]);
}
if (params.count("url")) {
image_data = Image::fromUrl(params["url"]);
Expand Down Expand Up @@ -142,8 +154,14 @@ void Controller::searchSimilarRequest(std::map<std::string, std::string>& params
if (params.count("dHash")) {
record.getHashes()[DHash] = str2bin(params["dHash"]);
}
if (params.count("cannyDHash")) {
record.getHashes()[CannyDHash] = str2bin(params["cannyDHash"]);
if (params.count("gaussDHash")) {
record.getHashes()[GaussDHash] = str2bin(params["gaussDHash"]);
}
if (params.count("gauss2DHash")) {
record.getHashes()[Gauss2DHash] = str2bin(params["gauss2DHash"]);
}
if (params.count("gaussBlockHash")) {
record.getHashes()[GaussBlockHash] = str2bin(params["gaussBlockHash"]);
}
if (params.count("url")) {
image_data = Image::fromUrl(params["url"]);
Expand Down
65 changes: 65 additions & 0 deletions fcgi/Gauss2DHashAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "Gauss2DHashAlgorithm.h"

#include <vector>
#include <sstream>
#include <cmath>

#include <opencv2/opencv.hpp>
#include <dhash.h>

#include "DHashException.h"

using namespace std;

Gauss2DHashAlgorithm::~Gauss2DHashAlgorithm() {
}

std::vector<uint64_t> Gauss2DHashAlgorithm::compute(const std::vector<uint8_t>* data) {
cv::Mat image = cv::imdecode(*data, CV_LOAD_IMAGE_GRAYSCALE);
cv::resize(image, image, getKeepRatioSize(image, 512), 0, 0, cv::INTER_LANCZOS4);

cv::Mat g1, g2;
cv::GaussianBlur(image, g1, cv::Size(3, 3), 0);
cv::GaussianBlur(image, g2, cv::Size(9, 9), 60);
image = g1 - g2;

std::vector<uint8_t> gaussdata;
cv::imencode(".png", image, gaussdata);

dhash_err* err = dhash_new_err();
uint64_t hash = dhash_compute_blob(&gaussdata[0], gaussdata.size(), err);

if (err->err_type != OK) {
ostringstream os;
os << "Error occured during compute dhash: " << err->description;
dhash_free_err(err);
throw DHashException(os.str());
}
dhash_free_err(err);

vector<uint64_t> result;
result.push_back(hash);
return result;
}

HashType Gauss2DHashAlgorithm::getType() {
return Gauss2DHash;
}

size_t Gauss2DHashAlgorithm::getHashSize() {
return 8;
}

cv::Size Gauss2DHashAlgorithm::getKeepRatioSize(const cv::Mat& image, int size) {
int width = image.cols;
int height = image.rows;

double ratio;

if (width < height) {
ratio = size * 1.0 / width;
} else {
ratio = size * 1.0 / height;
}
return cv::Size((int) round(width*ratio), (int) round(height*ratio));
}
22 changes: 22 additions & 0 deletions fcgi/Gauss2DHashAlgorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef GAUSS2DHASH_H
#define GAUSS2DHASH_H

#include <opencv2/opencv.hpp>

#include "HashAlgorithm.h"
#include "HashType.h"


class Gauss2DHashAlgorithm : public HashAlgorithm {
public:
virtual ~Gauss2DHashAlgorithm();

virtual std::vector<uint64_t> compute(const std::vector<uint8_t>* data);
virtual HashType getType();
virtual size_t getHashSize();
private:
virtual cv::Size getKeepRatioSize(const cv::Mat& image, int size);
};

#endif /* GAUSS2DHASH_H */

50 changes: 50 additions & 0 deletions fcgi/GaussBlockHashAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "GaussBlockHashAlgorithm.h"

#include <vector>
#include <sstream>
#include <cmath>

#include <opencv2/opencv.hpp>
#include <blockhash.h>

using namespace std;

GaussBlockHashAlgorithm::~GaussBlockHashAlgorithm() {
}

std::vector<uint64_t> GaussBlockHashAlgorithm::compute(const std::vector<uint8_t>* data) {
cv::Mat image = cv::imdecode(*data, CV_LOAD_IMAGE_GRAYSCALE);
cv::resize(image, image, getKeepRatioSize(image, 512), 0, 0, cv::INTER_LANCZOS4);

cv::Mat g1, g2;
cv::GaussianBlur(image, g1, cv::Size(1, 1), 0);
cv::GaussianBlur(image, g2, cv::Size(3, 3), 60);
image = g1 - g2;

std::vector<uint8_t> gaussdata;
cv::imencode(".png", image, gaussdata);

return blockhash::compute(gaussdata, 256);
}

HashType GaussBlockHashAlgorithm::getType() {
return GaussBlockHash;
}

size_t GaussBlockHashAlgorithm::getHashSize() {
return 32;
}

cv::Size GaussBlockHashAlgorithm::getKeepRatioSize(const cv::Mat& image, int size) {
int width = image.cols;
int height = image.rows;

double ratio;

if (width < height) {
ratio = size * 1.0 / width;
} else {
ratio = size * 1.0 / height;
}
return cv::Size((int) round(width*ratio), (int) round(height*ratio));
}
22 changes: 22 additions & 0 deletions fcgi/GaussBlockHashAlgorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef GAUSSBLOCKHASH_H
#define GAUSSBLOCKHASH_H

#include <opencv2/opencv.hpp>

#include "HashAlgorithm.h"
#include "HashType.h"


class GaussBlockHashAlgorithm : public HashAlgorithm {
public:
virtual ~GaussBlockHashAlgorithm();

virtual std::vector<uint64_t> compute(const std::vector<uint8_t>* data);
virtual HashType getType();
virtual size_t getHashSize();
private:
virtual cv::Size getKeepRatioSize(const cv::Mat& image, int size);
};

#endif /* GAUSSBLOCKHASH_H */

63 changes: 27 additions & 36 deletions fcgi/CannyDHashAlgorithm.cpp → fcgi/GaussDHashAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
/*
* File: CannyDHash.cpp
* Author: dudae
*
* Created on Štvrtok, 2015, júla 9, 10:12
*/

#include "CannyDHashAlgorithm.h"
#include "GaussDHashAlgorithm.h"

#include <vector>
#include <sstream>
Expand All @@ -18,39 +11,23 @@

using namespace std;

const int low_threshold = 50;
const int ratio = 3;
const int kernel_size = 3;

CannyDHashAlgorithm::~CannyDHashAlgorithm() {
}

cv::Size getKeepRatioSize(const cv::Mat& image, int size) {
int width = image.cols;
int height = image.rows;

double ratio;

if (width < height) {
ratio = size * 1.0 / width;
} else {
ratio = size * 1.0 / height;
}
return cv::Size((int) round(width*ratio), (int) round(height*ratio));
GaussDHashAlgorithm::~GaussDHashAlgorithm() {
}

std::vector<uint64_t> CannyDHashAlgorithm::compute(const std::vector<uint8_t>* data) {
std::vector<uint64_t> GaussDHashAlgorithm::compute(const std::vector<uint8_t>* data) {
cv::Mat image = cv::imdecode(*data, CV_LOAD_IMAGE_GRAYSCALE);
cv::blur(image, image, cv::Size(3, 3));
cv::resize(image, image, getKeepRatioSize(image, 512), 0, 0, cv::INTER_LANCZOS4);

cv::Canny(image, image, low_threshold, low_threshold*ratio, kernel_size);
cv::Mat g1, g2;
cv::GaussianBlur(image, g1, cv::Size(1, 1), 0);
cv::GaussianBlur(image, g2, cv::Size(3, 3), 60);
image = g1 - g2;

std::vector<uint8_t> cannydata;
cv::imencode(".png", image, cannydata);
std::vector<uint8_t> gaussdata;
cv::imencode(".png", image, gaussdata);

dhash_err* err = dhash_new_err();
uint64_t hash = dhash_compute_blob(&cannydata[0], cannydata.size(), err);
uint64_t hash = dhash_compute_blob(&gaussdata[0], gaussdata.size(), err);

if (err->err_type != OK) {
ostringstream os;
Expand All @@ -65,10 +42,24 @@ std::vector<uint64_t> CannyDHashAlgorithm::compute(const std::vector<uint8_t>* d
return result;
}

HashType CannyDHashAlgorithm::getType() {
return CannyDHash;
HashType GaussDHashAlgorithm::getType() {
return GaussDHash;
}

size_t CannyDHashAlgorithm::getHashSize() {
size_t GaussDHashAlgorithm::getHashSize() {
return 8;
}

cv::Size GaussDHashAlgorithm::getKeepRatioSize(const cv::Mat& image, int size) {
int width = image.cols;
int height = image.rows;

double ratio;

if (width < height) {
ratio = size * 1.0 / width;
} else {
ratio = size * 1.0 / height;
}
return cv::Size((int) round(width*ratio), (int) round(height*ratio));
}
Loading

0 comments on commit f497e61

Please sign in to comment.