Updated to latest coding conventions
This commit is contained in:
		
							parent
							
								
									47e594e6de
								
							
						
					
					
						commit
						6174b355a6
					
				
					 1 changed files with 28 additions and 47 deletions
				
			
		|  | @ -9,8 +9,6 @@ Demo program that displays a webcam using OpenCV and applies some very basic ima | ||||||
| none:       no processing | none:       no processing | ||||||
| threshold:  simple b/w-threshold on the luma channel, slider sets the threshold value | threshold:  simple b/w-threshold on the luma channel, slider sets the threshold value | ||||||
| canny:      edge finding with canny, sliders set the two threshold values for the function => edge sensitivity | canny:      edge finding with canny, sliders set the two threshold values for the function => edge sensitivity | ||||||
| contour:    colour finding in the frame, first slider sets the hue for the colour to find, second the minimum saturation |  | ||||||
|             for the object. Found objects are drawn with a red contour. |  | ||||||
| blur:       simple Gaussian blur, slider sets the sigma, i.e. the amount of blur smear | blur:       simple Gaussian blur, slider sets the sigma, i.e. the amount of blur smear | ||||||
| hue:        moves the image hue values by the amount selected on the slider | hue:        moves the image hue values by the amount selected on the slider | ||||||
| enhance:    applies local contrast enhancement on the luma channel to make the image fancier - slider controls fanciness. | enhance:    applies local contrast enhancement on the luma channel to make the image fancier - slider controls fanciness. | ||||||
|  | @ -22,72 +20,55 @@ def main(): | ||||||
| 
 | 
 | ||||||
|     # define the window layout |     # define the window layout | ||||||
|     layout = [ |     layout = [ | ||||||
|       [sg.Text('OpenCV Demo', size=(40, 1), justification='center')], |       [sg.Text('OpenCV Demo', size=(60, 1), justification='center')], | ||||||
|       [sg.Image(filename='', key='image')], |       [sg.Image(filename='', key='-IMAGE-')], | ||||||
|       [sg.Radio('None', 'Radio', True, size=(10, 1))], |       [sg.Radio('None', 'Radio', True, size=(10, 1))], | ||||||
|       [sg.Radio('threshold', 'Radio', size=(10, 1), key='thresh'), |       [sg.Radio('threshold', 'Radio', size=(10, 1), key='-THRESH-'), | ||||||
|        sg.Slider((0, 255), 128, 1, orientation='h', size=(40, 15), key='thresh_slider')], |        sg.Slider((0, 255), 128, 1, orientation='h', size=(40, 15), key='-THRESH SLIDER-')], | ||||||
|       [sg.Radio('canny', 'Radio', size=(10, 1), key='canny'), |       [sg.Radio('canny', 'Radio', size=(10, 1), key='-CANNY-'), | ||||||
|        sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='canny_slider_a'), |        sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='-CANNY SLIDER A-'), | ||||||
|        sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='canny_slider_b')], |        sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='-CANNY SLIDER B-')], | ||||||
|       [sg.Radio('contour', 'Radio', size=(10, 1), key='contour'), |       [sg.Radio('blur', 'Radio', size=(10, 1), key='-BLUR-'), | ||||||
|        sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='contour_slider'), |        sg.Slider((1, 11), 1, 1, orientation='h', size=(40, 15), key='-BLUR SLIDER-')], | ||||||
|        sg.Slider((0, 255), 80, 1, orientation='h', size=(20, 15), key='base_slider')], |       [sg.Radio('hue', 'Radio', size=(10, 1), key='-HUE-'), | ||||||
|       [sg.Radio('blur', 'Radio', size=(10, 1), key='blur'), |        sg.Slider((0, 225), 0, 1, orientation='h', size=(40, 15), key='-HUE SLIDER-')], | ||||||
|        sg.Slider((1, 11), 1, 1, orientation='h', size=(40, 15), key='blur_slider')], |       [sg.Radio('enhance', 'Radio', size=(10, 1), key='-ENHANCE-'), | ||||||
|       [sg.Radio('hue', 'Radio', size=(10, 1), key='hue'), |        sg.Slider((1, 255), 128, 1, orientation='h', size=(40, 15), key='-ENHANCE SLIDER-')], | ||||||
|        sg.Slider((0, 225), 0, 1, orientation='h', size=(40, 15), key='hue_slider')], |  | ||||||
|       [sg.Radio('enhance', 'Radio', size=(10, 1), key='enhance'), |  | ||||||
|        sg.Slider((1, 255), 128, 1, orientation='h', size=(40, 15), key='enhance_slider')], |  | ||||||
|       [sg.Button('Exit', size=(10, 1))] |       [sg.Button('Exit', size=(10, 1))] | ||||||
|     ] |     ] | ||||||
| 
 | 
 | ||||||
