简体   繁体   English

通过opencv Python检测图像中是否有灰色

[英]Detect if there is gray color in image by opencv Python

I'm new to cv2 library and I want to make a program that will detect if there is gray color in an image at real time.我是 cv2 库的新手,我想制作一个程序来实时检测图像中是否有灰色。 Until now I took a code that will show me the screen in real time直到现在我拿了一个代码来实时显示屏幕

import numpy as np
import cv2
import pyautogui
from mss import mss
from PIL import Image

mon = {'top': 268, 'left': 968, 'width': 931, 'height': 599}

sct = mss()

while 1:
    sct.get_pixels(mon)
    img = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
    cv2.imshow('test', np.array(img))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

But I have no idea how to detect if gray color appear in the screen(The other questions is about if all the picture is gray and not if gray appear in the screen)但我不知道如何检测屏幕上是否出现灰色(其他问题是关于是否所有图片都是灰色的,而不是屏幕上是否出现灰色)

The better way is to change the colour space into HSV and find the Hue value range for colour.更好的方法是将颜色空间更改为 HSV 并找到颜色的色调值范围。

  • Take each frame of the video拍摄视频的每一帧
  • Convert from BGR to HSV color-space从 BGR 转换为 HSV 色彩空间
  • Threshold the HSV image for a range of blue colour为一系列蓝色设置 HSV 图像的阈值

Below code is from OpenCV official site to detect the blue colour object下面的代码来自OpenCV 官方网站,用于检测蓝色对象

import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
while(1):
    # Take each frame
    _, frame = cap.read()
    # Convert BGR to HSV
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    # define range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])
    # Threshold the HSV image to get only blue colors
    mask = cv.inRange(hsv, lower_blue, upper_blue)
    # Bitwise-AND mask and original image
    res = cv.bitwise_and(frame,frame, mask= mask)
    cv.imshow('frame',frame)
    cv.imshow('mask',mask)
    cv.imshow('res',res)
    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break
cv.destroyAllWindows()

For Live screen Gray colour Detection用于实时屏幕灰度检测

the range will be in HSV is范围将在 HSV 是

lower_blue = np.array([0,0,0]) upper_blue = np.array([255,10,255]) lower_blue = np.array([0,0,0]) upper_blue = np.array([255,10,255])

In HSV/HSL colourspace, the grey pixels are characterized by having a Saturation of very close to zero.在 HSV/HSL 色彩空间中,灰色像素的特点是饱和度非常接近于零。 Then the Value channel will tell you how far along the scale from black to white they actually are, low Lightness/Value is dark grey whereas high Lightness/Value means light grey.然后 Value 通道会告诉你它们实际上从黑色到白色的比例有多远,低亮度/值是深灰色,而高亮度/值意味着浅灰色。

So for the grey colour, your code is as below所以对于灰色,你的代码如下

import numpy as np
from PIL import Image
from mss import mss
import cv2 as cv
import cv2
mon = {'top': 268, 'left': 968, 'width': 931, 'height': 599}
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([0, 0, 0])
  upper_gray = np.array([255, 10, 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)
  cv.imshow('original', img)
  cv.imshow('mask', mask)
  cv.imshow('res', res)
  k = cv.waitKey(5) & 0xFF
  if k == 27:
    break
cv.destroyAllWindows()

The following code can be used for static images以下代码可用于静态图像

import cv2
import numpy as np
image_path ="trail.jpg"
img = cv2.imread(image_path)
# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of gray color in HSV
lower_gray = np.array([0,0,0])
upper_gray = np.array([255,10,255])
# Threshold the HSV image to get only gray colors
mask = cv2.inRange(hsv, lower_gray, upper_gray)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img,img, mask= mask)
cv2.imwrite("output.png",res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM