Python + OpenCV实时图像处理(mac pycharm 正常运行)

这段代码最初的出处有待考证,国内有诸多版本,下面给出两个链接

PySimpleGUI/Demo_OpenCV_Simple_GUI.py at master · PySimpleGUI/PySimpleGUI · GitHub

50 行代码,看 Python + OpenCV 玩转实时图像处理!_爬遍天下无敌手的博客-CSDN博客

下面这段代码是国外版本修改过的,我的mac上可以正常运行

import PySimpleGUI as sg
import cv2
import numpy as np

"""
Demo program that displays a webcam using OpenCV and applies some very basic image functions
- functions from top to bottom -
none:       no processing
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
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
enhance:    applies local contrast enhancement on the luma channel to make the image fancier - slider controls fanciness.
"""


def main():
    sg.theme('LightGreen')

    # define the window layout
    layout = [
      [sg.Image(size=(100, 100), filename='', key='-IMAGE-')],
      [sg.Radio('threshold', 'Radio', size=(10, 1), key='-THRESH-'),
       sg.Slider((0, 255), 128, 1, orientation='h', size=(40, 15), key='-THRESH SLIDER-')],
      [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 B-')],
      [sg.Radio('blur', 'Radio', True, size=(10, 1), key='-BLUR-'),
       sg.Slider((1, 11), 1, 1, orientation='h', size=(40, 15), key='-BLUR SLIDER-')],
      [sg.Radio('hue', 'Radio', size=(10, 1), key='-HUE-'),
       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))]
    ]

    # create the window and show it without the plot
    window = sg.Window('OpenCV Integration', layout, location=(0,0), size=(800,1200), keep_on_top=True, finalize=True)
    cap = cv2.VideoCapture(0)

    while True:
        event, values = window.read(timeout=20)
        if event == 'Exit' or event == sg.WIN_CLOSED:
            break

        ret, frame = cap.read()
        height, width, channels = frame.shape
        scale=10
        # prepare the crop

        if values['-THRESH-']:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)[:, :, 0]
            frame = cv2.threshold(frame, values['-THRESH SLIDER-'], 255, cv2.THRESH_BINARY)[1]
        elif values['-CANNY-']:
            frame = cv2.Canny(frame, values['-CANNY SLIDER A-'], values['-CANNY SLIDER B-'])
        elif values['-BLUR-']:
            frame = cv2.GaussianBlur(frame, (21, 21), values['-BLUR SLIDER-'])
        elif values['-HUE-']:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            frame[:, :, 0] += int(values['-HUE SLIDER-'])
            frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
        elif values['-ENHANCE-']:
            enh_val = values['-ENHANCE SLIDER-'] / 40
            clahe = cv2.createCLAHE(clipLimit=enh_val, tileGridSize=(8, 8))
            lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
            lab[:, :, 0] = clahe.apply(lab[:, :, 0])
            frame = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)

        imgbytes = cv2.imencode('.png', frame)[1].tobytes()

        window['-IMAGE-'].update(data=imgbytes)

    window.close()


main()

国内版本都没给出完整的代码,都是分段解释。

国外版本应该可以在很多电脑上正常运行,不过我的苹果本上运行是这个样子的

Python + OpenCV实时图像处理(mac pycharm 正常运行)_第1张图片

 貌似因为 控件 数量 超过某一个值,window 自动缩小了,缩小后导致其余控件不可见了。

把其中 Window初始化 的代码改了

window = sg.Window('OpenCV Integration', layout, location=(0,0), size=(800,1200), keep_on_top=True, finalize=True)

倒是都可见了,但有一部分控件在 程序坞之下,还是不能用,于是在原基础上又删了 ‘text 控件’和默认选中的‘radio’,把默认选中的改为

[sg.Radio('blur', 'Radio', True, size=(10, 1), key='-BLUR-'),
 sg.Slider((1, 11), 1, 1, orientation='h', size=(40, 15), key='-BLUR SLIDER-')],

最终运行如下

Python + OpenCV实时图像处理(mac pycharm 正常运行)_第2张图片

 大还是大了点,能用就行。

你可能感兴趣的:(python,opencv,计算机视觉)