def __init__(self,save_path output ,wide 800, height 500):
self.W, self.H wide, height
self.button_w, self.button_h 50, 30
self.button_hstretch,self.button_wstretch 10,10
self._init_root()
self.calib_tool None
self.save_path save_path
os.makedirs(self.save_path, exist_ok True)
def run(self):
self.root.mainloop()
def _init_root(self,):
# creat window
self.root tk.Tk()
self.root.title( camera calib )
self.root.config(background white )
self.root.geometry(str(self.W) x str(self.H) -500-200 )
self.root.resizable(0, 0)
#self.root.iconbitmap( ./image/icon.ico )
self.root.attributes( -alpha , 0.95)
left area
# creat video area
self.left_w, self.left_h int(3 / 4 * self.W), int(self.H)
self.photo_area tk.Label(self.root, bg black , fg green , cursor cross )
self.photo_area.place(x 1, y 1, w self.left_w, h self.left_h - 35)
# state area
self.state_area tk.Label(self.root, text ---------------- )
self.state_area.place(x 1, y self.left_h - 32, w self.left_w, h 30)
right area
right_x, right_y self.left_w 20, 7 / 11 * self.H
# the area output the calib info
self.text_area tk.Text(self.root,wrap None,bg black ,fg green ,relief flat ,font ( 楷体 ,12))
self.text_area.place(x self.left_w 5, y 1, w self.W - self.left_w - 10, h right_y)
# entry
tk.Label(self.root, text 标定板参数: , bg white , justify left )
.place(x right_x, y right_y 5, w 60, h 20)
tk.Label(self.root, text 分布 , bg white , justify left )
.place(x right_x, y right_y 25, w 30, h self.button_h)
self.input_rc tk.Entry(self.root, bd 5, relief groove )
self.input_rc.place(x right_x 30, y right_y 25, w 100, h self.button_h)
self.input_rc.insert(1, 8 6 )
tk.Label(self.root, text 尺寸 , bg white , justify left )
.place(x right_x, y right_y 55, w 30, h self.button_h)
self.input_square tk.Entry(self.root, bd 5, relief groove )
self.input_square.place(x right_x 30, y right_y 55, w 100, h self.button_h)
self.input_square.insert(1, 20 20 )
#button
tk.Button(self.root, text 标定 , relief groove , command self._calib)
.place(x right_x 15, y right_y 100, w self.button_w, h self.button_h)
tk.Button(self.root, text 矫正 , relief groove , command self._rectify)
.place(x right_x 15 self.button_w self.button_wstretch, y right_y 100, w self.button_w, h self.button_h)
def _calib(self):
# 获取标定板参数
pattern_rc tuple(int(x) for x in self.input_rc.get().split( ))
pattern_square tuple(int(x) for x in self.input_square.get().split( ))
self.calib_tool CalibTool(pattern_rc, pattern_square)
file filedialog.askopenfilename()
filename file.split( . )[0].split( / ) [-1] # os.sep is incorrect
self._set_img(img self._open_img(file))
C, dist, R, T, corners_img self.calib_tool.calib(img self._open_img(file))
self._set_img(img corners_img)
save_location os.path.join(self.save_path, filename.split( . )[0] .txt )
content self.calib_tool.save_paras(save_location,other 425 2.5 )
self.text_area.delete(1.0, end ) # 清空再插入
self.text_area.insert(1.0,content)
self.state_area.config(text save paras to: save_location)
self.state_area.update()
def _rectify(self):
file filedialog.askopenfilename()
filename file.split( . )[0].split( / ) [-1] # os.sep is incorrect
save_location os.path.join(self.save_path, filename.split( . )[0] .bmp )
self._set_img(img self._open_img(file))
rectify_img self.calib_tool.rectify(self._open_img(file), save_location)
self._set_img(img rectify_img)
self.state_area.config(text save rectify img to: save_location)
self.state_area.update()
# 使能读取中文名称图片
def _open_img(self,img_path):
return cv2.imdecode(np.fromfile(img_path, dtype np.uint8), cv2.IMREAD_COLOR)
# convert frame to tk img and show
def _set_img(self,img):
h, w, _ img.shape
wc ,hc self.left_w,int(h * (self.left_w / w))
img cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
current_image Image.fromarray(img, RGB ).resize((wc, hc), Image.ANTIALIAS)
img ImageTk.PhotoImage(image current_image)
self.photo_area.config(image img)
self.photo_area.image img