Debugging the image comparison.

This commit is contained in:
Nissim Hadar 2017-11-30 16:27:28 -08:00
parent 0842e09570
commit c63571a710

View file

@ -10,11 +10,8 @@
#include "ImageComparer.h" #include "ImageComparer.h"
// Computes SSIM - see https://en.wikipedia.org/wiki/Structural_similarity // Computes SSIM - see https://en.wikipedia.org/wiki/Structural_similarity
// The value is computed for the luminence component and the maximum value is returned // The value is computed for the luminance component and the average value is returned
double ImageComparer::compareImages(QImage resultImage, QImage expectedImage) const { double ImageComparer::compareImages(QImage resultImage, QImage expectedImage) const {
const double K1{ 0.01 };
const double K2{ 0.03 };
// Make sure the image is 8 bits per colour // Make sure the image is 8 bits per colour
QImage::Format format = expectedImage.format(); QImage::Format format = expectedImage.format();
if (format != QImage::Format::Format_RGB32) { if (format != QImage::Format::Format_RGB32) {
@ -22,8 +19,10 @@ double ImageComparer::compareImages(QImage resultImage, QImage expectedImage) co
} }
const int L = 255; // (2^number of bits per pixel) - 1 const int L = 255; // (2^number of bits per pixel) - 1
const double c1 = (K1 * L) * (K1 * L); const double K1{ 0.01 };
const double c2 = (K2 * L) * (K2 * L); const double K2{ 0.03 };
const double c1 = pow((K1 * L), 2.0);
const double c2 = pow((K2 * L), 2.0);
// Coefficients for luminosity calculation // Coefficients for luminosity calculation
const double R_Y = 0.212655f; const double R_Y = 0.212655f;
@ -42,21 +41,22 @@ double ImageComparer::compareImages(QImage resultImage, QImage expectedImage) co
const int WIN_SIZE = 8; const int WIN_SIZE = 8;
int x{ 0 }; // column index (start of block) int x{ 0 }; // column index (start of block)
int y{ 0 }; // row index (start of block int y{ 0 }; // row index (start of block
double ssimMin{ 1.0 };
// Pixels are processed in sqare blocks // Pixels are processed in square blocks
double p[WIN_SIZE * WIN_SIZE]; double p[WIN_SIZE * WIN_SIZE];
double q[WIN_SIZE * WIN_SIZE]; double q[WIN_SIZE * WIN_SIZE];
int windowCounter{ 0 };
double ssim{ 0.0 };
while (x < expectedImage.width()) { while (x < expectedImage.width()) {
int lastX = x + WIN_SIZE; int lastX = x + WIN_SIZE - 1;
if (lastX > expectedImage.width()) { if (lastX > expectedImage.width() - 1) {
x -= (lastX - expectedImage.width()); x -= (lastX - expectedImage.width());
} }
while (y < expectedImage.height()) { while (y < expectedImage.height()) {
int lastY = y + WIN_SIZE; int lastY = y + WIN_SIZE - 1;
if (lastY > expectedImage.height()) { if (lastY > expectedImage.height() - 1) {
y -= (lastY - expectedImage.height()); y -= (lastY - expectedImage.height());
} }
@ -68,7 +68,7 @@ double ImageComparer::compareImages(QImage resultImage, QImage expectedImage) co
QRgb pixelP = expectedImage.pixel(QPoint(x + xx, y + yy)); QRgb pixelP = expectedImage.pixel(QPoint(x + xx, y + yy));
QRgb pixelQ = resultImage.pixel(QPoint(x + xx, y + yy)); QRgb pixelQ = resultImage.pixel(QPoint(x + xx, y + yy));
// Convert to luminence // Convert to luminance
p[i] = R_Y * qRed(pixelP) + G_Y * qGreen(pixelP) + B_Y * qBlue(pixelP); p[i] = R_Y * qRed(pixelP) + G_Y * qGreen(pixelP) + B_Y * qBlue(pixelP);
q[i] = R_Y * qRed(pixelQ) + G_Y * qGreen(pixelQ) + B_Y * qBlue(pixelQ); q[i] = R_Y * qRed(pixelQ) + G_Y * qGreen(pixelQ) + B_Y * qBlue(pixelQ);
@ -91,8 +91,8 @@ double ImageComparer::compareImages(QImage resultImage, QImage expectedImage) co
double sigsqQ{ 0.0 }; double sigsqQ{ 0.0 };
double sigPQ{ 0.0 }; double sigPQ{ 0.0 };
for (int j = 0; j < WIN_SIZE * WIN_SIZE; ++j) { for (int j = 0; j < WIN_SIZE * WIN_SIZE; ++j) {
sigsqP += (p[j] - mP) * (p[j] - mP); sigsqP += pow((p[j] - mP), 2.0);
sigsqQ += (q[j] - mQ) * (q[j] - mQ); sigsqQ += pow((q[j] - mQ), 2.0);
sigPQ += (p[j] - mP) * (q[j] - mQ); sigPQ += (p[j] - mP) * (q[j] - mQ);
} }
@ -103,16 +103,15 @@ double ImageComparer::compareImages(QImage resultImage, QImage expectedImage) co
double numerator = (2.0 * mP * mQ + c1) * (2.0 * sigPQ + c2); double numerator = (2.0 * mP * mQ + c1) * (2.0 * sigPQ + c2);
double denominator = (mP * mP + mQ * mQ + c1) * (sigsqP + sigsqQ + c2); double denominator = (mP * mP + mQ * mQ + c1) * (sigsqP + sigsqQ + c2);
double ssim = numerator / denominator; ssim += numerator / denominator;
++windowCounter;
if (ssim < ssimMin) {
ssimMin = ssim;
}
y += WIN_SIZE; y += WIN_SIZE;
} }
x += WIN_SIZE; x += WIN_SIZE;
y = 0;
} }
return ssimMin; return ssim / windowCounter;
}; };