Hi. I am new to opencv, and after reading some documentation have combined it with mss. I am trying to implement edge detection to detect objects in a game. However, I am facing an issue where the objects continuously flicker randomly. I am unsure what to do to fix it. Can someone point me in the right direction? Many thanks. Here is an attached video: (lots of flashing) Imgur: The magic of the Internet
You can see that most of the time the objects are caught correctly, yet sometimes the whole screen becomes flashbanged etc.
import time
import pygetwindow as gw
import cv2
import numpy as np
import mss
width = 1920
height = 1080
offset = 200
x1 = width // 2 - offset
y1 = height // 2 - offset
x2 = width // 2 + offset
y2 = height // 2 + offset
with mss.mss() as sct:
# Part of the screen to capture
monitor = {"top": 0, "left": 0, "width": 1920, "height": 1080}
while "Screen capturing":
last_time = time.time()
# region in middle of screen to cut out
img = np.array(sct.grab(monitor))
crop = img[y1:y2, x1:x2]
img[y1:y2, x1:x2] = [0,0,0,255]
gray = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
edges = cv2.Canny(gray,100,200)
kernel = np.ones((3,3), np.uint8)
closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
filled_img = np.zeros_like(closing)
cv2.drawContours(filled_img, contours, -1, (255), thickness=cv2.FILLED)
cv2.imshow("Filled Ships", filled_img)
print(f"fps: {1 / (time.time() - last_time)}")
# Press "q" to quit
if cv2.waitKey(1) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break