import cv2
import numpy as np
kernel=np.ones ((5,5),np.uint8)
txp=cv2.imread("tx.jfif ")改图片,图片要自己导到项目
ht=txp.copy()
def md(txp):
yu,oi=cv2.findContours(txp,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS )
for cnt in yu:
area=cv2.contourArea(cnt)
print(area)
if area >500:
cv2.drawContours(ht,cnt ,-1,(255,0,0),3)
peri=cv2 .arcLength(cnt ,True )
print(peri)
approx=cv2.approxPolyDP(cnt,0.02*peri ,True )
print(len (approx ))
cd=len(approx )
x,y,w,h=cv2 .boundingRect(approx )
if cd3:Type=“Tri”
elif cd4:
asp=w/float (h )
if asp>0.95and asp<1.05:Type =“square”
else: Type =“Rectangle”
elif cd>4:Type =“Circles”
else: Type =“None”
cv2.rectangle(ht,(x,y),(x+w,y+h),(0,250,0),2)
cv2.putText(ht ,Type ,
(x+(w//2)-10,y+(h//2)-1
简单的图形识别python
最新推荐文章于 2024-07-31 14:30:10 发布