如何增强python形状检测

2024-04-26 05:37:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个代码,它应该从我们的机器人飞行软件中截取一个屏幕截图,计算屏幕上的形状数,并显示数字。目前,当代码传递形状的完美图像时,我可以识别和计算形状。当我们试图通过机器人识别形状时,形状会倾斜、变暗或变大。我们需要能够识别形状,而不考虑这些缺陷

完美的图像,完美的工作: Image

无法识别的机器人图像示例: Image

我们尝试添加更多的遮罩和图层,但没有任何改变


import numpy as np
import pyautogui
import cv2
import tkinter as tk
from PIL import Image, ImageDraw, ImageFont

######################################################################
pyautogui.screenshot('image.jpg')

font = cv2.FONT_HERSHEY_COMPLEX

tri = 0
rect = 0
line = 0
circ = 0

img = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)

#img = img[700:1800, 1000:3300]

_, threshold = cv2.threshold(img, 240, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
    cv2.drawContours(img, [approx], 0, (0))
    x = approx.ravel()[0]
    y = approx.ravel()[1]
    area = cv2.contourArea(cnt)
    if len(approx) == 3:
        if(50000 > area > 5000):
            cv2.putText(img, str(area), (x, y), font, 1, (0))
            tri += 1
    elif len(approx) == 4:
        if (50000 > area > 5000):
            cv2.putText(img, str(area), (x, y), font, 1, (0))
            rect += 1
    elif len(approx) == 5:
        if (50000 > area > 5000):
            cv2.putText(img, str(area), (x, y), font, 1, (0))
            tri += 1
    elif 6 < len(approx) < 15:
        if (50000 > area > 5000):
            cv2.putText(img, str(area), (x, y), font, 1, (0))
            circ += 1
    else:
        if (4000 > area > 1300):
            cv2.putText(img, str(area), (x, y), font, 1, (0))
            line += 1

cv2.imshow("shapes", img)
##cv2.imshow("Threshold", threshold)

im = Image.new("RGB", (512,512), (255,255,255))
fnt = ImageFont.truetype('arial.ttf', 40)
d = ImageDraw.Draw(im)

d.polygon((60,10,10,110,110,110),(255,0,0))
d.rectangle((10,120,110,220),(255,0,0))
d.line((60,230,60,330),(255,0,0),20)
d.ellipse((10,340,110,440),(255,0,0))
shapes = [tri, rect, line, circ]
i=0
while i<4:
    d.text((200,i*110+50), str(shapes[i]) , font=fnt, fill=(255,0,0))
    i+=1

im.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

我们需要一个增强,可以使代码更可靠,能够检测形状,而不管这些缺陷


Tags: imageimportimgthresholdiflineareacv2