Coder Social home page Coder Social logo

ssim's Introduction

rmgr::ssim

This is a small and fast library whose only purpose in life is to compute the SSIM (Structural Similarity) of two images. SSIM is intended to more closely model human perception than PSNR (Peak Signal to Noise Ratio) does.

Portability

The library is written in C++ 98 so as to make it as portable as possible and has no dependency beyond compiler-provided headers and libraries. It is tested against several different compilers (namely Visual C++, GCC and Clang) and with several versions of each on several different platforms.

Usage

For the common use case, there's only one function you will need: rmgr::ssim::compute_ssim(). Granted, it takes quite a few parameters but that's the price to pay for flexibility and there's nothing really complicated about them.

compute_ssim() can optionally compute the per-pixel SSIM map (see below), which has a small impact on performance, but can come in handy.

The default behaviour is to make no allocation and only use stack-based storage, which is fine if you have at least 400 KiB of stack space. If that's a lot for your stack to handle, you can have the function allocate buffers off the heap instead.

Speed

SSIM is not exactly renowned for its speed, each pixel requiring 1230 arithmetic operations (which can be lowered by exploiting symmetries, but this is to be compared with PSNR requiring only 2 operations). With this in mind, the library has been thoroughly optimized in order to make SSIM computation as light as possible.

The library can use the SSE, AVX or FMA instruction sets (AVX-512 is not yet supported) with runtime detection of which is available. Multi-threaded operation is also possible using OpenMP (see below how to enable support).

Figures

Here is a table of performance figures on my laptop's Core i7-4710HQ:

Without map With map OpenMP w/o map OpenMP w/ map
Generic 9.3 Mpix/s 9.1 Mpix/s 40.6 Mpix/s 39.8 Mpix/s
SSE 34.7 Mpix/s 34.2 Mpix/s 144.2 Mpix/s 135.1 Mpix/s
AVX 53.3 Mpix/s 51.9 Mpix/s 223.5 Mpix/s 206.0 Mpix/s
FMA 57.3 Mpix/s 55.6 Mpix/s 232.4 Mpix/s 223.4 Mpix/s

An ARM version is also available with Neon (or ASIMD) support to speed up operation. The following are performance figures from a quad-core Cortex A53 @ 1.8 GHz, in both 32 and 64-bit modes

Without map With map OpenMP w/o map OpenMP w/ map
32-bit Generic 1.4 Mpix/s 1.4 Mpix/s 5.6 Mpix/s 5.5 Mpix/s
64-bit Generic 1.3 Mpix/s 1.3 Mpix/s 9.0 Mpix/s 8.8 Mpix/s
32-bit Neon 3.1 Mpix/s 1.3 Mpix/s 11.5 Mpix/s 11.3 Mpix/s
64-bit ASIMD 6.5 Mpix/s 6.4 Mpix/s 30.7 Mpix/s 29.9 Mpix/s

Comparison

How does this compare to other versions available out there? I have only tested two of them:

  • An OpenCV-based one, written by Mehdi Rabah, that's listed on SSIM's official page. It is based on the reference MATLAB code from Zhou Wang. It runs at around 22 Mpix/s, which is quite good, especially considering how small is the code. Note that due to the code being quite old I could only test it with Open CV 3.4.8, more recent versions might fare better.
  • The one from Basis. This one is a full C++ re-implementation that has no outside dependency. It does not use SIMD nor multi-threading and runs at around 1.5 Mpix/s.

There are several other SSIM implementations you can find on GitHub. Most of them are similar to Mehdi Rabah's and rely on OpenCV, they should therefore have comparable performance figures.

Accuracy

The API only deals with float, because 6 digits is enough precision for SSIM values. However, internal computations are done with more precision. The default is to perform all per-pixel intermediate computations in single precision and only the sum for the global SSIM is done in double precision.

Using the RMGR_SSIM_USE_DOUBLE option, you can force all computations to be carried out in double precision. But be aware that this will basically make the library run half as fast.

The following table gives the maximum errors of global and per-pixel SSIMs depending on whether the library was built with single or double internal precision. Those errors are relative to a fully quad-precision (128-bit) reference implementation. As you can see, the double precision is almost as precise as the float API allows. The single precision version should nonetheless be precise enough for most applications.

Average
Global Error
Maximum
Global Error
Average
Per-Pixel Error
Maximum
Per-Pixel Error
Single Precision 1.75×10⁻⁷ 1.49×10⁻⁶ 5.61×10⁻⁶ 6.22×10⁻⁴
Double Precision 1.31×10⁻⁷ 4.75×10⁻⁷ 7.35×10⁻⁸ 9.21×10⁻⁶

Multi-Channel Images

SSIM is only defined for grayscale images and this is all that the library supports. A common way of handling color images is to convert them to luminance. You may also compute the SSIM of each channel and somehow merge them into a composite SSIM (taking the smallest value for example, or making a weighted sum). You can even take this a step further using the SSIM map: compute per-pixel composite SSIMs and then compute a global composite SSIM (the global SSIM is simply the average of per-pixel SSIMs).

