hi guys, I hope you guys can give me some advice, even if you can this point me in a direction, please.
the folwing is for a conveyor system that I am working on.
I have an output array from a captured screen with pixels (list). the output looks like this:
[5 7 4 3]
[7 4 1 2]
[5 4 7 2]
[7 6 2 1]
I need to know how can I solve this by doing the following:
[5 7 4 3]
[7 4 1 2]
[5 4 7 2]
[1 7 6 2]
the bottom row has moved to the right: lining up the 2
the catch is that it can only move in a line (vertical and horizontal), so you can only move the col up or down and the row left to right.
and how can I get the mouse to interact with this?
# Here is my code:
the folwing is for a conveyor system that I am working on.
I have an output array from a captured screen with pixels (list). the output looks like this:
[5 7 4 3]
[7 4 1 2]
[5 4 7 2]
[7 6 2 1]
I need to know how can I solve this by doing the following:
[5 7 4 3]
[7 4 1 2]
[5 4 7 2]
[1 7 6 2]
the bottom row has moved to the right: lining up the 2
the catch is that it can only move in a line (vertical and horizontal), so you can only move the col up or down and the row left to right.
and how can I get the mouse to interact with this?
# Here is my code:
import cv2
import numpy as np
import pyautogui
from PIL import ImageGrab, Image
while True:
# Screen Capture realtime
screenshot = ImageGrab.grab(bbox=(1158, 594, 1891, 1332))
screenshot = np.array(screenshot)
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
img = screenshot
rows, cols, _ = img.shape
# Creating the board
virtual_board = np.zeros((rows, cols, 3), dtype = np.uint8)
board_array = np.zeros((8, 8), dtype = int)
# Detecting the board
screenshot = cv2.rectangle(screenshot, (0, 0), (732, 736), (255, 255, 255), 3)
board_color = np.array([255, 255, 255])
board_mask = cv2.inRange(img, board_color, board_color)
contours, _ = cv2.findContours(board_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse = True)
cnt = contours[0]
(board_x, board_y, board_w, board_h) = cv2.boundingRect(cnt)
cv2.drawContours(img, [cnt], -1, (0, 255, 0), 2)
cv2.drawContours(virtual_board, [cnt], -1, (0, 255, 0), 0)
# Detecting boxes
boxes = {"1": [180, 180, 180], # White Rectangle
"2": [255, 72, 255], # Purple Round
"3": [213, 124, 255], # Pink Square
"4": [30, 137, 255], # Orange Oval
"5": [80, 116, 163], # Bronze Triangle
"6": [255, 199, 64], # Teal Rectangle
"7": [56, 255, 255], # Yellow Sharp Oval
"8": [29, 255, 154], # Green EyeDrop
"9": [164, 59, 108], # Blue Square
"10": [27, 28, 143]} # Red Harts
match_list = [(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
# Creating mask for each boxes
for key in boxes:
bgr_color = boxes[key]
bgr_color = np.array(bgr_color)
mask = cv2.inRange(img, bgr_color, bgr_color)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
cv2.rectangle(virtual_board, (x, y), (x + w, y + h),
(0, 0, 255), 2)
# Creating cells of board
block_width = int(board_w / 8)
block_height = int(board_h / 8)
for board_row in range(8):
for board_col in range(8):
block_x = block_width * board_col
block_y = block_height * board_row
cv2.rectangle(virtual_board, (board_x + block_x, board_y + block_y),
(board_x + block_x + block_width, board_y + block_y + block_height),
(255, 255, 255), 1)
# Check if boxes is inside the cells
if board_x + block_x <= x < board_x + block_x + block_width and \
board_y + block_y <= y < board_y + block_y + block_height:
board_array[board_row][board_col] = key
