OpenCV realization of SSIM structure similarity algorithm

Source: Internet
Author: User
Tags mul scalar

Introduction to the SSIM algorithm:
http://blog.csdn.net/chaipp0607/article/details/70158835

The code does a bit of processing:
(1) Set up two sets of contrast test, the original image of the kernel for the 5*5 filter, compared with the original image to obtain the Ssim index. The original image is filtered by 10*10, and the Ssim index is obtained by comparison with the original image.
(2) Convert the Ssim index to percentile
(3) using Gaussian blur to obtain the mean value of the image

Code reference:
Http://jingyan.baidu.com/article/456c463b67aa310a5931447a.html

#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> #include <opencv2/highgui/ 
Highgui.hpp> #include <iostream> using namespace std;

using namespace CV;
Scalar Getmssim (Mat Inputimage1, Mat inputimage2);
    int main () {Mat BlurImage1;
    Mat BlurImage2;
    Mat srcimage = Imread ("1.jpg");
    Blur (Srcimage, BlurImage1, Size (5, 5));
    Blur (Srcimage,blurimage2,size (10,10));
    Scalar SSIM1 = Getmssim (Srcimage, BlurImage1);
    Scalar SSIM2 = Getmssim (Srcimage, BlurImage2);
    printf ("Fuzzy 5*5 channel 1:%f\n", Ssim1.val[0] * 100);
    printf ("Fuzzy 5*5 channel 2:%f\n", Ssim1.val[1] * 100);
    printf ("Fuzzy 5*5 channel 3:%f\n", Ssim1.val[2] * 100);
    printf ("Fuzzy 5*5:%f\n", (Ssim1.val[2] + ssim1.val[1] + ssim1.val[0])/3 * 100);
    printf ("Fuzzy 10*10 channel 1:%f\n", Ssim2.val[0] * 100);
    printf ("Fuzzy 10*10 channel 2:%f\n", Ssim2.val[1] * 100);
    printf ("Fuzzy 10*10 channel 3:%f\n", Ssim2.val[2] * 100);
    printf ("Fuzzy 10*10:%f\n", (Ssim2.val[2] + ssim2.val[1] + ssim2.val[0])/3 * 100); ImsHow ("original", Srcimage);
    Imshow ("Fuzzy 5*5", BlurImage1);
    Imshow ("Fuzzy 10*10", BlurImage2);
    Waitkey (0);
return 0;
    } Scalar Getmssim (Mat Inputimage1, Mat inputimage2) {mat i1 = Inputimage1;
    Mat i2 = Inputimage2;
    Const double C1 = 6.5025, C2 = 58.5225;
    int d = cv_32f;
    Mat I1, I2;
    I1.convertto (I1, D);
    I2.convertto (I2, D);
    Mat i2_2 = I2.mul (I2);
    Mat i1_2 = I1.mul (I1);
    Mat I1_i2 = I1.mul (I2);
    Mat MU1, MU2;
    Gaussianblur (I1, MU1, Size (11, 11), 1.5);
    Gaussianblur (I2, MU2, Size (11, 11), 1.5);
    Mat mu1_2 = Mu1.mul (MU1);
    Mat mu2_2 = Mu2.mul (MU2);
    Mat MU1_MU2 = Mu1.mul (MU2);
    Mat sigma1_2, sigma2_2, Sigma12;
    Gaussianblur (I1_2, Sigma1_2, Size (11, 11), 1.5);
    Sigma1_2-= mu1_2;
    Gaussianblur (I2_2, Sigma2_2, Size (11, 11), 1.5);
    Sigma2_2-= mu2_2;
    Gaussianblur (I1_i2, Sigma12, Size (11, 11), 1.5);
    Sigma12-= MU1_MU2;
    Mat T1, T2, T3;
    T1 = 2 * MU1_MU2 + C1;
    T2 = 2 * sigma12 + C2;
 T3 = T1.mul (T2);   T1 = mu1_2 + mu2_2 + C1;
    T2 = sigma1_2 + sigma2_2 + C2;
    T1 = T1.mul (T2);
    Mat Ssim_map;
    Divide (T3, T1, Ssim_map);
    Scalar MsSIM = mean (SSIM_MAP);
return MsSIM; }

Printing results:
Blur 5*5 Channel 1:82.523627
Blur 5*5 Channel 2:85.781376
Blur 5*5 Channel 3:85.903646
Fuzzy 5*5:84.736216
Blur 10*10 Channel 1:65.029142
Blur 10*10 Channel 2:69.286267
Blur 10*10 Channel 3:68.664205
Fuzzy 10*10:67.659871



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.