Skip to content

Commit

Permalink
0.2.8: fixed vifp1 and added mse-c and psnr-c
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Jan 9, 2023
1 parent 4447402 commit 973843d
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 49 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Includes metrics:

- MSE / MAX^2, MAX = 255
- PSNR
- MSE / MAX^2 of correlation, MAX = 255
- PSNR of correlation
- [SSIM](https://github.com/rolinh/VQMT)
- [VIFP1](https://github.com/rolinh/VQMT)
- [SMALLFRY](https://github.com/dwbuiten/smallfry)
Expand Down Expand Up @@ -135,8 +137,10 @@ Save png: lena.quant444.psnr.png

* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.mse.png) MSE: 0.013604
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.psnr.png) PSNR: 18.663233
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.mse-c.png) MSE-C: 0.006495, UM: -
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.psnr-c.png) PSNR-C: 21.873943, UM: -
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.ssim.png) SSIM: 0.618509, UM: -0.239359
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.vifp1.png) VIFP1: 0.806863, UM: -
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.vifp1.png) VIFP1: 0.170903, UM: -
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.smallfry.png) SMALLFRY: 83.753342
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.shbad.png) SHARPENBAD: -0.213132
* ![metric](https://raw.githubusercontent.com/ImageProcessing-ElectronicPublications/stb-image-metrics-demo/main/images/lena.quant444.cor.png) Corelation: 0.953666
Expand Down
6 changes: 5 additions & 1 deletion man/man1/stbimmetrics.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH "StbImMetrics" 1 0.2.7 "9 Jan 2023" "User Manual"
.TH "StbImMetrics" 1 0.2.8 "9 Jan 2023" "User Manual"

.SH NAME
stbimmetrics
Expand All @@ -9,6 +9,8 @@ This utility compares two graphics files based on metrics.
Includes metrics:
- MSE / MAX^2, MAX = 255
- PSNR
- MSE / MAX^2 of correlation, MAX = 255
- PSNR of correlation
- SSIM
- VIFP1
- SMALLFRY
Expand Down Expand Up @@ -39,6 +41,8 @@ stbimmetrics [options] ${IMAGE_ORIG_PATH} ${IMAGE_COMPARE_PATH} [metric.out.png]
metric (default psnr):
mse - MSE / MAX^2, MAX = 255
psnr - PSNR
mse-c - MSE / MAX^2 of correlation, MAX = 255
psnr-c - PSNR of correlation
ssim - SSIM
vifp1 - VIFP 1 layer
smallfry - SMALLFRY
Expand Down
4 changes: 2 additions & 2 deletions src/metricsnhw.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
****************************************************************************
* Metrics NHW *
* file: metricsnhw.h *
* version: 0.2.7 *
* version: 0.2.8 *
* *
****************************************************************************
***************************************************************************/
Expand All @@ -18,7 +18,7 @@
#ifndef __METRICS_NHW__H
#define __METRICS_NHW__H

#define METRICSNHW_VERSION "0.2.7"
#define METRICSNHW_VERSION "0.2.8"

#ifdef METRICS_STATIC
#define METRICSAPI static
Expand Down
157 changes: 155 additions & 2 deletions src/metricspsnr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
****************************************************************************
* Metrics PSNR *
* file: metricspsnr.h *
* version: 0.2.7 *
* version: 0.2.8 *
* *
****************************************************************************
***************************************************************************/
Expand All @@ -14,7 +14,7 @@
#ifndef __METRICS_PSNR__H
#define __METRICS_PSNR__H

#define METRICS_PSNR_VERSION "0.2.7"
#define METRICS_PSNR_VERSION "0.2.8"

#ifdef METRICS_STATIC
#define METRICSAPI static
Expand All @@ -27,6 +27,8 @@ extern "C" {
#endif
METRICSAPI float metrics_mse(unsigned char *ref, unsigned char *cmp, unsigned char* delta, int height, int width, int channels);
METRICSAPI float metrics_psnr(unsigned char *ref, unsigned char *cmp, unsigned char* delta, int height, int width, int channels);
METRICSAPI float metrics_mse_cor(unsigned char *ref, unsigned char *cmp, unsigned char* delta, int height, int width, int channels);
METRICSAPI float metrics_psnr_cor(unsigned char *ref, unsigned char *cmp, unsigned char* delta, int height, int width, int channels);
#ifdef __cplusplus
}
#endif
Expand Down Expand Up @@ -101,6 +103,157 @@ METRICSAPI float metrics_psnr(unsigned char *ref, unsigned char *cmp, unsigned c
return psnr;
}

METRICSAPI float metrics_mse_cor(unsigned char *ref, unsigned char *cmp, unsigned char* delta, int height, int width, int channels)
{
float im1, im2;
float sum1, sum2, sum1l, sum2l, suml, sum, error;
float sum12, sumq1, sumq2, q12, sum12l, sumq1l, sumq2l, sumq, cor;
int y, x, d;
size_t k, n;

n = width * height;

k = 0;
sum1 = 0.0f;
sum2 = 0.0f;
for (y = 0; y < height; y++)
{
sum1l = 0.0f;
sum2l = 0.0f;
for (x = 0; x < width; x++)
{
for (d = 0; d < channels; d++)
{
im1 = (float)ref[k];
im2 = (float)cmp[k];
sum1l += im1;
sum2l += im2;
k++;
}
}
sum1 += sum1l;
sum2 += sum2l;
}
sum1 /= (float)k;
sum2 /= (float)k;

k = 0;
sum12 = 0.0f;
sumq1 = 0.0f;
sumq2 = 0.0f;
for (y = 0; y < height; y++)
{
sum12l = 0.0f;
sumq1l = 0.0f;
sumq2l = 0.0f;
for (x = 0; x < width; x++)
{
for (d = 0; d < channels; d++)
{
im1 = (float)ref[k];
im1 -= sum1;
im2 = (float)cmp[k];
im2 -= sum2;
q12 = (im1 * im2);
sum12l += q12;
sumq1l += (im1 * im1);
sumq2l += (im2 * im2);
k++;
}
}
sum12 += sum12l;
sumq1 += sumq1l;
sumq2 += sumq2l;
}
sumq = sqrt(sumq1 * sumq2);
if (sumq > 0.0f)
{
cor = sum12 / sumq;
} else {
cor = (sumq1 == sumq2) ? 1.0f : 0.0f;
}
cor = (cor < 0.0f) ? -cor : cor;
sumq1 /= k;
sumq2 /= k;
sumq1 = sqrt(sumq1);
sumq2 = sqrt(sumq2);
q12 = (sumq1 + sumq2);
if (q12 > 0.0f)
{
sumq1 *= (2.0f / q12);
sumq2 *= (2.0f / q12);
}
else
{
sumq1 = 1.0f;
sumq2 = 1.0f;
}
k = 0;
sum = 0.0f;
for (y = 0; y < height; y++)
{
suml = 0.0f;
for (x = 0; x < width; x++)
{
for (d = 0; d < channels; d++)
{
im1 = (float)ref[k];
im1 -= sum1;
im1 *= sumq2;
im2 = (float)cmp[k];
im2 -= sum2;
im2 *= sumq1;
error = (im1 > im2) ? (im1 - im2) : (im2 - im1);
suml += error * error;
if (delta) delta[k] = (unsigned char)error;
k++;
}
}
sum += suml;
}
sum /= (float)height;
sum /= (float)width;
sum /= (float)channels;
sum /= 255.0f;
sum /= 255.0f;

return sum;
}

METRICSAPI float metrics_psnr_cor(unsigned char *ref, unsigned char *cmp, unsigned char* delta, int height, int width, int channels)
{
int y, x, d;
size_t k;
float mse, psnr, t, sum1, sum2, sum1l, sum2l;

mse = metrics_mse_cor(ref, cmp, delta, height, width, channels);
psnr = 10.0f * log10( 1.0f / mse);

if (delta)
{
k = 0;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
for (d = 0; d < channels; d++)
{
t = (float)delta[k];
t /= 255.0f;
t *= t;
t += mse;
t *= 0.5f;
t = (t > 0.0f) ? (10.0f * log10( 1.0f / t)) : 255.0f;
delta[k] = (unsigned char)((t < 255.0f) ? t : 255);
k++;
}
}
}
}