For alpha channel, just as with color, you're on your own. Try to figure out some way of computing a composite SSIM from per-channel SSIMs.

Bit Depth Support

Only 8-bit images are supported. However, it shouldn't be complicated to modify the code to support other bit depths: all that needs to be changed are the L constant and the retrieve_tile() function.

Building

The library relies on CMake to build. SIMD intrinsics will be used whenever possible and OpenMP can be enabled by turning on a simple option (RMGR_SSIM_USE_OPENMP).

As mentioned above, for improved precision, you can have the library perform all the computations using double rather than float by setting the RMGR_SSIM_USE_DOUBLE option to ON.

Otherwise, the code is mostly self-configurable and should therefore be easy to port to another build system.

Debian/Ubuntu

Here's how to build the library:

sudo apt install build-essential g++ cmake
make rmgr-ssim
ls -l lib/linux-*gcc*/release/

Windows

You need Visual Studio and CMake.
Run the GenSolution.cmd script and follow the instructions.

Command Line Tool

A simple command line tool that computes the SSIM of two images is available. To build it, set the CMake variable RMGR_SSIM_BUILD_CLI to ON.

Debian/Ubuntu

Here's how to build and run the command-line tool:

sudo apt install build-essential g++ cmake
make rmgr-ssim-cli
bin/linux-*gcc*/release/rmgr-ssim

Windows

You need Visual Studio and CMake.
Run the GenSolution.cmd script and follow the instructions.

Sample Code

sample/rmgr-ssim-sample.cpp is a simplified version of the aforementioned command line tool that showcases how to call rmgr::ssim::compute_ssim() in the most straightforward manner. For sample code of more advanced uses, please refer to the unit tests.

Debian/Ubuntu

Here's how to build the command-line tool:

sudo apt install build-essential g++ cmake
make rmgr-ssim-sample
bin/linux-*gcc*/release/rmgr-ssim-sample

Windows

You need Visual Studio and CMake.
Run the GenSolution.cmd script and follow the instructions.

Documentation

You can generate the documentation by running doxygen in the root folder, the HTML documentation will end up in the doc folder.

Licensing

This library is licensed under the zlib license, as follows:

Copyright © 2023, Romain Bailly

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

ssim's People

Contributors

aous72 avatar romigrou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ssim's Issues

Support for webp?

Thanks for sharing this project, I'm thinking of adding support for webp, from the closed issues it seems to me that stb has no intention of supporting it, see Will you add more image types to stb_image.h?
This project interests me mainly to evaluate if and how to convert a quantity of images into webp. Before trying to add support I wanted to ask if you already plan to support it

Add command line executable

Thank you for open source this powerful lib!

It would be great if there's a command line executable available so people like me (who have not much C experience) can easily try and integrate the tool into our pipeline.

`RMGR_SIMD_USE_OPENMP` - Possible Typo

The Readme mentions option RMGR_SIMD_USE_OPENMP, but the code seems to be using option RMGR_SSIM_USE_OPENMP to enable openmp.

I'll send a PR soon, please merge if it's indeed a typo. Thanks.

I want to build ARM64 version to use it on android, however build failed, how to fix this?

D:\Users\80362386\Desktop\ssim-master>GenerateSolution.cmd
Which version of Visual Studio do you want to generate a solution for?
1- Visual Studio 12 (2013)
2- Visual Studio 14 (2015)
3- Visual Studio 15 (2017)
4- Visual Studio 16 (2019)
5- Visual Studio 17 (2022)
5

Which platform to you want to generate for?
1- x86
2- AMD64
3- x86 (Clang)
4- AMD64 (Clang)
5- ARM
6- ARM64
6

Calling CMake as follows:
"cmake" -G "Visual Studio 17 2022" -DRMGR_SSIM_BUILD_CLI=ON -DRMGR_SSIM_BUILD_SAMPLE=ON -DRMGR_SSIM_BUILD_TESTS=ON -A ARM64 "-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY:PATH=D:\Users\80362386\Desktop\ssim-master\lib\windows-aarch64-msvc19.3-vs2022" "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY:PATH=D:\Users\80362386\Desktop\ssim-master\bin\windows-aarch64-msvc19.3-vs2022" "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY:PATH=D:\Users\80362386\Desktop\ssim-master\bin\windows-aarch64-msvc19.3-vs2022" "-DCMAKE_INSTALL_PREFIX:PATH=D:\Users\80362386\Desktop\ssim-master\package\windows-aarch64-msvc19.3" "-DCPACK_PACKAGE_DIRECTORY:PATH=D:\Users\80362386\Desktop\ssim-master\package" "-DCPACK_SYSTEM_NAME:STRING=windows-aarch64-msvc19.3" "D:\Users\80362386\Desktop\ssim-master"

