неділя, 11 квітня 2021 р.

Виділення контурів на зображенні

Mathedemo Нанесення всіх контурів
 
def find_draw(img):
    if len(img.shape)==3:
        img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        copy=img.copy()
    else:
        img_gray=img
        copy=cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
    threshold1=100
    threshold12=200
    edges=cv2.Canny(img_gray,threshold1,threshold12,apertureSize=5)
    contours, hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    img_countours=cv2.drawContours(copy, contours, -1, (0, 255, 0), 1)
    return  img_countours
Кількість контурів
 
len(contours)
Для нанесення конкретного контуру треба замість $-1$ вказати його індекс в масиві contours.

Знаходження контурів без нанесеня на зображення

 
def find_cont(img):
    if len(img.shape)==3:
        img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    else:
        img_gray=img
    threshold1=1000
    threshold12=2000
    edges=cv2.Canny(img_gray,threshold1,threshold12,apertureSize=5)
    contours = cv2.findContours(edges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]
    return contours
   

Немає коментарів:

Дописати коментар