-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
mutual_information.py
78 lines (56 loc) · 2.37 KB
/
mutual_information.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
from skimage.viewer import ImageViewer
from skimage import data, img_as_float
from skimage.viewer.plugins.plotplugin import PlotPlugin
from skimage.viewer.widgets import Slider
from skimage.exposure import rescale_intensity
from skimage.transform import rotate
import numpy as np
import matplotlib.pyplot as plt
class RotatedImageViewer(ImageViewer):
def __init__(self, image, **kwargs):
super(RotatedImageViewer, self).__init__(image, **kwargs)
slider_kwds = dict(value=0, low=0, high=5, update_on='release',
callback=self.update_angle, value_type='float')
self.slider = Slider('angle', **slider_kwds)
self.layout.addWidget(self.slider)
self.origin_image = image
def update_angle(self, name, angle):
self.image = rotate(self.original_image, angle)
self.histogram.draw(angle=angle)
class Histogram(PlotPlugin):
name = 'Histogram'
def __init__(self, original_viewer, **kwargs):
super(Histogram, self).__init__(height=400, **kwargs)
self.bins = np.linspace(0, 1, 100)
self.mpl_image = None
self.original_viewer = original_viewer
def attach(self, image_viewer):
super(Histogram, self).attach(image_viewer)
self.rotated_viewer = image_viewer
self.ax.set_title('Histogram')
self.ax.set_xlabel('Value in image 1')
self.ax.set_ylabel('Value in image 2')
self.draw(angle=0)
def draw(self, angle=0):
image1 = self.original_viewer.image
image2 = self.rotated_viewer.image
hist, x_edges, y_edges = np.histogram2d(image1.flatten(),
image2.flatten(),
self.bins, normed=True)
hist = np.log(1 + hist)
hist = rescale_intensity(hist, in_range=(0, 3))
if self.mpl_image is None:
self.mpl_image = self.ax.imshow(hist, extent=[0, 1, 0, 1],
cmap=plt.cm.gray)
else:
self.mpl_image.set_data(hist)
self.ax.figure.canvas.draw()
return hist
image = img_as_float(data.camera())
viewer = ImageViewer(image)
rotated_viewer = RotatedImageViewer(image)
histogram = Histogram(viewer)
rotated_viewer += histogram
rotated_viewer.histogram = histogram
super(ImageViewer, viewer).show()
rotated_viewer.show()