Image Contours
lastmod 2023-10-26created 2019-12-118 edits
Contours are lines connecting the boundaries of regions that have the same color or the same intensity. Familiar examples are contour lines on maps and those seen in weather forecasts.
Categories
- Convex hull - Outermost boundary detection
- Hough transform - Extracts the parts where the most lines formed by points in an image overlap. Mainly used to detect lines (Line), circles (Circle), and the like.
- Canny edge detector - Edge extraction
- Contours - Image boundary extraction.
Example
Use the cv2.findContours() and cv2.drawContours() functions of OpenCV.
#-*- coding:utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('images/rectangle.jpg')
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#threshold를 이용하여 binary image로 변환
ret, thresh = cv2.threshold(imgray,127,255,0)
#contours는 point의 list형태. 예제에서는 사각형이 하나의 contours line을 구성하기 때문에 len(contours) = 1. 값은 사각형의 꼭지점 좌표.
#hierachy는 contours line의 계층 구조
image, contours, hierachy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
image = cv2.drawContours(img, contours, -1, (0,255,0), 3)
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hierarchy
- [Recommended] OpenCV - 22. Contour - Explains the hierarchy result values for "tree hierarchy contours".
See also
Favorite site
References
-
Datascienceschool_-_f9f8983941254a34bf0fee42c66c5539.ipynb.zip ↩