-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
117 lines (91 loc) · 3.46 KB
/
testing.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
#!/usr/bin/env python
# Author: Igor Topcin <topcin@ime.usp.br>
# PTC5892 Processamento de Imagens Medicas
# POLI - University of Sao Paulo
# This is the test package for this project
# In order do run tests, go into tests directory and run "nosetests".
import numpy as np
from PIL import Image
import kuan as kuan
import matplotlib.pyplot as plt
from nose.tools import *
from canny import canny
from prat import fom
from scipy.ndimage import distance_transform_edt
def filter(im_orig_array, im_gold_array, window_size, cu, test_index = 1):
"""
This function encapsulates all the calls that are necessary in order to
test kuan filter in a noisy image.
"""
# plot original image
plt.figure(test_index * 10 + 1)
plt.subplot(131)
plt.imshow(im_orig_array, cmap=plt.cm.gray)
plt.axis('off')
plt.title('noisy')
# apply kuan filter using the computed coefficient of variation of a smooth
im_filtered_array = kuan.filter(im_orig_array, window_size, cu)
plt.figure(test_index * 10 + 1)
plt.subplot(132)
plt.imshow(im_filtered_array, cmap=plt.cm.gray)
plt.axis('off')
plt.title('filtered, Cu=%s' % cu)
# plot the gold standard image
plt.figure(test_index * 10 + 1)
plt.subplot(133)
plt.imshow(im_gold_array, cmap=plt.cm.gray)
plt.axis('off')
plt.title('gold standard')
# detect the edges of the filtered image
im_filtered_edges = canny(im_filtered_array, 0.1, 20, 50)
plt.figure(test_index * 10 + 2)
plt.subplot(131)
plt.imshow(im_filtered_edges, cmap=plt.cm.gray)
plt.axis('off')
plt.title('filtered edges')
# detect the edges of the gold standard using the same edge detector
im_gold_edges = canny(im_gold_array, 0.1, 20, 50)
plt.figure(test_index * 10 + 2)
plt.subplot(132)
plt.imshow(im_gold_edges, cmap=plt.cm.gray)
plt.axis('off')
plt.title('gold std edges')
# Plot the distance transform for the edges in the gold standard image
im_gold_dist = distance_transform_edt(np.invert(im_gold_edges))
f = fom(im_filtered_array, im_gold_array)
plt.figure(test_index * 10 + 2)
plt.subplot(133)
plt.imshow(im_gold_dist)
plt.axis('off')
plt.title('dist transf gold,fom=%s' % f)
plt.show()
def test_image1():
# read the image with speckle
im_orig = Image.open('imgs/fig_geom_speckled.tif').convert('L')
im_orig_array = np.array(im_orig)
# calculate the coefficient of variation of a smooth region of the image.
cu = kuan.variation(im_orig_array[50:100,300:350])
# read the gold standard image
im_gold = Image.open('imgs/fig_geom_goldStd.tif').convert('L')
im_gold_array = np.array(im_gold)
filter(im_orig_array, im_gold_array, 11, cu, 1)
def test_image2():
# read the original image, with Gaussian noise
im_orig = Image.open('imgs/cistosRuidoGaussian10dB.jpg').convert('L')
im_orig_array = np.array(im_orig)
# calculate the coefficient of variation of a smooth region of the image.
cu = kuan.variation(im_orig_array[50:100,50:100])
# read the gold standard image
im_gold = Image.open('imgs/cistosGoldStd.tif').convert('L')
im_gold_array = np.array(im_gold)
filter(im_orig_array, im_gold_array, 9, cu, 2)
def test_image3():
# read the original image, with Gaussian noise
im_orig = Image.open('imgs/cistosSpeckle.tif').convert('L')
im_orig_array = np.array(im_orig)
# calculate the coefficient of variation of a smooth region of the image.
cu = kuan.variation(im_orig_array[50:100,50:100])
# read the gold standard image
im_gold = Image.open('imgs/cistosGoldStd.tif').convert('L')
im_gold_array = np.array(im_gold)
filter(im_orig_array, im_gold_array, 13, cu, 3)