opencv图形检测

 

import cv2
txp=cv2.imread("999.png")
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)
        if area>1500:
            cv2.drawContours(ht,cnt,-1,(255,0,0),3)
            peri=cv2.arcLength(cnt,True)
            approx=cv2.approxPolyDP(cnt,0.02*peri,True)
            cd=len(approx)
            x,y,w,h=cv2.boundingRect(approx)
            if cd==3:
                Type="Tri"
            elif cd==4:
                asp=w/float(h)
                if asp>0.95 and asp<1.05:
                    Type="squ"
                else:
                    Type="rec"
            elif cd>4:
                Type="cir"
            else:
                Type="None"
            cv2.rectangle(ht,(x,y),(x+w,y+h),(250,250,0),2)
            cv2.putText(ht,Type,(x+(w//2)-10,y+(h//2)-10),cv2.FONT_HERSHEY_COMPLEX,0.7,(255,255,0),2)
txphs=cv2.cvtColor(txp,cv2.COLOR_BGR2GRAY)
txpbure=cv2.GaussianBlur(txphs,(7,7),0)
txpcanny=cv2.Canny(txpbure,200,200)
md(txpcanny)
cv2.imshow("sb",ht)
cv2.waitKey(0)

 

opencv图形检测_第1张图片

 

你可能感兴趣的:(python)