OpenCV:Contours
ROI to Contour
from numpy import array, int32, ndarray
assert data is not None
assert isinstance(data, (list, tuple))
assert len(data) == 4
x1 = data[0]
y1 = data[1]
x2 = data[2]
y2 = data[3]
points = (x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)
raw = [p for xy in points for p in xy]
contour = array(raw, dtype=int32).reshape((5, 1, 2))
assert isinstance(contour, ndarray)
assert len(contour.shape) == 3
assert contour.shape[0] == 5
assert contour.shape[1] == 1
assert contour.shape[2] == 2
Contour intersection
두 컨투어의 교차영역을 구하는 방법.
이미지에 Bit 단위로 그린 후 Locical AND 연산으로 구하는 방법
import numpy as np # just for matrix manipulation, C/C++ use cv::Mat
# find contours.
contours,h = findContours( img, mode=RETR_LIST, method=CHAIN_APPROX_SIMPLE )
# Suppose this has the contours of just the car and the obstacle.
# create an image filled with zeros, single-channel, same size as img.
blank = np.zeros( img.shape[0:2] )
# copy each of the contours (assuming there's just two) to its own image.
# Just fill with a '1'.
img1 = drawContours( blank.copy(), contours, 0, 1 )
img2 = drawContours( blank.copy(), contours, 1, 1 )
# now AND the two together
intersection = np.logical_and( img1, img2 )
# OR we could just add img1 to img2 and pick all points that sum to 2 (1+1=2):
intersection2 = (img1+img2)==2
위 방법은 Floating 단위는 사라지므로 정확하진 않다.
OpenCV Contours APIs
- Contours
- OpenCV:Contours
- cv2.approxPolyDP - 지정된 정밀도로 다각형 곡선을 근사화(단순화)합니다.
- cv2.arcLength - 윤곽 둘레 또는 곡선 길이를 계산합니다.
- cv2.boundingRect - 점 집합의 오른쪽 위 경계 사각형 또는 회색조 이미지의 0이 아닌 픽셀을 계산합니다.
- cv2.boxPoints - 회전된 직사각형의 꼭지점 4개를 찾습니다. 회전된 직사각형을 그리는 데 유용합니다.
- cv2.connectedComponents - 부울 이미지의 이미지 라벨이 붙은 연결된 구성요소를 계산합니다.
- cv2.connectedComponentsWithStats - 부울 이미지의 이미지로 표시된 연결된 구성 요소를 계산하고 각 레이블에 대한 통계 출력도 생성합니다.
- cv2.contourArea - 등고선 면적을 계산합니다.
- cv2.convexHull - 점 세트의 볼록 껍질을 찾습니다.
- cv2.convexityDefects - 윤곽선의 볼록성 결함을 찾습니다.
- cv2.createGeneralizedHoughBallard - cv2.GeneralizedHoughBallard 클래스에 대한 스마트 포인터를 생성하고 초기화합니다.
- cv2.createGeneralizedHoughGuil - cv2.GeneralizedHoughGuil 클래스에 대한 스마트 포인터를 생성하고 초기화합니다.
- cv2.drawContours - 윤곽선을 그린다.
- cv2.findContours - 바이너리 이미지에서 윤곽선을 찾습니다.
- cv2.fitEllipse - 2D 점 세트 주위에 타원을 맞춥니다.
- cv2.fitEllipseAMS - 2D 점 세트 주위에 타원을 맞춥니다.
- cv2.fitEllipseDirect - 2D 점 세트 주위에 타원을 맞춥니다.
- cv2.fitLine - 선을 2D 또는 3D 점 세트에 맞춥니다.
- cv2.HuMoments - 7개의 Hu 불변값을 계산합니다.
- cv2.intersectConvexConvex - 두 개의 볼록 다각형의 교차점을 찾습니다.
- cv2.isContourConvex - 윤곽 볼록성을 테스트합니다.
- cv2.matchShapes - 두 모양을 비교합니다.
- cv2.minAreaRect - 입력 2D 점 세트를 둘러싸는 최소 영역의 회전된 직사각형을 찾습니다.
- cv2.minEnclosingCircle - 2D 점 세트를 둘러싸는 최소 면적의 원을 찾습니다.
- cv2.minEnclosingTriangle - 2D 점 세트를 둘러싸는 최소 면적의 삼각형을 찾아 해당 면적을 반환합니다.
- cv2.moments - 다각형 또는 래스터화된 모양의 3차까지의 모든 모멘트를 계산합니다.
- cv2.pointPolygonTest - 윤곽선 내 점 테스트를 수행합니다.
- cv2.rotatedRectangleIntersection - 회전된 두 직사각형 사이에 교차점이 있는지 확인합니다.
Convex Hull
- cv2.convexHull - 주어진 점으로부터 컨벡스 헐(최외곽 점들을 연결)을 반환합니다.
- cv2.convexityDefects - 주어진 점과 컨벡스 헐로부터 컨벡스 디펙트를 반환합니다.
- cv2.isContourConvex - 컨벡스 인지를 검사합니다.