私的AI研究会 > PythonGUI2
Python プログラム作成に必要な GUI を試してみる。
OpenCV 下で利用できるプログラミング方法を考察する。Webサイトのサンプルプログラムを実行してみる。(不都合がある箇所は修正)
# -*- coding: utf-8 -*- ##------------------------------------------ ## File select dialog ## ## 2021.12.15 Masahiro Izutsu ##------------------------------------------ ## file.py from tkinter import filedialog filename = filedialog.askopenfilename( title = "画像ファイルを開く", filetypes = [("Image file", ".bmp .png .jpg .tif"), ("Bitmap", ".bmp"), ("PNG", ".png"), ("JPEG", ".jpg"), ("Tiff", ".tif") ], # ファイルフィルタ initialdir = "./" # 自分自身のディレクトリ ) print(filename)
# -*- coding: utf-8 -*- ##------------------------------------------ ## Track Bar with OpenCV ## ## 2021.12.16 Masahiro Izutsu ##------------------------------------------ ## cvtrackbar.py ## http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_gui/py_trackbar/py_trackbar.html import cv2 import numpy as np def nothing(x): pass # Create a black image, a window img = np.zeros((100,512,3), np.uint8) cv2.namedWindow('image') # create trackbars for color change cv2.createTrackbar('R','image',0,255,nothing) cv2.createTrackbar('G','image',0,255,nothing) cv2.createTrackbar('B','image',0,255,nothing) # create switch for ON/OFF functionality switch = '0 : OFF \n1 : ON' cv2.createTrackbar(switch, 'image',1,1,nothing) while(1): cv2.imshow('image',img) k = cv2.waitKey(1) & 0xFF if k == 27: break # get current positions of four trackbars r = cv2.getTrackbarPos('R','image') g = cv2.getTrackbarPos('G','image') b = cv2.getTrackbarPos('B','image') s = cv2.getTrackbarPos(switch,'image') if s == 0: img[:] = 0 else: img[:] = [b,g,r] cv2.destroyAllWindows()
# -*- coding: utf-8 -*- ##------------------------------------------ ## Track Bar 2 with OpenCV ## ## 2021.12.16 Masahiro Izutsu ##------------------------------------------ ## cvtrackbar2.py ## https://epic-life.me/archives/1363 import cv2 import numpy as np def printing(position): print(position) # 画像を読み込み # cv2.imread() を利用しグレースケールで読み込む img = cv2.imread("color_circle.png", 0) # トラックバーを表示するウィンドウを作成 cv2.namedWindow("window") # トラックバーを作成する # トラックバーの名前、トラックバーをつけるウィンドウ名、初期値、トラックバーの最大値。コールバック関数 cv2.createTrackbar("track", "window", 150, 255, printing) while True: # トラックバーの位置をthreshholdに代入 threshold = cv2.getTrackbarPos("track", "window") # cv2.THRESH_BINARY にて2値化する ret, th = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY) # 画像を表示する # 引数はWindow名に合わせる cv2.imshow('window', th) if cv2.waitKey(10) == 27: break cv2.destroyAllWindows()
-*- coding: utf-8 -*- ##------------------------------------------ ## Track Bar 3 with OpenCV ## ## 2021.12.16 Masahiro Izutsu ##------------------------------------------ ## cvtrackbar3.py ## https://blog.electroica.com/trackbar-in-opencv-python/ #import opencv and numpy import cv2 import numpy as np #trackbar callback fucntion does nothing but required for trackbar def nothing(x): pass #create a seperate window named 'controls' for trackbar cv2.namedWindow('controls') #create trackbar in 'controls' window with name 'r'' cv2.createTrackbar('r','controls',15,255,nothing) #create a while loop act as refresh for the view while(1): #create a black image img = np.zeros((512,512,3), np.uint8) #calculate center of image img_center_y=img.shape[0]//2 img_center_x=img.shape[1]//2 #returns current position/value of trackbar radius= int(cv2.getTrackbarPos('r','controls')) #draw a red circle in the center of the image with radius set by trackbar position cv2.circle(img,(img_center_y,img_center_x), radius, (0,0,255), -1) #show the image window cv2.imshow('img',img) # waitfor the user to press escape and break the while loop k = cv2.waitKey(1) & 0xFF if k == 27: break #destroys all window cv2.destroyAllWindows()
# -*- coding: utf-8 -*- ##------------------------------------------ ## Track Bar 4 with OpenCV ## ## 2021.12.16 Masahiro Izutsu ##------------------------------------------ ## cvtrackbar4.py ## https://blog.electroica.com/trackbar-in-opencv-python/ #import opencv and numpy import cv2 import numpy as np #trackbar callback fucntion does nothing but required for trackbar def change_color(x): #condition to change color if trackbar value is greater than 127 if(cv2.getTrackbarPos('r','controls')>127): global circle_color circle_color=(255,0,0) else: circle_color=(0,0,255) #create a seperate window named 'controls' for trackbar cv2.namedWindow('controls') #create trackbar in 'controls' window with name 'r'' cv2.createTrackbar('r','controls',15,255,change_color) #initial color circle_color=(0,0,255) while(1): #create a black image img = np.zeros((512,512,3), np.uint8) #calculate center of image img_center_y=img.shape[0]//2 img_center_x=img.shape[1]//2 #returns current position/value of trackbar radius= int(cv2.getTrackbarPos('r','controls')) #draw a red circle in the center of the image with radius set by trackbar position cv2.circle(img,(img_center_y,img_center_x), radius, circle_color, -1) #show the image window cv2.imshow('img',img) #waitfor the user to press escape and break the while loop k = cv2.waitKey(1) & 0xFF if k == 27: break #destroys all window cv2.destroyAllWindows()
# -*- coding: utf-8 -*- ##------------------------------------------ ## Track Bar 5 with OpenCV ## ## 2021.12.16 Masahiro Izutsu ##------------------------------------------ ## cvtrackbar5.py ## https://blog.electroica.com/trackbar-in-opencv-python/ #import opencv and numpy import cv2 import numpy as np #trackbar callback fucntion does nothing but required for trackbar def nothing(x): pass def change_color(x): #condition to change color if trackbar value is greater than 127 if(cv2.getTrackbarPos('r','controls')>127): global color color=(255,0,0) else: color=(0,0,255) #create a seperate window named 'controls' for trackbar cv2.namedWindow('controls') #create trackbar in 'controls' window with name 'r'' cv2.createTrackbar('r','controls',15,255,change_color) cv2.createTrackbar('s','controls',0,1,nothing) #initial color color=(0,0,255) while(1): #create a black image img = np.zeros((512,512,3), np.uint8) #calculate center of image img_center_y=img.shape[0]//2 img_center_x=img.shape[1]//2 #returns current position/value of trackbar radius= int(cv2.getTrackbarPos('r','controls')) #returns current slider value of trackbar 's' shape=cv2.getTrackbarPos('s','controls') #checks if shape varible is off (0) or on (1) #draw square if switch is on if(shape==1): start_point=(img_center_y-100,img_center_x-100) end_point=(img_center_y+100,img_center_x+100) #change thickness of square based in trackbar 'r' thickness = radius #create a square image = cv2.rectangle(img, start_point, end_point, color, thickness) pass #draw circle if switch is off else: #draw a red circle cv2.circle(img,(img_center_y,img_center_x), radius, color, -1) #show the image window cv2.imshow('img',img) #waitfor the user to press escape and break the while loop k = cv2.waitKey(1) & 0xFF if k == 27: break #destroys all window cv2.destroyAllWindows()
# -*- coding: utf-8 -*- ##------------------------------------------ ## Track Bar 6 with OpenCV ## HSV Trackbar ## ## 2021.12.16 Masahiro Izutsu ##------------------------------------------ ## cvtrackbar6.py ## https://blog.electroica.com/hsv-trackbar-opencv-python/ #import opencv and numpy import cv2 import numpy as np #trackbar callback fucntion to update HSV value def callback(x): global H_low,H_high,S_low,S_high,V_low,V_high #assign trackbar position value to H,S,V High and low variable H_low = cv2.getTrackbarPos('low H','controls') H_high = cv2.getTrackbarPos('high H','controls') S_low = cv2.getTrackbarPos('low S','controls') S_high = cv2.getTrackbarPos('high S','controls') V_low = cv2.getTrackbarPos('low V','controls') V_high = cv2.getTrackbarPos('high V','controls') #create a seperate window named 'controls' for trackbar cv2.namedWindow('controls',2) cv2.resizeWindow("controls", 550,200); #global variable H_low = 0 H_high = 179 S_low= 0 S_high = 255 V_low= 0 V_high = 255 #create trackbars for high,low H,S,V cv2.createTrackbar('low H','controls',0,179,callback) cv2.createTrackbar('high H','controls',179,179,callback) cv2.createTrackbar('low S','controls',0,255,callback) cv2.createTrackbar('high S','controls',255,255,callback) cv2.createTrackbar('low V','controls',0,255,callback) cv2.createTrackbar('high V','controls',255,255,callback) while(1): #read source image img=cv2.imread("color_circle.png") #convert sourece image to HSC color mode hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # hsv_low = np.array([H_low, S_low, V_low], np.uint8) hsv_high = np.array([H_high, S_high, V_high], np.uint8) #making mask for hsv range mask = cv2.inRange(hsv, hsv_low, hsv_high) print (mask) #masking HSV value selected color becomes black res = cv2.bitwise_and(img, img, mask=mask) #show image cv2.imshow('mask',mask) cv2.imshow('res',res) #waitfor the user to press escape and break the while loop k = cv2.waitKey(1) & 0xFF if k == 27: break #destroys all window cv2.destroyAllWindows()
Trackbar を使用したプログラムについて「2:OpenVino」環境では下記のワーニングエラーが発生する。
$ cd ~/workspace_py37/exercise $ python3 cvtrackbar2.py [ WARN:0] global ../opencv/modules/highgui/src/window.cpp (661) createTrackbar UI/Trackbar(track@window): Using 'value' pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback. 150