return psnr;
}

#endif /* METRICS_PSNR_IMPLEMENTATION */

#endif // __METRICS_PSNR__H
4 changes: 2 additions & 2 deletions src/metricsum.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
****************************************************************************
* Metrics UM *
* file: metricsum.h *
* version: 0.2.7 *
* version: 0.2.8 *
* *
****************************************************************************
***************************************************************************/
Expand All @@ -15,7 +15,7 @@
#ifndef __METRICS_UM__H
#define __METRICS_UM__H

#define METRICS_VERSION "0.2.7"
#define METRICS_VERSION "0.2.8"

#ifdef METRICS_STATIC
#define METRICSAPI static
Expand Down
40 changes: 14 additions & 26 deletions src/smallfry.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
****************************************************************************
* Metrics PSNR *
* file: metricspsnr.h *
* version: 0.2.7 *
* version: 0.2.8 *
* *
****************************************************************************
***************************************************************************/
Expand All @@ -31,7 +31,7 @@
#ifndef __METRICS_SMALLFRY__H
#define __METRICS_SMALLFRY__H

#define METRICS_SMALLFRY_VERSION "0.2.7"
#define METRICS_SMALLFRY_VERSION "0.2.8"

#ifdef METRICS_STATIC
#define METRICSAPI static
Expand Down Expand Up @@ -390,7 +390,6 @@ METRICSAPI float metric_cor (unsigned char *ref, unsigned char *cmp, unsigned ch
float im1, im2;
float sum1, sum2, sum1l, sum2l;
float sum12, sumq1, sumq2, q12, sum12l, sumq1l, sumq2l, sumq, cor;
float suma1l, suma2l, suma1, suma2;
int y, x, d;
size_t k, n;

Expand Down Expand Up @@ -424,15 +423,11 @@ METRICSAPI float metric_cor (unsigned char *ref, unsigned char *cmp, unsigned ch
sum12 = 0.0f;
sumq1 = 0.0f;
sumq2 = 0.0f;
suma1 = 0.0f;
suma2 = 0.0f;
for (y = 0; y < height; y++)
{
sum12l = 0.0f;
sumq1l = 0.0f;
sumq2l = 0.0f;
suma1l = 0.0f;
suma2l = 0.0f;
for (x = 0; x < width; x++)
{
for (d = 0; d < channels; d++)
Expand All @@ -442,8 +437,6 @@ METRICSAPI float metric_cor (unsigned char *ref, unsigned char *cmp, unsigned ch
im2 = (float)cmp[k];
im2 -= sum2;
q12 = (im1 * im2);
suma1l += ((im1 < 0) ? -im1 : im1);
suma2l += ((im2 < 0) ? -im2 : im2);
sum12l += q12;
sumq1l += (im1 * im1);
sumq2l += (im2 * im2);
Expand All @@ -453,8 +446,6 @@ METRICSAPI float metric_cor (unsigned char *ref, unsigned char *cmp, unsigned ch
sum12 += sum12l;
sumq1 += sumq1l;
sumq2 += sumq2l;
suma1 += suma1l;
suma2 += suma2l;
}
sumq = sqrt(sumq1 * sumq2);
if (sumq > 0.0f)
Expand All @@ -466,18 +457,20 @@ METRICSAPI float metric_cor (unsigned char *ref, unsigned char *cmp, unsigned ch
cor = (cor < 0.0f) ? -cor : cor;
if (delta)
{
suma1 /= k;
suma2 /= k;
q12 = (suma1 + suma2);
sumq1 /= k;
sumq2 /= k;
sumq1 = sqrt(sumq1);
sumq2 = sqrt(sumq2);
q12 = (sumq1 + sumq2);
if (q12 > 0.0f)
{
suma1 *= (2.0f / q12);
suma2 *= (2.0f / q12);
sumq1 *= (2.0f / q12);
sumq2 *= (2.0f / q12);
}
else
{
suma1 = 1.0f;
suma2 = 1.0f;
sumq1 = 1.0f;
sumq2 = 1.0f;
}
k = 0;
for (y = 0; y < height; y++)
Expand All @@ -488,21 +481,16 @@ METRICSAPI float metric_cor (unsigned char *ref, unsigned char *cmp, unsigned ch
{
im1 = (float)ref[k];
im1 -= sum1;
im1 *= suma2;
im1 *= sumq2;
im2 = (float)cmp[k];
im2 -= sum2;
im2 *= suma1;
q12 = (im1 > im2) ? (im1 - im2) : (im2 > im1);
im2 *= sumq1;
q12 = (im1 > im2) ? (im1 - im2) : (im2 - im1);
q12 = 255.0f - q12;
delta[k] = (unsigned char)((q12 < 255.0f) ? q12 : 255);
k++;
}
}
sum12 += sum12l;
sumq1 += sumq1l;
sumq2 += sumq2l;
suma1 += suma1l;
suma2 += suma2l;
}
}

Expand Down
Loading

0 comments on commit 973843d

Please sign in to comment.