图像识别01 实时图像处理程序

本文通过Python结合OpenCV库,展示了如何进行实时图像处理,包括黑白模式、一键截图、阈值调节、边缘检测、轮廓检测、高斯滤波、色彩转换和对比度增强等功能。用户可以通过GUI界面轻松操作,实现多种图像处理效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文为50 行代码,看 Python + OpenCV 玩转实时图像处理!一文的代码整合与功能拓展,当启动程序时会在控制台输出当前的模式:

在这里插入图片描述

本程序还增加了黑白模式
    if(values['gray']):
        print("黑白模式")
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
一键截图模式(注意修改你保存截图的路径):
    if event == 'Cut':
        print("一键截图")
        cv2.imencode('.jpg', frame)[1].tofile('C:\\Users\\13529\\Desktop\\jietu.jpg') # 注意保存图片的路径

在这里插入图片描述

Python代码

import PySimpleGUI as sg  #pip install pysimplegui
import cv2  #pip install opencv-python
import numpy as np #pip install numpy

# 背景色
sg.theme('LightGreen')

# 定义窗口布局
layout = [
    [sg.Image(filename='', key='image')],
    [sg.Radio('None', 'Radio', True, size=(10, 1)),
     sg.Radio('gray', 'Radio', size=(10, 1), key='gray'),sg.Button('Cut', size=(10, 1))],
    [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('contour', 'Radio', size=(10, 1), key='contour'),
     sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='contour_slider'),
     sg.Slider((0, 255), 80, 1, orientation='h', size=(20, 15), key='base_slider')],
    [sg.Radio('blur', 'Radio', 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))],
    []
]

# 窗口设计
window = sg.Window('OpenCV实时图像处理',
                   layout,
                   location=(800, 200),
                   finalize=True)


# 打开内置摄像头
cap = cv2.VideoCapture(0)
while True:
    event, values = window.read(timeout=0, timeout_key='timeout')

    # 实时读取图像
    ret, frame = cap.read()
    # # 阈值调节
    if values['thresh']:
        print("阈值调节模式")
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)[:, :, 0]
        frame = cv2.threshold(frame, values['thresh_slider'], 255, cv2.THRESH_BINARY)[1]

    # 边缘检测
    if values['canny']:
        print("边缘检测模式")
        frame = cv2.Canny(frame, values['canny_slider_a'], values['canny_slider_b'])

    # 轮廓检测
    if values['contour']:
        print("轮廓检测模式")
        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)[0]
        cv2.drawContours(frame, cnts, -1, (0, 0, 255), 2)

    # 高斯滤波
    if values['blur']:
        print("高斯滤波模式")
        frame = cv2.GaussianBlur(frame, (21, 21), values['blur_slider'])

    # 色彩转换
    if values['hue']:
        print("色彩转换模式")
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        frame[:, :, 0] += int(values['hue_slider'])
        frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)

    # 调节对比度
    if values['enhance']:
        print("调节对比度模式")
        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)

    if(values['gray']):
        print("黑白模式")
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    if event == 'Cut':
        cv2.imencode('.jpg', frame)[1].tofile('C:\\Users\\13529\\Desktop\\jietu.jpg')
    # 退出
    if event == 'Exit' or event is None:
        break

    # GUI实时更新
    imgbytes = cv2.imencode('.png', frame)[1].tobytes()
    window['image'].update(data=imgbytes)

window.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值