輪廓發現 contours

原理contours參數說明參考文檔

 

import cv2 as cv
import numpy as np
#contour 外形 輪廓
def contours_demo(src):
    GaussianBlur=cv.GaussianBlur(src,(3,3),0)
    gray=cv.cvtColor(GaussianBlur,cv.COLOR_BGR2GRAY)
    ret,binary=cv.threshold(gray,0,255,cv.THRESH_OTSU|cv.THRESH_BINARY)
    cv.imshow('threshold_binary',binary)

    #輪廓
    contours,heriachy=cv.findContours(binary,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
    for i ,contour in enumerate(contours):
        cv.drawContours(src,contours,i,(0,0,255),2)
        #輪廓內填充
        #cv.drawContours(src, contours, i, (0, 0, 255), -1)
        print(i)
    cv.imshow("detect contours",src)
src=cv.imread('circle.png')
cv.namedWindow("yuantu",cv.WINDOW_AUTOSIZE)
cv.imshow("yuantu",src)
contours_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章