Reputation: 31
I'm unable to change the colors so that they are the same as my natural diaply colors. They are always inverted. I have tried using different color spaces, but am not able to have it work. I want the program to capture my screen, but also be able detect certain colors on the screen
import cv2
import numpy as np
import pyautogui
def nothing(x):
pass
cv2.namedWindow("Tracking")
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing)
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing)
cv2.createTrackbar("US", "Tracking", 255, 255, nothing)
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing)
while True:
screenshot = pyautogui.screenshot()
screenshot = np.array(screenshot)
hsv = cv2.cvtColor(screenshot, cv2.COLOR_BGR2HSV) #
l_h = cv2.getTrackbarPos("LH", "Tracking")
l_s = cv2.getTrackbarPos("LS", "Tracking")
l_v = cv2.getTrackbarPos("LV", "Tracking")
u_h = cv2.getTrackbarPos("UH", "Tracking")
u_s = cv2.getTrackbarPos("US", "Tracking")
u_v = cv2.getTrackbarPos("UV", "Tracking")
l_b = np.array([l_h, l_s, l_v])
u_b = np.array([u_h, u_s, u_v])
mask = cv2.inRange(hsv, l_b, u_b)
res = cv2.bitwise_and(screenshot, screenshot, mask=mask)
# cv2.imshow("screenshot", screenshot)
cv2.imshow("mask", mask)
cv2.imshow("res", res)
key = cv2.waitKey(1)
if key == 27:
break
cv2.destroyAllWindows()
Upvotes: 3
Views: 579
Reputation: 2191
You are taking screenshot using Pyautogui which must be reading it in RGB instead of the BGR used in Opencv. So after reading the screenshot convert it to BGR.
screenshot = pyautogui.screenshot()
screenshot = np.array(screenshot)
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
Upvotes: 1