본문 바로가기
OpenCV

[OpenCV] contours.py

by Lizardee 2024. 4. 10.
Contour Detection

import cv2 as cv
import numpy as np

# Reading an image
img = cv.imread('Photos/cats.jpg')
cv.imshow('Cats', img)

# Blank image
blank = np.zeros(img.shape, dtype='uint8')
cv.imshow('Blank', blank)

# Converting to grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

# Blur
blur = cv.GaussianBlur(gray, (5,5), cv.BORDER_DEFAULT)
cv.imshow('Blur', blur)

# Edge cascade
canny = cv.Canny(blur, 125, 175)
cv.imshow('Canny Edges', canny)



# Contours Detection

# 1. Threshhold: Convert an image to a binary form
ret, thresh = cv.threshold(gray, 125, 255, cv.THRESH_BINARY)
cv.imshow('Thresh', thresh)


# 2. Contours
contours, hierarchies = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
print(f'{len(contours)} contour(s) found!')

# Draw contours on the blank image
cv.drawContours(blank, contours, -1, (255,125,0), 1)
cv.imshow('Coutours Drawn', blank)


cv.waitKey(0)

'OpenCV' 카테고리의 다른 글

[OpenCV] smoothing.py: Blurring Techniques  (0) 2024.04.11
[OpenCV] splitmerge.py: Color Channels  (0) 2024.04.10
[OpenCV] transformations.py  (0) 2024.04.10
[OpenCV] basic.py  (0) 2024.04.10
[OpenCV] draw.py  (0) 2024.04.10