i was trying to find rectangles the size i choose. using the box width and box height in the code but i find all sorts of rectangle sizes.
i have my test pattern image. to see it when you run the program, first have two monitors, second run the program and move the pop up window to the monitor the program is not showing in the pop up window.
my program does not detect rectangles that are divided with lines and have random letters and numbers touching it.
that is my first problem in the test pattern this is the top picture.
the second problem is the bottom image in the test picture, that is i get too many results in the one image when i tweak the canny and approxPolyDP numerical values to allow for more results. but i'm just trying to find the rectangles the size i want.
here is my code and my test picture:
to use the test picture run the program and video the test picture through the window that is showing video of your desktop.
here is my code, its in python 3:
import numpy as np
import cv2
from mss import mss
from PIL import Image
import imutils
sct = mss()
BOX_WIDTH = 235
BOX_HEIGHT = 10
while 1:
w, h = 240, 640
monitor = {'top': 100, 'left': 900, 'width': w, 'height': h}
img = Image.frombytes('RGB', (w, h), sct.grab(monitor).rgb)
image_data = np.asarray(img)
# Convert the image to grayscale
gray = cv2.cvtColor(image_data, cv2.COLOR_BGR2GRAY)
# Perform Canny edge detection
edges = cv2.Canny(gray, 600900, 150) # i don't know what these values should be? so i used 900, 150 which makes less results
# Find contours in the edges image
hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = imutils.grab_contours(hierarchy)
# Iterate over each contour
for contour in contours:
# Approximate the contour to a polygon
polygon = cv2.approxPolyDP(contour, 0.01004 * cv2.arcLength(contour, True), True) # detects if its a square or rectangle
# Check if the polygon has 4 sides
if len(polygon) <= 11:
# Draw the rectangle on the image
x, y, w, h = cv2.boundingRect(contour) # changed polygon to contour
ratio = float(w) / h
length = cv2.arcLength(contour, True)
half_width = BOX_WIDTH // 2
lift_height = BOX_HEIGHT // 6
if (length <= 950) and (length >= 100):
if not ((ratio >= 0.99) and (ratio <= 1.0)):
cv2.rectangle(image_data, (x, y), (x + half_width, y + BOX_HEIGHT - lift_height), (0, 0, 255), 2) # draws green box around rectangle
else:
cv2.rectangle(image_data, (x, y), (x + half_width, y + BOX_HEIGHT - lift_height), (0, 0, 255), 2) # draws green box around square
# Show the result
cv2.imshow('test', image_data)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
to fix my first problem, i have tried looking at number 4 in the list in the line its called "4. Contour Approximation", here is the link to the documentation:
https://docs.opencv.org/4.x/dd/d49/tutorial_py_contour_features.html
i tried some test code and it didn't fix it. i have since lost the test code.
for my second problem i have tried adjusting the values in my code. the canny, the approxPolyDP, the len(polygon).
edit. i fixed my problem i put the good code in the first post, edit i found out how i can post the answer so i undo edit and put old bad code in this post again. now i go make the answer reply.