Image Quality Assessment (IQA) 1.1.2
A fast, accurate, and reliable C library for measuring image quality.
source/ssim.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Tom Distler (http://tdistler.com)
00003  * All rights reserved.
00004  *
00005  * The BSD License
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  * - Redistributions of source code must retain the above copyright notice, 
00011  *   this list of conditions and the following disclaimer.
00012  *
00013  * - Redistributions in binary form must reproduce the above copyright notice,
00014  *   this list of conditions and the following disclaimer in the documentation
00015  *   and/or other materials provided with the distribution.
00016  *
00017  * - Neither the name of the tdistler.com nor the names of its contributors may
00018  *   be used to endorse or promote products derived from this software without
00019  *   specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
00025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  * POSSIBILITY OF SUCH DAMAGE.
00032  */
00033 
00034 #include "iqa.h"
00035 #include "convolve.h"
00036 #include "decimate.h"
00037 #include "math_utils.h"
00038 #include "ssim.h"
00039 #include <stdlib.h>
00040 #include <math.h>
00041 
00042 
00043 /* Forward declarations. */
00044 IQA_INLINE static double _calc_luminance(float, float, float, float);
00045 IQA_INLINE static double _calc_contrast(double, float, float, float, float);
00046 IQA_INLINE static double _calc_structure(float, double, float, float, float, float);
00047 static int _ssim_map(const struct _ssim_int *, void *);
00048 static float _ssim_reduce(int, int, void *);
00049 
00050 /* 
00051  * SSIM(x,y)=(2*ux*uy + C1)*(2sxy + C2) / (ux^2 + uy^2 + C1)*(sx^2 + sy^2 + C2)
00052  * where,
00053  *  ux = SUM(w*x)
00054  *  sx = (SUM(w*(x-ux)^2)^0.5
00055  *  sxy = SUM(w*(x-ux)*(y-uy))
00056  *
00057  * Returns mean SSIM. MSSIM(X,Y) = 1/M * SUM(SSIM(x,y))
00058  */
00059 float iqa_ssim(const unsigned char *ref, const unsigned char *cmp, int w, int h, int stride,
00060     int gaussian, const struct iqa_ssim_args *args)
00061 {
00062     int scale;
00063     int x,y,src_offset,offset;
00064     float *ref_f,*cmp_f;
00065     struct _kernel low_pass;
00066     struct _kernel window;
00067     float result;
00068     double ssim_sum=0.0;
00069     struct _map_reduce mr;
00070 
00071     /* Initialize algorithm parameters */
00072     scale = _max( 1, _round( (float)_min(w,h) / 256.0f ) );
00073     if (args) {
00074         if(args->f)
00075             scale = args->f;
00076         mr.map     = _ssim_map;
00077         mr.reduce  = _ssim_reduce;
00078         mr.context = (void*)&ssim_sum;
00079     }
00080     window.kernel = (float*)g_square_window;
00081     window.w = window.h = SQUARE_LEN;
00082     window.normalized = 1;
00083     window.bnd_opt = KBND_SYMMETRIC;
00084     if (gaussian) {
00085         window.kernel = (float*)g_gaussian_window;
00086         window.w = window.h = GAUSSIAN_LEN;
00087     }
00088 
00089     /* Convert image values to floats. Forcing stride = width. */
00090     ref_f = (float*)malloc(w*h*sizeof(float));
00091     cmp_f = (float*)malloc(w*h*sizeof(float));
00092     if (!ref_f || !cmp_f) {
00093         if (ref_f) free(ref_f);
00094         if (cmp_f) free(cmp_f);
00095         return INFINITY;
00096     }
00097     for (y=0; y<h; ++y) {
00098         src_offset = y*stride;
00099         offset = y*w;
00100         for (x=0; x<w; ++x, ++offset, ++src_offset) {
00101             ref_f[offset] = (float)ref[src_offset];
00102             cmp_f[offset] = (float)cmp[src_offset];
00103         }
00104     }
00105 
00106     /* Scale the images down if required */
00107     if (scale > 1) {
00108         /* Generate simple low-pass filter */
00109         low_pass.kernel = (float*)malloc(scale*scale*sizeof(float));
00110         if (!low_pass.kernel) {
00111             free(ref_f);
00112             free(cmp_f);
00113             return INFINITY;
00114         }
00115         low_pass.w = low_pass.h = scale;
00116         low_pass.normalized = 0;
00117         low_pass.bnd_opt = KBND_SYMMETRIC;
00118         for (offset=0; offset<scale*scale; ++offset)
00119             low_pass.kernel[offset] = 1.0f/(scale*scale);
00120 
00121         /* Resample */
00122         if (_iqa_decimate(ref_f, w, h, scale, &low_pass, 0, 0, 0) ||
00123             _iqa_decimate(cmp_f, w, h, scale, &low_pass, 0, &w, &h)) { /* Update w/h */
00124             free(ref_f);
00125             free(cmp_f);
00126             free(low_pass.kernel);
00127             return INFINITY;
00128         }
00129         free(low_pass.kernel);
00130     }
00131 
00132     result = _iqa_ssim(ref_f, cmp_f, w, h, &window, &mr, args);
00133     
00134     free(ref_f);
00135     free(cmp_f);
00136 
00137     return result;
00138 }
00139 
00140 
00141 /* _iqa_ssim */
00142 float _iqa_ssim(float *ref, float *cmp, int w, int h, const struct _kernel *k, const struct _map_reduce *mr, const struct iqa_ssim_args *args)
00143 {
00144     float alpha=1.0f, beta=1.0f, gamma=1.0f;
00145     int L=255;
00146     float K1=0.01f, K2=0.03f;
00147     float C1,C2,C3;
00148     int x,y,offset;
00149     float *ref_mu,*cmp_mu,*ref_sigma_sqd,*cmp_sigma_sqd,*sigma_both;
00150     double ssim_sum, numerator, denominator;
00151     double luminance_comp, contrast_comp, structure_comp, sigma_root;
00152     struct _ssim_int sint;
00153 
00154     /* Initialize algorithm parameters */
00155     if (args) {
00156         if (!mr)
00157             return INFINITY;
00158         alpha = args->alpha;
00159         beta  = args->beta;
00160         gamma = args->gamma;
00161         L     = args->L;
00162         K1    = args->K1;
00163         K2    = args->K2;
00164     }
00165     C1 = (K1*L)*(K1*L);
00166     C2 = (K2*L)*(K2*L);
00167     C3 = C2 / 2.0f;
00168 
00169     ref_mu = (float*)malloc(w*h*sizeof(float));
00170     cmp_mu = (float*)malloc(w*h*sizeof(float));
00171     ref_sigma_sqd = (float*)malloc(w*h*sizeof(float));
00172     cmp_sigma_sqd = (float*)malloc(w*h*sizeof(float));
00173     sigma_both = (float*)malloc(w*h*sizeof(float));
00174     if (!ref_mu || !cmp_mu || !ref_sigma_sqd || !cmp_sigma_sqd || !sigma_both) {
00175         if (ref_mu) free(ref_mu);
00176         if (cmp_mu) free(cmp_mu);
00177         if (ref_sigma_sqd) free(ref_sigma_sqd);
00178         if (cmp_sigma_sqd) free(cmp_sigma_sqd);
00179         if (sigma_both) free(sigma_both);
00180         return INFINITY;
00181     }
00182 
00183     /* Calculate mean */
00184     _iqa_convolve(ref, w, h, k, ref_mu, 0, 0);
00185     _iqa_convolve(cmp, w, h, k, cmp_mu, 0, 0);
00186 
00187     for (y=0; y<h; ++y) {
00188         offset = y*w;
00189         for (x=0; x<w; ++x, ++offset) {
00190             ref_sigma_sqd[offset] = ref[offset] * ref[offset];
00191             cmp_sigma_sqd[offset] = cmp[offset] * cmp[offset];
00192             sigma_both[offset] = ref[offset] * cmp[offset];
00193         }
00194     }
00195 
00196     /* Calculate sigma */
00197     _iqa_convolve(ref_sigma_sqd, w, h, k, 0, 0, 0);
00198     _iqa_convolve(cmp_sigma_sqd, w, h, k, 0, 0, 0);
00199     _iqa_convolve(sigma_both, w, h, k, 0, &w, &h); /* Update the width and height */
00200 
00201     /* The convolution results are smaller by the kernel width and height */
00202     for (y=0; y<h; ++y) {
00203         offset = y*w;
00204         for (x=0; x<w; ++x, ++offset) {
00205             ref_sigma_sqd[offset] -= ref_mu[offset] * ref_mu[offset];
00206             cmp_sigma_sqd[offset] -= cmp_mu[offset] * cmp_mu[offset];
00207             sigma_both[offset] -= ref_mu[offset] * cmp_mu[offset];
00208         }
00209     }
00210 
00211     ssim_sum = 0.0;
00212     for (y=0; y<h; ++y) {
00213         offset = y*w;
00214         for (x=0; x<w; ++x, ++offset) {
00215 
00216             if (!args) {
00217                 /* The default case */
00218                 numerator   = (2.0 * ref_mu[offset] * cmp_mu[offset] + C1) * (2.0 * sigma_both[offset] + C2);
00219                 denominator = (ref_mu[offset]*ref_mu[offset] + cmp_mu[offset]*cmp_mu[offset] + C1) * 
00220                     (ref_sigma_sqd[offset] + cmp_sigma_sqd[offset] + C2);
00221                 ssim_sum += numerator / denominator;
00222             }
00223             else {
00224                 /* User tweaked alpha, beta, or gamma */
00225 
00226                 /* passing a negative number to sqrt() cause a domain error */
00227                 if (ref_sigma_sqd[offset] < 0.0f)
00228                     ref_sigma_sqd[offset] = 0.0f;
00229                 if (cmp_sigma_sqd[offset] < 0.0f)
00230                     cmp_sigma_sqd[offset] = 0.0f;
00231                 sigma_root = sqrt(ref_sigma_sqd[offset] * cmp_sigma_sqd[offset]);
00232 
00233                 luminance_comp = _calc_luminance(ref_mu[offset], cmp_mu[offset], C1, alpha);
00234                 contrast_comp  = _calc_contrast(sigma_root, ref_sigma_sqd[offset], cmp_sigma_sqd[offset], C2, beta);
00235                 structure_comp = _calc_structure(sigma_both[offset], sigma_root, ref_sigma_sqd[offset], cmp_sigma_sqd[offset], C3, gamma);
00236 
00237                 sint.l = luminance_comp;
00238                 sint.c = contrast_comp;
00239                 sint.s = structure_comp;
00240 
00241                 if (mr->map(&sint, mr->context))
00242                     return INFINITY;
00243             }
00244         }
00245     }
00246 
00247     free(ref_mu);
00248     free(cmp_mu);
00249     free(ref_sigma_sqd);
00250     free(cmp_sigma_sqd);
00251     free(sigma_both);
00252 
00253     if (!args)
00254         return (float)(ssim_sum / (double)(w*h));
00255     return mr->reduce(w, h, mr->context);
00256 }
00257 
00258 
00259 /* _ssim_map */
00260 int _ssim_map(const struct _ssim_int *si, void *ctx)
00261 {
00262     double *ssim_sum = (double*)ctx;
00263     *ssim_sum += si->l * si->c * si->s;
00264     return 0;
00265 }
00266 
00267 /* _ssim_reduce */
00268 float _ssim_reduce(int w, int h, void *ctx)
00269 {
00270     double *ssim_sum = (double*)ctx;
00271     return (float)(*ssim_sum / (double)(w*h));
00272 }
00273 
00274 
00275 /* _calc_luminance */
00276 IQA_INLINE static double _calc_luminance(float mu1, float mu2, float C1, float alpha)
00277 {
00278     double result;
00279     float sign;
00280     /* For MS-SSIM* */
00281     if (C1 == 0 && mu1*mu1 == 0 && mu2*mu2 == 0)
00282         return 1.0;
00283     result = (2.0 * mu1 * mu2 + C1) / (mu1*mu1 + mu2*mu2 + C1);
00284     if (alpha == 1.0f)
00285         return result;
00286     sign = result < 0.0 ? -1.0f : 1.0f;
00287     return sign * pow(fabs(result),(double)alpha);
00288 }
00289 
00290 /* _calc_contrast */
00291 IQA_INLINE static double _calc_contrast(double sigma_comb_12, float sigma1_sqd, float sigma2_sqd, float C2, float beta)
00292 {
00293     double result;
00294     float sign;
00295     /* For MS-SSIM* */
00296     if (C2 == 0 && sigma1_sqd + sigma2_sqd == 0)
00297         return 1.0;
00298     result = (2.0 * sigma_comb_12 + C2) / (sigma1_sqd + sigma2_sqd + C2);
00299     if (beta == 1.0f)
00300         return result;
00301     sign = result < 0.0 ? -1.0f : 1.0f;
00302     return sign * pow(fabs(result),(double)beta);
00303 }
00304 
00305 /* _calc_structure */
00306 IQA_INLINE static double _calc_structure(float sigma_12, double sigma_comb_12, float sigma1, float sigma2, float C3, float gamma)
00307 {
00308     double result;
00309     float sign;
00310     /* For MS-SSIM* */
00311     if (C3 == 0 && sigma_comb_12 == 0) {
00312         if (sigma1 == 0 && sigma2 == 0)
00313             return 1.0;
00314         else if (sigma1 == 0 || sigma2 == 0)
00315             return 0.0;
00316     }
00317     result = (sigma_12 + C3) / (sigma_comb_12 + C3);
00318     if (gamma == 1.0f)
00319         return result;
00320     sign = result < 0.0 ? -1.0f : 1.0f;
00321     return sign * pow(fabs(result),(double)gamma);
00322 }
 All Data Structures Files Functions Variables Typedefs Defines


Copyright (c) 2011 by Tom Distler