commit
82c477c9f7
71 changed files with 2652 additions and 1386 deletions
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 630 B |
After Width: | Height: | Size: 42 KiB |
@ -0,0 +1,112 @@ |
|||||||
|
Out-of-focus Deblur Filter {#tutorial_out_of_focus_deblur_filter} |
||||||
|
========================== |
||||||
|
|
||||||
|
Goal |
||||||
|
---- |
||||||
|
|
||||||
|
In this tutorial you will learn: |
||||||
|
|
||||||
|
- what is a degradation image model |
||||||
|
- what is PSF of out-of-focus image |
||||||
|
- how to restore a blurred image |
||||||
|
- what is Wiener filter |
||||||
|
|
||||||
|
Theory |
||||||
|
------ |
||||||
|
|
||||||
|
@note The explanation is based on the books @cite gonzalez and @cite gruzman. Also, you can refer to Matlab's tutorial [Image Deblurring in Matlab] and an article [SmartDeblur]. |
||||||
|
@note An out-of-focus image on this page is a real world image. An out-of-focus was done manually by camera optics. |
||||||
|
|
||||||
|
### What is a degradation image model? |
||||||
|
|
||||||
|
A mathematical model of the image degradation in frequency domain representation is: |
||||||
|
|
||||||
|
\f[S = H\cdot U + N\f] |
||||||
|
|
||||||
|
where |
||||||
|
\f$S\f$ is a spectrum of blurred (degraded) image, |
||||||
|
\f$U\f$ is a spectrum of original true (undegraded) image, |
||||||
|
\f$H\f$ is frequency response of point spread function (PSF), |
||||||
|
\f$N\f$ is a spectrum of additive noise. |
||||||
|
|
||||||
|
Circular PSF is a good approximation of out-of-focus distortion. Such PSF is specified by only one parameter - radius \f$R\f$. Circular PSF is used in this work. |
||||||
|
|
||||||
|
 |
||||||
|
|
||||||
|
### How to restore an blurred image? |
||||||
|
|
||||||
|
The objective of restoration (deblurring) is to obtain an estimate of the original image. Restoration formula in frequency domain is: |
||||||
|
|
||||||
|
\f[U' = H_w\cdot S\f] |
||||||
|
|
||||||
|
where |
||||||
|
\f$U'\f$ is spectrum of estimation of original image \f$U\f$, |
||||||
|
\f$H_w\f$ is restoration filter, for example, Wiener filter. |
||||||
|
|
||||||
|
### What is Wiener filter? |
||||||
|
|
||||||
|
Wiener filter is a way to restore a blurred image. Let's suppose that PSF is a real and symmetric signal, a power spectrum of the original true image and noise are not known, |
||||||
|
then simplified Wiener formula is: |
||||||
|
|
||||||
|
\f[H_w = \frac{H}{|H|^2+\frac{1}{SNR}} \f] |
||||||
|
|
||||||
|
where |
||||||
|
\f$SNR\f$ is signal-to-noise ratio. |
||||||
|
|
||||||
|
So, in order to recover an out-of-focus image by Wiener filter, it needs to know \f$SNR\f$ and \f$R\f$ of circular PSF. |
||||||
|
|
||||||
|
|
||||||
|
Source code |
||||||
|
----------- |
||||||
|
|
||||||
|
You can find source code in the `samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp` of the OpenCV source code library. |
||||||
|
|
||||||
|
@include cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp |
||||||
|
|
||||||
|
Explanation |
||||||
|
----------- |
||||||
|
|
||||||
|
An out-of-focus image recovering algorithm consists of PSF generation, Wiener filter generation and filtering an blurred image in frequency domain: |
||||||
|
@snippet samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp main |
||||||
|
|
||||||
|
A function calcPSF() forms an circular PSF according to input parameter radius \f$R\f$: |
||||||
|
@snippet samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp calcPSF |
||||||
|
|
||||||
|
A function calcWnrFilter() synthesizes simplified Wiener filter \f$H_w\f$ according to formula described above: |
||||||
|
@snippet samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp calcWnrFilter |
||||||
|
|
||||||
|
A function fftshift() rearranges PSF. This code was just copied from tutorial @ref tutorial_discrete_fourier_transform "Discrete Fourier Transform": |
||||||
|
@snippet samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp fftshift |
||||||
|
|
||||||
|
A function filter2DFreq() filters an blurred image in frequency domain: |
||||||
|
@snippet samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp filter2DFreq |
||||||
|
|
||||||
|
Result |
||||||
|
------ |
||||||
|
|
||||||
|
Below you can see real out-of-focus image: |
||||||
|
 |
||||||
|
|
||||||
|
|
||||||
|
Below result was done by \f$R\f$ = 53 and \f$SNR\f$ = 5200 parameters: |
||||||
|
 |
||||||
|
|
||||||
|
The Wiener filter was used, values of \f$R\f$ and \f$SNR\f$ were selected manually to give the best possible visual result. |
||||||
|
We can see that the result is not perfect, but it gives us a hint to the image content. With some difficulty, the text is readable. |
||||||
|
|
||||||
|
@note The parameter \f$R\f$ is the most important. So you should adjust \f$R\f$ first, then \f$SNR\f$. |
||||||
|
@note Sometimes you can observe the ringing effect in an restored image. This effect can be reduced by several methods. For example, you can taper input image edges. |
||||||
|
|
||||||
|
You can also find a quick video demonstration of this on |
||||||
|
[YouTube](https://youtu.be/0bEcE4B0XP4). |
||||||
|
@youtube{0bEcE4B0XP4} |
||||||
|
|
||||||
|
References |
||||||
|
------ |
||||||
|
- [Image Deblurring in Matlab] - Image Deblurring in Matlab |
||||||
|
- [SmartDeblur] - SmartDeblur site |
||||||
|
|
||||||
|
<!-- invisible references list --> |
||||||
|
[Digital Image Processing]: http://web.ipac.caltech.edu/staff/fmasci/home/astro_refs/Digital_Image_Processing_2ndEd.pdf |
||||||
|
[Image Deblurring in Matlab]: https://www.mathworks.com/help/images/image-deblurring.html |
||||||
|
[SmartDeblur]: http://yuzhikov.com/articles/BlurredImagesRestoration1.htm |
@ -0,0 +1,5 @@ |
|||||||
|
// This file is part of OpenCV project.
|
||||||
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||||
|
// of this distribution and at http://opencv.org/license.html.
|
||||||
|
#include "test_precomp.hpp" |
||||||
|
#include "test_intrin.simd.hpp" |
@ -0,0 +1,296 @@ |
|||||||
|
// This file is part of OpenCV project.
|
||||||
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||||
|
// of this distribution and at http://opencv.org/license.html.
|
||||||
|
#include "test_precomp.hpp" |
||||||
|
#include "test_intrin_utils.hpp" |
||||||
|
|
||||||
|
namespace opencv_test { namespace hal { |
||||||
|
CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN |
||||||
|
|
||||||
|
void test_hal_intrin_uint8(); |
||||||
|
void test_hal_intrin_int8(); |
||||||
|
void test_hal_intrin_uint16(); |
||||||
|
void test_hal_intrin_int16(); |
||||||
|
void test_hal_intrin_uint32(); |
||||||
|
void test_hal_intrin_int32(); |
||||||
|
void test_hal_intrin_uint64(); |
||||||
|
void test_hal_intrin_int64(); |
||||||
|
void test_hal_intrin_float32(); |
||||||
|
void test_hal_intrin_float64(); |
||||||
|
|
||||||
|
#ifndef CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY |
||||||
|
|
||||||
|
//============= 8-bit integer =====================================================================
|
||||||
|
|
||||||
|
void test_hal_intrin_uint8() |
||||||
|
{ |
||||||
|
TheTest<v_uint8>() |
||||||
|
.test_loadstore() |
||||||
|
.test_interleave() |
||||||
|
.test_expand() |
||||||
|
.test_expand_q() |
||||||
|
.test_addsub() |
||||||
|
.test_addsub_wrap() |
||||||
|
.test_cmp() |
||||||
|
.test_logic() |
||||||
|
.test_min_max() |
||||||
|
.test_absdiff() |
||||||
|
.test_mask() |
||||||
|
.test_popcount() |
||||||
|
.test_pack<1>().test_pack<2>().test_pack<3>().test_pack<8>() |
||||||
|
.test_pack_u<1>().test_pack_u<2>().test_pack_u<3>().test_pack_u<8>() |
||||||
|
.test_unpack() |
||||||
|
.test_extract<0>().test_extract<1>().test_extract<8>().test_extract<15>() |
||||||
|
.test_rotate<0>().test_rotate<1>().test_rotate<8>().test_rotate<15>() |
||||||
|
; |
||||||
|
|
||||||
|
#if CV_SIMD256 |
||||||
|
TheTest<v_uint8>() |
||||||
|
.test_pack<9>().test_pack<10>().test_pack<13>().test_pack<15>() |
||||||
|
.test_pack_u<9>().test_pack_u<10>().test_pack_u<13>().test_pack_u<15>() |
||||||
|
.test_extract<16>().test_extract<17>().test_extract<23>().test_extract<31>() |
||||||
|
.test_rotate<16>().test_rotate<17>().test_rotate<23>().test_rotate<31>() |
||||||
|
; |
||||||
|
#endif |
||||||
|
} |
||||||
|
|
||||||
|
void test_hal_intrin_int8() |
||||||
|
{ |
||||||
|
TheTest<v_int8>() |
||||||
|
.test_loadstore() |
||||||
|
.test_interleave() |
||||||
|
.test_expand() |
||||||
|
.test_expand_q() |
||||||
|
.test_addsub() |
||||||
|
.test_addsub_wrap() |
||||||
|
.test_cmp() |
||||||
|
.test_logic() |
||||||
|
.test_min_max() |
||||||
|
.test_absdiff() |
||||||
|
.test_abs() |
||||||
|
.test_mask() |
||||||
|
.test_popcount() |
||||||
|
.test_pack<1>().test_pack<2>().test_pack<3>().test_pack<8>() |
||||||
|
.test_unpack() |
||||||
|
.test_extract<0>().test_extract<1>().test_extract<8>().test_extract<15>() |
||||||
|
.test_rotate<0>().test_rotate<1>().test_rotate<8>().test_rotate<15>() |
||||||
|
; |
||||||
|
} |
||||||
|
|
||||||
|
//============= 16-bit integer =====================================================================
|
||||||
|
|
||||||
|
void test_hal_intrin_uint16() |
||||||
|
{ |
||||||
|
TheTest<v_uint16>() |
||||||
|
.test_loadstore() |
||||||
|
.test_interleave() |
||||||
|
.test_expand() |
||||||
|
.test_addsub() |
||||||
|
.test_addsub_wrap() |
||||||
|
.test_mul() |
||||||
|
.test_mul_expand() |
||||||
|
.test_cmp() |
||||||
|
.test_shift<1>() |
||||||
|
.test_shift<8>() |
||||||
|
.test_logic() |
||||||
|
.test_min_max() |
||||||
|
.test_absdiff() |
||||||
|
.test_reduce() |
||||||
|
.test_mask() |
||||||
|
.test_popcount() |
||||||
|
.test_pack<1>().test_pack<2>().test_pack<7>().test_pack<16>() |
||||||
|
.test_pack_u<1>().test_pack_u<2>().test_pack_u<7>().test_pack_u<16>() |
||||||
|
.test_unpack() |
||||||
|
.test_extract<0>().test_extract<1>().test_extract<4>().test_extract<7>() |
||||||
|
.test_rotate<0>().test_rotate<1>().test_rotate<4>().test_rotate<7>() |
||||||
|
; |
||||||
|
} |
||||||
|
|
||||||
|
void test_hal_intrin_int16() |
||||||
|
{ |
||||||
|
TheTest<v_int16>() |
||||||
|
.test_loadstore() |
||||||
|
.test_interleave() |
||||||
|
.test_expand() |
||||||
|
.test_addsub() |
||||||
|
.test_addsub_wrap() |
||||||
|
.test_mul() |
||||||
|
.test_mul_expand() |
||||||
|
.test_cmp() |
||||||
|
.test_shift<1>() |
||||||
|
.test_shift<8>() |
||||||
|
.test_dot_prod() |
||||||
|
.test_logic() |
||||||
|
.test_min_max() |
||||||
|
.test_absdiff() |
||||||
|
.test_abs() |
||||||
|
.test_reduce() |
||||||
|
.test_mask() |
||||||
|
.test_popcount() |
||||||
|
.test_pack<1>().test_pack<2>().test_pack<7>().test_pack<16>() |
||||||
|
.test_unpack() |
||||||
|
.test_extract<0>().test_extract<1>().test_extract<4>().test_extract<7>() |
||||||
|
.test_rotate<0>().test_rotate<1>().test_rotate<4>().test_rotate<7>() |
||||||
|
; |
||||||
|
} |
||||||
|
|
||||||
|
//============= 32-bit integer =====================================================================
|
||||||
|
|
||||||
|
void test_hal_intrin_uint32() |
||||||
|
{ |
||||||
|
TheTest<v_uint32>() |
||||||
|
.test_loadstore() |
||||||
|
.test_interleave() |
||||||
|
.test_expand() |
||||||
|
.test_addsub() |
||||||
|
.test_mul() |
||||||
|
.test_mul_expand() |
||||||
|
.test_cmp() |
||||||
|
.test_shift<1>() |
||||||
|
.test_shift<8>() |
||||||
|
.test_logic() |
||||||
|
.test_min_max() |
||||||
|
.test_absdiff() |
||||||
|
.test_reduce() |
||||||
|
.test_mask() |
||||||
|
.test_popcount() |
||||||
|
.test_pack<1>().test_pack<2>().test_pack<15>().test_pack<32>() |
||||||
|
.test_unpack() |
||||||
|
.test_extract<0>().test_extract<1>().test_extract<2>().test_extract<3>() |
||||||
|
.test_rotate<0>().test_rotate<1>().test_rotate<2>().test_rotate<3>() |
||||||
|
.test_transpose() |
||||||
|
; |
||||||
|
} |
||||||
|
|
||||||
|
void test_hal_intrin_int32() |
||||||
|
{ |
||||||
|
TheTest<v_int32>() |
||||||
|
.test_loadstore() |
||||||
|
.test_interleave() |
||||||
|
.test_expand() |
||||||
|
.test_addsub() |
||||||
|
.test_mul() |
||||||
|
.test_abs() |
||||||
|
.test_cmp() |
||||||
|
.test_popcount() |
||||||
|
.test_shift<1>().test_shift<8>() |
||||||
|
.test_logic() |
||||||
|
.test_min_max() |
||||||
|
.test_absdiff() |
||||||
|
.test_reduce() |
||||||
|
.test_mask() |
||||||
|
.test_pack<1>().test_pack<2>().test_pack<15>().test_pack<32>() |
||||||
|
.test_unpack() |
||||||
|
.test_extract<0>().test_extract<1>().test_extract<2>().test_extract<3>() |
||||||
|
.test_rotate<0>().test_rotate<1>().test_rotate<2>().test_rotate<3>() |
||||||
|
.test_float_cvt32() |
||||||
|
.test_float_cvt64() |
||||||
|
.test_transpose() |
||||||
|
; |
||||||
|
} |
||||||
|
|
||||||
|
//============= 64-bit integer =====================================================================
|
||||||
|
|
||||||
|
void test_hal_intrin_uint64() |
||||||
|
{ |
||||||
|
TheTest<v_uint64>() |
||||||
|
.test_loadstore() |
||||||
|
.test_addsub() |
||||||
|
.test_shift<1>().test_shift<8>() |
||||||
|
.test_logic() |
||||||
|
.test_extract<0>().test_extract<1>() |
||||||
|
.test_rotate<0>().test_rotate<1>() |
||||||
|
; |
||||||
|
} |
||||||
|
|
||||||
|
void test_hal_intrin_int64() |
||||||
|
{ |
||||||
|
TheTest<v_int64>() |
||||||
|
.test_loadstore() |
||||||
|
.test_addsub() |
||||||
|
.test_shift<1>().test_shift<8>() |
||||||
|
.test_logic() |
||||||
|
.test_extract<0>().test_extract<1>() |
||||||
|
.test_rotate<0>().test_rotate<1>() |
||||||
|
; |
||||||
|
} |
||||||
|
|
||||||
|
//============= Floating point =====================================================================
|
||||||
|
void test_hal_intrin_float32() |
||||||
|
{ |
||||||
|
TheTest<v_float32>() |
||||||
|
.test_loadstore() |
||||||
|
.test_interleave() |
||||||
|
.test_interleave_2channel() |
||||||
|
.test_addsub() |
||||||
|
.test_mul() |
||||||
|
.test_div() |
||||||
|
.test_cmp() |
||||||
|
.test_sqrt_abs() |
||||||
|
.test_min_max() |
||||||
|
.test_float_absdiff() |
||||||
|
.test_reduce() |
||||||
|
.test_mask() |
||||||
|
.test_unpack() |
||||||
|
.test_float_math() |
||||||
|
.test_float_cvt64() |
||||||
|
.test_matmul() |
||||||
|
.test_transpose() |
||||||
|
.test_reduce_sum4() |
||||||
|
.test_extract<0>().test_extract<1>().test_extract<2>().test_extract<3>() |
||||||
|
.test_rotate<0>().test_rotate<1>().test_rotate<2>().test_rotate<3>() |
||||||
|
; |
||||||
|
|
||||||
|
#if CV_SIMD256 |
||||||
|
TheTest<v_float32>() |
||||||
|
.test_extract<4>().test_extract<5>().test_extract<6>().test_extract<7>() |
||||||
|
.test_rotate<4>().test_rotate<5>().test_rotate<6>().test_rotate<7>() |
||||||
|
; |
||||||
|
#endif |
||||||
|
} |
||||||
|
|
||||||
|
void test_hal_intrin_float64() |
||||||
|
{ |
||||||
|
#if CV_SIMD_64F |
||||||
|
TheTest<v_float64>() |
||||||
|
.test_loadstore() |
||||||
|
.test_addsub() |
||||||
|
.test_mul() |
||||||
|
.test_div() |
||||||
|
.test_cmp() |
||||||
|
.test_sqrt_abs() |
||||||
|
.test_min_max() |
||||||
|
.test_float_absdiff() |
||||||
|
.test_mask() |
||||||
|
.test_unpack() |
||||||
|
.test_float_math() |
||||||
|
.test_float_cvt32() |
||||||
|
.test_extract<0>().test_extract<1>() |
||||||
|
.test_rotate<0>().test_rotate<1>() |
||||||
|
; |
||||||
|
|
||||||
|
#if CV_SIMD256 |
||||||
|
TheTest<v_float64>() |
||||||
|
.test_extract<2>().test_extract<3>() |
||||||
|
.test_rotate<2>().test_rotate<3>() |
||||||
|
; |
||||||
|
#endif //CV_SIMD256
|
||||||
|
|
||||||
|
#endif |
||||||
|
} |
||||||
|
|
||||||
|
#if CV_FP16 && CV_SIMD_WIDTH > 16 |
||||||
|
void test_hal_intrin_float16() |
||||||
|
{ |
||||||
|
TheTest<v_float16>() |
||||||
|
.test_loadstore_fp16() |
||||||
|
.test_float_cvt_fp16() |
||||||
|
; |
||||||
|
} |
||||||
|
#endif |
||||||
|
|
||||||
|
#endif //CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
|
||||||
|
|
||||||
|
CV_CPU_OPTIMIZATION_NAMESPACE_END |
||||||
|
|
||||||
|
}} //namespace
|
Loading…
Reference in new issue