forked from andrewlewis/camera-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_characteristic.py
executable file
·126 lines (107 loc) · 4.81 KB
/
make_characteristic.py
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
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
import cPickle
import glob
import math
import numpy
import sys
# http://www.pybytes.com/pywavelets/ref/
import pywt
# http://www.pythonware.com/library/pil/handbook/index.htm
from PIL import Image, ImageOps
# Accelerator module for filtering.
from accel.filter_accel import wiener_filter
TILE_OVERLAP = 8
TILE_SIZE = 512
DENOISE_SIGMA = 5
def denoise_coefficient_list(coefficient_list, sigma):
ll = coefficient_list[0]
denoised_bands = [ll]
for band, subband_coefficients in enumerate(coefficient_list[1 :]):
denoised_bands.append([wiener_filter(s.astype(numpy.float), sigma)
for s in subband_coefficients])
return denoised_bands
def get_noise(greyscale_matrix):
original_shape = greyscale_matrix.shape
# The image will be transformed in TILE_SIZE * TILE_SIZE tiles with overlap
# TILE_OVERLAP on each side.
tiles_count = [(d - TILE_OVERLAP) / (TILE_SIZE - TILE_OVERLAP)
for d in original_shape]
tiled_shape = [TILE_OVERLAP + c * (TILE_SIZE - TILE_OVERLAP)
for c in tiles_count]
without_edges_shape = [c * (TILE_SIZE - TILE_OVERLAP) - TILE_OVERLAP
for c in tiles_count]
# The greyscale image is represented as a matrix of float values.
greyscale_matrix = greyscale_matrix.astype(float)
result_matrix = numpy.zeros(tiled_shape, dtype = numpy.float)
# Work out how many levels of wavelet decomposition we will do.
dyad_length = math.ceil(math.log(TILE_SIZE, 2))
ll_levels = 5
wavelet_levels = dyad_length - ll_levels
ll_size = 2 ** ll_levels
# Make a window for the tile edges.
tile_window = numpy.zeros((TILE_SIZE, TILE_SIZE), dtype=numpy.float)
tile_window[TILE_OVERLAP / 2 :
-(TILE_OVERLAP / 2),
TILE_OVERLAP / 2 :
-(TILE_OVERLAP / 2)] = 1.0
# Transform and filter each non-overlapping TILE_SIZE * TILE_SIZE square of
# the image separately.
for ty in range(0, tiles_count[1]):
for tx in range(0, tiles_count[0]):
print (tx, ty)
transform_input = greyscale_matrix[
tx * (TILE_SIZE - TILE_OVERLAP) :
tx * (TILE_SIZE - TILE_OVERLAP) + TILE_SIZE,
ty * (TILE_SIZE - TILE_OVERLAP) :
ty * (TILE_SIZE - TILE_OVERLAP) + TILE_SIZE]
coefficient_list = pywt.wavedec2(transform_input,
'db8',
level = int(wavelet_levels),
mode = 'per')
coefficient_list = denoise_coefficient_list(coefficient_list,
DENOISE_SIGMA)
denoised_tile = pywt.waverec2(coefficient_list,
'db8',
mode = 'per')
denoised_tile[denoised_tile > 255.0] = 255.0
denoised_tile[denoised_tile < 0.0] = 0.0
result_matrix[tx * (TILE_SIZE - TILE_OVERLAP) :
tx * (TILE_SIZE - TILE_OVERLAP) + TILE_SIZE,
ty * (TILE_SIZE - TILE_OVERLAP) :
ty * (TILE_SIZE - TILE_OVERLAP) + TILE_SIZE] += \
(denoised_tile * tile_window)
# Remove the edges.
result_matrix = result_matrix[TILE_OVERLAP : -TILE_OVERLAP,
TILE_OVERLAP : -TILE_OVERLAP]
# Subtract the denoised image from the original to get an estimate of the
# noise.
original = greyscale_matrix[TILE_OVERLAP : tiled_shape[0] - TILE_OVERLAP,
TILE_OVERLAP : tiled_shape[1] - TILE_OVERLAP]
return (result_matrix, original - result_matrix)
def get_noise_from_file(file_name):
original = Image.open(file_name)
greyscale = ImageOps.grayscale(original)
greyscale_vector = numpy.fromstring(greyscale.tostring(), dtype=numpy.uint8)
greyscale_matrix = numpy.reshape(greyscale_vector,
(original.size[1], original.size[0]))
return get_noise(greyscale_matrix)
# Command line utility for creating the characteristic.
if __name__ == '__main__':
if len(sys.argv) != 3:
print "Usage:\n\t%s path_with_png_files output_file_name" % (sys.argv[0],)
sys.exit(0)
# Get a list of images to process.
file_list = glob.glob(sys.argv[1] + '/*.png')
print "Processing %d images" % (len(file_list),)
# Denoise and build the numerator/denominator.
numerator = None
denominator = None
for i, f in enumerate(file_list):
print "Processing %03d %s" % (i, f,)
(denoised_matrix, residual_matrix) = get_noise_from_file(f)
if numerator is None:
numerator = numpy.zeros_like(residual_matrix)
denominator = numpy.zeros_like(residual_matrix)
numerator += denoised_matrix * residual_matrix
denominator += denoised_matrix * denoised_matrix
numpy.savetxt(sys.argv[2], numerator / denominator)