From 4b3af72048c4c40a159679eaa2efbbe8db8a10b9 Mon Sep 17 00:00:00 2001 From: kawakami Date: Tue, 11 Feb 2020 06:07:11 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AD=E3=83=A3=E3=83=97=E3=83=81=E3=83=A3?= =?UTF-8?q?=E6=98=A0=E5=83=8F=E4=B8=8A=E3=81=A7=E3=81=AE=E3=83=88=E3=83=AA?= =?UTF-8?q?=E3=83=A0=E7=94=BB=E5=83=8F=E4=BF=9D=E5=AD=98=E6=A9=9F=E8=83=BD?= =?UTF-8?q?=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit マウス左ドラッグののdownとupで決める 範囲指定用の線が出せないので敬遠していたが代用で改良していきたい --- SerialController/Camera.py | 12 +++++++++--- SerialController/GuiAssets.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/SerialController/Camera.py b/SerialController/Camera.py index ae9ac32..8ac2655 100644 --- a/SerialController/Camera.py +++ b/SerialController/Camera.py @@ -35,7 +35,8 @@ def readFrame(self): _, self.image_bgr = self.camera.read() return self.image_bgr - def saveCapture(self): + # box(for trim) format: (left, right, up, bottom) + def saveCapture(self, box=None): dt_now = datetime.datetime.now() fileName = dt_now.strftime('%Y-%m-%d_%H-%M-%S')+".png" @@ -43,8 +44,13 @@ def saveCapture(self): os.makedirs(self.capture_dir) save_path = os.path.join(self.capture_dir, fileName) - cv2.imwrite(save_path, self.image_bgr) - print('capture succeeded: ' + save_path) + + try: + image = self.image_bgr if box is None else self.image_bgr[box[2]:box[3], box[0]:box[1]] + cv2.imwrite(save_path, image) + print('capture succeeded: ' + save_path) + except: + print('capture failed') def destroy(self): if self.camera is not None and self.camera.isOpened(): diff --git a/SerialController/GuiAssets.py b/SerialController/GuiAssets.py index fbe5fdf..04c1717 100644 --- a/SerialController/GuiAssets.py +++ b/SerialController/GuiAssets.py @@ -14,9 +14,11 @@ def __init__(self, camera, fps, is_show, master=None): self.camera = camera self.show_size = (640, 360) self.is_show_var = is_show + self.is_trim_mode = False self.setFps(fps) self.bind("", self.mouseLeftDown) + self.bind("", self.mouseLeftUp) # Set disabled image first disabled_img = cv2.imread("../Images/disabled.png", cv2.IMREAD_GRAYSCALE) @@ -33,6 +35,24 @@ def mouseLeftDown(self, event): ratio_x = float(self.camera.capture_size[0] / self.show_size[0]) ratio_y = float(self.camera.capture_size[1] / self.show_size[1]) print('mouse down: show ({}, {}) / capture ({}, {})'.format(x, y, int(x * ratio_x), int(y * ratio_y))) + + self.is_trim_mode = True + self.down_trim_x = int(x * ratio_x) + self.down_trim_y = int(y * ratio_y) + + def mouseLeftUp(self, event): + x, y = event.x, event.y + ratio_x = float(self.camera.capture_size[0] / self.show_size[0]) + ratio_y = float(self.camera.capture_size[1] / self.show_size[1]) + up_trim_x = int(x * ratio_x) + up_trim_y = int(y * ratio_y) + print('mouse up: show ({}, {}) / capture ({}, {})'.format(x, y, up_trim_x, up_trim_y)) + + if self.is_trim_mode: + self.camera.saveCapture( + (min(self.down_trim_x, up_trim_x), max(self.down_trim_x, up_trim_x), + min(self.down_trim_y, up_trim_y), max(self.down_trim_y, up_trim_y))) + self.is_trim_mode = False def startCapture(self): self.capture()