-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLRHEQ_sequential.cpp
114 lines (92 loc) · 2.89 KB
/
NLRHEQ_sequential.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <boost/gil.hpp>
#include <boost/gil/extension/dynamic_image/any_image.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <boost/mpl/vector.hpp>
#include <iostream>
#include <cmath>
using namespace boost::gil;
struct Pixel
{
int x;
int y;
Pixel(int x, int y, gray8c_view_t *src): x(x), y(y), src(src) {}
gray8c_view_t *src;
const double sigma = 2.5;
const double h = 2.5;
//Member of equation 6
double gaussian_kernel()
{
return 1 / (2*M_PI*sigma*sigma) * std::exp(-((x*x)+(y*y))/(2*sigma*sigma));
}
//Member of equation 6
double weighted_difference(Pixel Y)
{
double sum = 0;
//Integrate over t for each pixel pair (x,y)
for(size_t t_x = 0; t_x < src->width(); t_x++)
{
for(size_t t_y = 0; t_y < src->height(); t_y++)
{
//It was ambiguous whether we should normalize t around the X or the Y variable of integration
//Point t(t_x - X.x, t_y - X.y, src);
Pixel t(t_x - Y.x, t_y - Y.y, src);
double diff = ((*src)(x + t_x, y + t_y) - (*src)(Y.x + t_y, Y.y + t_y));
sum += t.gaussian_kernel() * diff * diff;
}
}
return sum;
}
//Member of equation 6
double normalizing_factor()
{
return 2.5; //placeholder
}
//Implemenation of equation 6
double nonlocal_deblur()
{
double sum = 0;
//Integrate over y for each pixel x
for(int y_x = 0; y_x < src->width(); y_x++)
{
for(int y_y = 0; y_y < src->height(); y_y++)
{
sum += std::exp(-1 * this->weighted_difference({y_x,y_y,src})/(h*h)) * (*src)(y_x, y_y);
}
}
return (1./normalizing_factor()) * sum;
}
}
//Implementation of equation 10
gray8_image_t deblur_itr(const gray8c_view_t& src)
{
gray8_image_t new_img(src.width(),src.height());
//Iterate over the image domain, x, ie preform deblurring at each pixel
for (int x_x=0; x_x<src.width(); x_x++)
{
for (int x_y=1; x_y<src.height(); x_y++)
{
Pixel p(x_x, x_y, &src);
new_img(x_x, x_y) = p.nonlocal_deblur();
}
}
return new_img;
}
//Iteratively deblurs image using equation 10 and a given number of iterations
gray8_image_t deblur_img(gray8c_view_t src, int iterations)
{
for(int i = 0; i < iterations; i++)
{
gray8_image_t new_img = deblur_itr(src);
src = new_img;
}
return src;
}
int main()
{
using my_img_types = boost::mpl::vector<gray8_image_t, gray16_image_t, rgb8_image_t, rgb16_image_t>;
any_image<my_img_types> src;
read_image("Lenna.jpg", src, jpeg_tag());
//here is the new deblurred image after 10 iterations
gray8_image_t deblurred_img = deblur_img(src, 10);
return 0;
}