現在、openCVを用いて画像の領域抽出を勉強しているところです。
抽出方法をラジオボタンを用いて選択できるように組みたいのですが
ラジオボタンの初期値を設定しても、反映されず、
4個設定しているラジオボタンのうち、3個が選択された状態になってしまいます。
ラジオボタンを1つ選択し、実行。その値をプリントしても、ラジオボタンの値は反映されません。
どこが悪いのか分からず困っています。
どなたかご教授いただけないでしょうか。
なお、コードは未完成な部分が多く見ずらく申し訳ございませんがよろしくお願い致します。
Python
1 2 3import tkinter as tk 4import cv2 5import matplotlib.pyplot as plt 6from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 7import numpy as np 8import pydicom 9import fileselect as fs 10import copy 11 12 13def level(): 14 global ww_low, ww_high, w_level, w_width 15 16 window_level, window_width = level_sc.get(), w_width_sc.get() 17 18 ww_low = window_level - (window_width // 2) 19 ww_high = window_level + (window_width // 2) 20 21def window(self): 22 global ww_low, ww_high, img_arr, ax1 23 24 level() 25 26 ax1.imshow(img_arr, cmap='bone', vmin=ww_low, vmax=ww_high) 27 fig.canvas.draw() 28 29 30def thresho(self): 31 global img_arr, ax2, fig, thresh 32 33 ret, thresh = cv2.threshold(img_arr, int(self), 255, cv2.THRESH_BINARY) 34 ax2.imshow(thresh, cmap='bone') 35 36 fig.canvas.draw() 37 38 39 40def cont(): 41 global ax3, fig, img_arr, thresh, ww_low, ww_high, var 42 43 img_unit8 = copy.deepcopy(img_arr) 44 45 np.clip(img_unit8, ww_low, ww_high, out= img_unit8) 46 47 img_unit8 -= img_unit8.min() 48 49 np.floor_divide(img_unit8, (img_unit8.max() + 1) / 256, 50 out = img_unit8, casting='unsafe') 51 52 53 cv2.imwrite('./img_8bit.png',img_unit8) 54 55 img_png = cv2.imread('img_8bit.png') 56 thresh_8bit = thresh.astype('u1') 57 58 cont_type = var.get() 59 print(cont_type) 60 61 ret, contours, hierarchy = cv2.findContours(thresh_8bit, cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE) 62 63 for contour in contours: 64 cv2.drawContours(img_png, contours, -1, (255,255,0) , 1) 65 66 ax3.imshow(img_png, cmap='bone') 67 68 fig.canvas.draw() 69 70def up(): 71 val = thre_sc.get() + 5 72 thre_sc.set(val) 73 74def down(): 75 val = thre_sc.get() - 5 76 thre_sc.set(val) 77 78def down_wl(): 79 val = level_sc.get() - 5 80 level_sc.set(val) 81 82def up_wl(): 83 val = level_sc.get() + 5 84 level_sc.set(val) 85 86 87def down_ww(): 88 val = level_sc.get() - 5 89 level_sc.set(val) 90 91 92def up_ww(): 93 val = level_sc.get() + 5 94 level_sc.set(val) 95 96def main(): 97 global ww_low, ww_high, img_arr_copy, thre_sc, ax2, fig,\ 98 img_arr, ax1, level_sc, w_width_sc, ww_low, ww_high, \ 99 w_level, w_width, ax3, var 100 101 102 filename = fs.single_fileselect() 103 dcm = pydicom.dcmread(filename) 104 img_arr = np.array(dcm.pixel_array) 105 w_level = int(dcm[0x0028,0x1050].value) 106 w_width = int(dcm[0x0028, 0x1051].value) 107 108 fig = plt.figure(figsize=(13, 4)) 109 ax1 = fig.add_subplot(1, 3, 1) 110 ax2 = fig.add_subplot(1, 3, 2) 111 ax3 = fig.add_subplot(1, 3, 3) 112 113 ax1.axes.xaxis.set_visible(False), ax1.axes.yaxis.set_visible(False) 114 ax2.axes.xaxis.set_visible(False), ax2.axes.yaxis.set_visible(False) 115 ax3.axes.xaxis.set_visible(False), ax3.axes.yaxis.set_visible(False) 116 117 plt.subplots_adjust(left=0.01, right=0.995, bottom=0, top=1, wspace=0.01) 118 119 root = tk.Tk() 120 root.geometry("1510x500") 121 122 Canvas = FigureCanvasTkAgg(fig, master=root) 123 Canvas.get_tk_widget().grid(row=0, column=0, rowspan=10) 124 125 126 127 var_scale_level = tk.IntVar() 128 level_sc = tk.Scale(root, 129 label='Window Level', 130 variable=var_scale_level, 131 orient=tk.HORIZONTAL, 132 length=200, 133 from_= np.min(img_arr), 134 to=np.max(img_arr), 135 command=window) 136 level_sc.set(w_level) 137 level_sc.grid(row=1, column=1,columnspan=2) 138 139 down_wl_btn = tk.Button(root, text="down", command=down_wl) 140 down_wl_btn.grid(row=2, column=1) 141 142 up_wl_btn = tk.Button(root, text=" up ", command=up_wl) 143 up_wl_btn.grid(row=2, column=2) 144 145 var_scale_w_width = tk.IntVar() 146 w_width_sc = tk.Scale(root, 147 label = 'Window Width', 148 variable=var_scale_w_width, 149 orient=tk.HORIZONTAL, 150 length=200, 151 from_=0, 152 to=(np.max(img_arr) - np.min(img_arr))//2, 153 command=window) 154 w_width_sc .set(w_width) 155 w_width_sc.grid(row=3, column=1,columnspan=2) 156 157 down_ww_btn = tk.Button(root, text="down", command=down_ww) 158 down_ww_btn.grid(row=4, column=1) 159 160 up_ww_btn = tk.Button(root, text=" up ", command=up_ww) 161 up_ww_btn.grid(row=4, column=2) 162 163 164 thre = 0 165 166 var_thre = tk.IntVar() 167 thre_sc = tk.Scale(root, 168 label='Threshold', 169 variable=var_thre, 170 orient=tk.HORIZONTAL, 171 length=200, 172 from_=np.min(img_arr), 173 to=np.max(img_arr), 174 command=thresho) 175 thre_sc.set(thre) 176 thre_sc.grid(row=5, column=1,columnspan=2) 177 178 down_btn = tk.Button(root, text="down", command=down) 179 down_btn.grid(row=6, column=1) 180 181 up_btn = tk.Button(root, text=" up ", command=up) 182 up_btn.grid(row=6, column=2) 183 184 185 ''' 186 0 : cv2.RETR_EXTERNAL 187 1 : cv2.RETR_LIST 188 2 : cv2.RETR_CCOMP 189 3 : cv2.RETR_TREE 190 ''' 191 192 var = tk.IntVar() 193 var.set(3) 194 195 rb1 = tk.Radiobutton(root, value=0, variable=var, text='EXTERNAL' ) 196 rb1.grid(row=8, column=1) 197 rb2 = tk.Radiobutton(root, value=1, variable=var, text='LIST' ) 198 rb2.grid(row=8, column=2) 199 rb3 = tk.Radiobutton(root, value=2, variable=var, text='CCOMP' ) 200 rb3.grid(row=9, column=1) 201 rb4 = tk.Radiobutton(root, value=3, variable=var, text='TREE' ) 202 rb4.grid(row=9, column=2) 203 204 205 app_btn = tk.Button(root, text="実行", command=cont) 206 app_btn.grid(row=10, column=2) 207 208 209 window(1) 210 thresho(1) 211 212 root.mainloop() 213 214if __name__ == "__main__": 215 main()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/03 06:59
2021/01/03 22:48 編集