CMake Error: Error: generator platform: ARM64
Does not match the platform used previously:
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
请按任意键继续. . .

D:\Users\80362386\Desktop\ssim-master>GenerateSolution.cmd
Which version of Visual Studio do you want to generate a solution for?
1- Visual Studio 12 (2013)
2- Visual Studio 14 (2015)
3- Visual Studio 15 (2017)
4- Visual Studio 16 (2019)
5- Visual Studio 17 (2022)
5

Which platform to you want to generate for?
1- x86
2- AMD64
3- x86 (Clang)
4- AMD64 (Clang)
5- ARM
6- ARM64
6

Calling CMake as follows:
"cmake" -G "Visual Studio 17 2022" -DRMGR_SSIM_BUILD_CLI=ON -DRMGR_SSIM_BUILD_SAMPLE=ON -DRMGR_SSIM_BUILD_TESTS=ON -A ARM64 "-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY:PATH=D:\Users\80362386\Desktop\ssim-master\lib\windows-aarch64-msvc19.3-vs2022" "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY:PATH=D:\Users\80362386\Desktop\ssim-master\bin\windows-aarch64-msvc19.3-vs2022" "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY:PATH=D:\Users\80362386\Desktop\ssim-master\bin\windows-aarch64-msvc19.3-vs2022" "-DCMAKE_INSTALL_PREFIX:PATH=D:\Users\80362386\Desktop\ssim-master\package\windows-aarch64-msvc19.3" "-DCPACK_PACKAGE_DIRECTORY:PATH=D:\Users\80362386\Desktop\ssim-master\package" "-DCPACK_SYSTEM_NAME:STRING=windows-aarch64-msvc19.3" "D:\Users\80362386\Desktop\ssim-master"

CMake Error at CMakeLists.txt:26 (project):
Failed to run MSBuild command:

C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/MSBuild/Current/Bin/amd64/MSBuild.exe

to get the value of VCTargetsPath:

适用于 .NET Framework MSBuild 版本 17.7.2+d6990bcfa
生成启动时间为 2024/5/21 19:12:07。

节点 1 上的项目“D:\Users\80362386\Desktop\ssim-master\build\windows-aarch64-msvc19.3-vs2022\CMakeFiles\3.26.4-msvc4\VCTargetsPath.vcxproj”(默认目标)。
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(832,5): error : The BaseOutputPath/OutputPath property is not set for project 'VCTargetsPath.vcxproj'.  Please check to make sure that you have specified a valid combination of Configuration and Platform for this project.  Configuration='Debug'  Platform='ARM64'.  You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Configuration or Platform that doesn't exist for this project. [D:\Users\80362386\Desktop\ssim-master\build\windows-aarch64-msvc19.3-vs2022\CMakeFiles\3.26.4-msvc4\VCTargetsPath.vcxproj]
已完成生成项目“D:\Users\80362386\Desktop\ssim-master\build\windows-aarch64-msvc19.3-vs2022\CMakeFiles\3.26.4-msvc4\VCTargetsPath.vcxproj”(默认目标)的操作 - 失败。

生成失败。

“D:\Users\80362386\Desktop\ssim-master\build\windows-aarch64-msvc19.3-vs2022\CMakeFiles\3.26.4-msvc4\VCTargetsPath.vcxproj”(默认目标) (1) ->
(_CheckForInvalidConfigurationAndPlatform 目标) ->
  C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(832,5): error : The BaseOutputPath/OutputPath property is not set for project 'VCTargetsPath.vcxproj'.  Please check to make sure that you have specified a valid combination of Configuration and Platform for this project.  Configuration='Debug'  Platform='ARM64'.  You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Configuration or Platform that doesn't exist for this project. [D:\Users\80362386\Desktop\ssim-master\build\windows-aarch64-msvc19.3-vs2022\CMakeFiles\3.26.4-msvc4\VCTargetsPath.vcxproj]

    0 个警告
    1 个错误

已用时间 00:00:00.11

Exit code: 1

-- Configuring incomplete, errors occurred!

Exemple

Bonjour,

Serait-il possible d'ajouter un exemple d'utilisation (comparaison de 2 JPEG) en C car je tourne un peu en rond pour l'utilisation
Merci

Support pour newbies

Bonjour,
Je débute en linux et j'ai du mal à utiliser le programme.

Pas à pas, voici mes démarches:

  1. git clone https://github.com/romigrou/ssim.git
  2. cd ssim
  3. vim CMakeLists.txt pour mettre sur RMGR_SSIM_NO_OPENMP sur ON, RMGR_SSIM_BUILD_CLI sur ON, RMGR_SSIM_BUILD_SAMPLE sur ON et RMGR_SSIM_BUILD_TESTS sur on
  4. cmake CMakeLists.txt
  5. make Makefile

A partir de là, je suis bloqué.

Je ne sais pas, en ligne de commande, comment utiliser le programme afin d'obtenir le SSIM entre 2 images.

Merci pour l'aide

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.