OpenCV imshow函数未响应[重复]

rkue9o1l  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(201)

此问题已在此处有答案

cv2.imshow() function is opening a window that always says not responding - python opencv(11个回答)
8个月前关闭。
我尝试真实的检测屏幕上的颜色,当我使用imshow功能时,它没有响应,我无法看到我的屏幕实时有人可以帮助吗?

import numpy as np
from PIL import Image
from mss import mss
import cv2 as cv
import pyautogui
from pynput.keyboard import Key, Controller
import time
import sys
import keyboard as detectClick

topPixels = 880
leftPixels = 200

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

while 1:
    # Take each
    sct.get_pixels(mon)
    img = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
    img = np.array(img)

    # Convert RGB to HSV
    hsv = cv.cvtColor(img, cv.COLOR_RGB2HSV)
    # define range of gray color in HSV

    lower_gray = np.array([16, 100, 20])
    upper_gray = np.array([25, 255, 255])

    # Threshold the HSV image to get only gray colors
    mask = cv.inRange(hsv, lower_gray, upper_gray)

    # Bitwise-AND mask and original image
    res = cv.bitwise_and(img, img, mask=mask)

    coord = cv.findNonZero(mask)
    if coord is None:
        print("No coords")
    else:
        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')
        time.sleep(1.5)
        
    cv.imshow('original', img)
    cv.imshow('mask', mask)
    cv.imshow('res', res)
cv.destroyAllWindows()

注意:我使用的是mss版本2。0.22,因为我必须使用它的OpenCV。如果有另一种方法来检测颜色实时视频我会很高兴听到

dxxyhpgq

dxxyhpgq1#

使用这个:

...
cv.imshow('original', img)
cv.imshow('mask', mask)
cv.imshow('res', res)
cv2.waitkey(0)
...

相关问题