-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_bilateral_filter.py
executable file
·119 lines (90 loc) · 4.07 KB
/
demo_bilateral_filter.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 21 23:10:35 2020
@author: vik748
"""
from cmtpy.histogram_warping_ace import HistogramWarpingACE
from cmtpy import contrast_measurement as cm
from cmtpy.harris_responnse import plot_harris_eig_vals
import cv2
import numpy as np
import sys, os
from matplotlib import pyplot as plt
import scipy.stats as st
def read_image(img_name):
img = cv2.imread(img_name, cv2.IMREAD_COLOR)
gr = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
return gr
def show_plot(fig=None):
if fig is None:
fig = plt.gcf()
#plt.show()
plt.pause(1e-3)
fig.canvas.manager.window.activateWindow()
fig.canvas.manager.window.raise_()
def analyze_contrast(gr_name, graph_axes, iceberg_slice=np.s_[:,:], set_name=None):
gr_full = cv2.imread(gr_name, cv2.IMREAD_GRAYSCALE)
gr = cv2.resize(gr_full, (0,0), fx=1/5, fy=1/5, interpolation=cv2.INTER_AREA)
adjs = np.arange(0,-1.2,-.2)
sfs = np.arange(-1.0,0.2,.2)
sfs_g, adj_g = np.meshgrid(sfs, adjs)
ace_obj = HistogramWarpingACE(no_bits=8, tau=0.01, lam=5, adjustment_factor=-1.0, stretch_factor=-1.0,
min_stretch_bits=4, downsample_for_kde=True,debug=False, plot_histograms=False)
v_k, a_k = ace_obj.compute_vk_and_ak(gr)
warped_images = np.empty(adj_g.shape,dtype=object)
fig,axes = plt.subplots(*adj_g.shape, sharex=True, sharey=True)
for axi in axes.ravel():
axi.get_xaxis().set_ticks ([])
axi.get_yaxis().set_ticks ([])
axi.spines['left'].set_visible(False)
axi.spines['right'].set_visible(False)
axi.spines['bottom'].set_visible(False)
axi.spines['top'].set_visible(False)
for (i,j),adj in np.ndenumerate(adj_g):
print(i,adj)
outputs = ace_obj.compute_bk_and_dk(v_k, a_k, adjustment_factor=adj, stretch_factor=sfs_g[i,j])
warped_images[i,j], Tx = ace_obj.transform_image(*outputs, gr)
axes[i,j].imshow(warped_images[i,j], cmap='gray', vmin=0, vmax=255)
#ax.set_title("Adj factor = {:.2f}".format(adj))
for i, sf in enumerate(sfs):
axes[-1,i].set_xlabel('Stretch: {:.2f}'.format(sf))
for j, adj in enumerate(adjs):
axes[j,0].set_ylabel('Adj: {:.2f}'.format(adj))
fig.subplots_adjust(left=0.025, bottom=0.025, right=0.99, top=.9, wspace=0.00, hspace=0.00)
fig.suptitle(set_name)
show_plot()
'''
full_img_gcfs = np.zeros(warped_images.shape)
iceberg_slice_gcfs = np.zeros(warped_images.shape)
for i, wimg in enumerate(warped_images):
full_img_gcfs[i] = compute_global_contrast_factor(wimg)
iceberg_slice_gcfs[i] = compute_global_contrast_factor(wimg[iceberg_slice])
graph_axes.plot(adjs, full_img_gcfs, '.-',label=set_name+" Full Image" )
graph_axes.plot(adjs, iceberg_slice_gcfs, '.-',label=set_name+" Iceberg Slice" )
graph_axes.legend()
fig2,axes2 = plt.subplots(2,5, sharex=True, sharey=True)
fig2.suptitle(set_name)
for adj, ax, img in zip(adjs_display, axes2.ravel(), warped_images_for_plot.ravel()):
plot_harris_eig_vals(img, ax)
#plot_harris_eig_vals(img, ax)
ax.set_title("Adj factor = {:.2f}".format(adj))
ax.set(adjustable='box', aspect='equal')
ax.set_xlim(ax.set_ylim(0,None))
'''
if sys.platform == 'darwin':
data_fold=os.path.expanduser('~/Google Drive/data')
else:`
data_fold=os.path.expanduser('~/data')
data_fold=os.path.expanduser('~/data')
gr1_name = os.path.join(data_fold,'Lars1_080818','G0287250.JPG')
gr2_name = os.path.join(data_fold,'Lars2_081018','G0029490.JPG')
gr3_name = os.path.join(data_fold, 'chess_board','GOPR1488.JPG')
img_full = cv2.imread(gr2_name, cv2.IMREAD_COLOR)
gr_full = cv2.cvtColor(img_full, cv2.COLOR_BGR2GRAY)
gr = cv2.resize(gr_full, (0,0), fx=1/5, fy=1/5, interpolation=cv2.INTER_AREA)
cm.compute_global_contrast_factor(gr)
cm.compute_rms_contrast(gr,debug=True)
cm.compute_box_filt_contrast(gr, kernel_size=17, debug=True)
cm.compute_gaussian_filt_contrast(gr, sigma=5.0, debug=True)
cm.compute_bilateral_filt_contrast(gr, sigmaSpace=5.0, sigmaColor=0.05, debug=True)