|     # create the window and show it without the plot |     # create the window and show it without the plot | ||||||
|     window = sg.Window('Demo Application - OpenCV Integration', |     window = sg.Window('OpenCV Integration', layout, location=(800, 400)) | ||||||
|                layout, |  | ||||||
|                location=(800, 400), |  | ||||||
|                finalize=True) |  | ||||||
| 
 | 
 | ||||||
|     cap = cv2.VideoCapture(0) |     cap = cv2.VideoCapture(0) | ||||||
|  | 
 | ||||||
|     while True: |     while True: | ||||||
|         event, values = window.read(timeout=0, timeout_key='timeout') |         event, values = window.read(timeout=20) | ||||||
|         if event == 'Exit' or event is None: |         if event == 'Exit' or event == sg.WIN_CLOSED: | ||||||
|             break |             break | ||||||
| 
 | 
 | ||||||
|         ret, frame = cap.read() |         ret, frame = cap.read() | ||||||
| 
 | 
 | ||||||
|         if values['thresh']: |         if values['-THRESH-']: | ||||||
|             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)[:, :, 0] |             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)[:, :, 0] | ||||||
|             frame = cv2.threshold(frame, values['thresh_slider'], 255, cv2.THRESH_BINARY)[1] |             frame = cv2.threshold(frame, values['-THRESH SLIDER-'], 255, cv2.THRESH_BINARY)[1] | ||||||
| 
 |         elif values['-CANNY-']: | ||||||
|         if values['canny']: |             frame = cv2.Canny(frame, values['-CANNY SLIDER A-'], values['-CANNY SLIDER B-']) | ||||||
|             frame = cv2.Canny(frame, values['canny_slider_a'], values['canny_slider_b']) |         elif values['-BLUR-']: | ||||||
| 
 |             frame = cv2.GaussianBlur(frame, (21, 21), values['-BLUR SLIDER-']) | ||||||
|         if values['blur']: |         elif values['-HUE-']: | ||||||
|             frame = cv2.GaussianBlur(frame, (21, 21), values['blur_slider']) |  | ||||||
| 
 |  | ||||||
|         if values['hue']: |  | ||||||
|             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) |             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | ||||||
|             frame[:, :, 0] += values['hue_slider'] |             frame[:, :, 0] += int(values['-HUE SLIDER-']) | ||||||
|             frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR) |             frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR) | ||||||
| 
 |         elif values['-ENHANCE-']: | ||||||
|         if values['enhance']: |             enh_val = values['-ENHANCE SLIDER-'] / 40 | ||||||
|             enh_val = values['enhance_slider'] / 40 |  | ||||||
|             clahe = cv2.createCLAHE(clipLimit=enh_val, tileGridSize=(8, 8)) |             clahe = cv2.createCLAHE(clipLimit=enh_val, tileGridSize=(8, 8)) | ||||||
|             lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB) |             lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB) | ||||||
|             lab[:, :, 0] = clahe.apply(lab[:, :, 0]) |             lab[:, :, 0] = clahe.apply(lab[:, :, 0]) | ||||||
|             frame = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) |             frame = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) | ||||||
| 
 | 
 | ||||||
|         if values['contour']: |  | ||||||
|             hue = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) |  | ||||||
|             hue = cv2.GaussianBlur(hue, (21, 21), 1) |  | ||||||
|             hue = cv2.inRange(hue, np.array([values['contour_slider'], values['base_slider'], 40]), |  | ||||||
|                               np.array([values['contour_slider'] + 30, 255, 220])) |  | ||||||
|             cnts = cv2.findContours(hue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1] |  | ||||||
|             cv2.drawContours(frame, cnts, -1, (0, 0, 255), 2) |  | ||||||
|          |  | ||||||
|         imgbytes = cv2.imencode('.png', frame)[1].tobytes() |         imgbytes = cv2.imencode('.png', frame)[1].tobytes() | ||||||
|         window['image'].update(data=imgbytes) |         window['-IMAGE-'].update(data=imgbytes) | ||||||
| 
 | 
 | ||||||
|     window.close() |     window.close() | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue