You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV imshow函数无响应,实时屏幕颜色检测问题求助

解决OpenCV imshow窗口无响应问题及实时颜色检测优化方案

核心问题修复

你的代码中cv.imshow后缺少cv.waitKey()调用,这是窗口无响应的直接原因。OpenCV需要通过waitKey()处理窗口事件循环,才能实时更新画面。

在所有cv.imshow语句后添加以下代码,既能更新窗口,也能通过按q键安全退出循环:

if cv.waitKey(1) & 0xFF == ord('q'):
    cv.destroyAllWindows()
    break

代码优化建议

  1. 替换time.sleep(1.5):sleep会阻塞整个程序,导致画面卡顿。改用时间戳控制操作间隔,不影响实时画面更新:

    last_operation_time = 0
    operation_interval = 1.5  # 操作间隔1.5秒
    # 在循环内判断
    current_time = time.time()
    if coord is not None and current_time - last_operation_time >= operation_interval:
        # 执行拖拽操作
        last_operation_time = current_time
    
  2. 简化截图流程:mss可直接返回numpy数组,跳过PIL转换,提升效率:

    img = np.array(sct.grab(mon))
    img = cv.cvtColor(img, cv.COLOR_BGRA2RGB)  # mss返回BGRA格式,转RGB适配OpenCV
    
  3. 修正灰色HSV范围:你当前的HSV范围并非标准灰色区间,灰色的饱和度(S)接近0,建议调整为:

    lower_gray = np.array([0, 0, 50])
    upper_gray = np.array([180, 50, 200])
    

修改后的完整代码

import numpy as np
import mss
import cv2 as cv
import pyautogui
import time

topPixels = 880
leftPixels = 200

mon = {'top': topPixels, 'left': leftPixels, 'width': 1000, 'height': 2}
sct = mss.mss()

last_operation_time = 0
operation_interval = 1.5

while True:
    # 直接获取屏幕帧并转换格式
    img = np.array(sct.grab(mon))
    img = cv.cvtColor(img, cv.COLOR_BGRA2RGB)

    # RGB转HSV
    hsv = cv.cvtColor(img, cv.COLOR_RGB2HSV)
    
    # 灰色HSV范围
    lower_gray = np.array([0, 0, 50])
    upper_gray = np.array([180, 50, 200])

    mask = cv.inRange(hsv, lower_gray, upper_gray)
    res = cv.bitwise_and(img, img, mask=mask)

    coord = cv.findNonZero(mask)
    current_time = time.time()
    if coord is not None and current_time - last_operation_time >= operation_interval:
        x = leftPixels + coord[0, 0].item(0)
        y = topPixels + coord[0, 0].item(1)
        pyautogui.moveTo(x, y)
        pyautogui.dragTo(x, y - 140, button='left')
        last_operation_time = current_time
    else:
        print("No coords or operation interval not reached")
        
    cv.imshow('original', img)
    cv.imshow('mask', mask)
    cv.imshow('res', res)
    
    # 处理窗口事件,按q退出
    if cv.waitKey(1) & 0xFF == ord('q'):
        sct.close()
        cv.destroyAllWindows()
        break

其他实时颜色检测方案

  • dxcam(Windows平台):支持硬件加速的高性能屏幕捕获库,适合高帧率需求:
    import dxcam
    import cv2 as cv
    
    camera = dxcam.create()
    region = (leftPixels, topPixels, leftPixels+1000, topPixels+2)
    while True:
        frame = camera.grab(region)
        if frame is not None:
            hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
            # 后续颜色检测逻辑
    
  • pyautogui截图:适合简单场景,无需额外配置,但帧率低于mss/dxcam:
    import pyautogui
    img = pyautogui.screenshot(region=(leftPixels, topPixels, 1000, 2))
    img = np.array(img)
    

内容的提问来源于stack exchange,提问作者Aviel Ovadiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 17:15:36