简单的图形识别python

简单的图形识别python,第1张

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 cd
4:
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)-10),cv2 .FONT_HERSHEY_COMPLEX ,0.7,
(0,255,0),2)
txphs=cv2.cvtColor(txp,cv2.COLOR_BGR2GRAY )
txpbure=cv2.GaussianBlur(txphs,(7,7),0)
txpcanny=cv2.Canny(txpbure ,200,200)
txpmt=cv2.dilate(txpcanny ,kernel,iterations= 3)
md(txpmt )
cv2.imshow(“kg888”,txp)
cv2 .imshow(“kg8888”,txphs )
cv2.imshow(“kg88888”,txpbure )
cv2.imshow(“kg8888888”,txpmt )
cv2.imshow(“kg8888888888”,ht )
cv2.waitKey(0)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/714809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存