영상의 이진화
- 영상의 픽셀 값을 0 또는 255(1)로 만드는 연산
그레이스케일 영상 이진화
- 임계값 연산 사용
double threshold( InputArray src, OutputArray dst,
double thresh, double maxval, int type );
자동 임계값 결정법
- Otsu 이진화 방법
- 입력 영상이 배경과 객체 두 개로 구성되어 있다고 가정
- 임의의 임계값 T에 의해 나워지는 두 픽셀 분포 그룹의 분산이 최소가 되는 T를 선택
- 일종의 최적화 알고리즘
- threshhold() 함수의 tyoe 인자에 THRESH_OTSU 지정
지역 이진화
전역 이진화: 영상 전체에 대해 동일한 임계값을 사용하여 이진화를 수행하는 기법
균일하지 않은 조명의 영향을 해결 -> 지역 이진화
지역 이진화
- 픽셀 또는 영역마다 다른 임계값을 사용하여 이진화를 수행하는 기법
- 보통 영상을 특정 크기 영역으로 분할하여 이진화를 수행하거나 또는 각 픽셀 근방에 윈도우를 설정하고 해당 위도우에서 임계값을 결정하여 이진화를 수행
int bw = src.cols / 4;
int bh = src.rows / 4;
Mat dst2 = Mat::zeros(src.rows, src.cols, CV_8UC1);
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
Mat src_ = src(Rect(x*bw, y*bh, bw, bh));
Mat dst_ = dst2(Rect(x*bw, y*bh, bw, bh));
threshold(src_, dst_, 0, 255, THRESH_BINARY | THRESH_OTSU);
}
}
OpenCV 적응형 이진화 함수
void adaptiveThreshold( InputArray src, OutputArray dst,
double maxValue, int adaptiveMethod,
int thresholdType, int blockSize, double C );
모폴로지
모폴로지 연산
- 영상을 형태학적인 측면에서 다루는 기법
- 다양한 영상 처리 시스템에서 전처리또는 후처리 형태로 널리 사용
침식과 팽창
침식
- 구조요소가 객체 영역 내부에 완전히 포함될 경우 고정점 픽셀을 255로 설정
- 객체 외곽을 깍아내는 연산 -> 객체의 크기는 줄어들고 배경은 확대
팽창 =
- 구조 요소와 객체 영역이 한 픽셀이라도 만날 경우 고정점 픽셀을 255로 설정
- 팽창 연산은 객체 외곽을 확대시키는 연산
// 침식 연산
void erode( InputArray src, OutputArray dst, InputArray kernel,
Point anchor = Point(-1,-1), int iterations = 1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue() );
// 팽창 연산
void dilate( InputArray src, OutputArray dst, InputArray kernel,
Point anchor = Point(-1,-1), int iterations = 1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue() );
// 구조 요소 생성
Mat getStructuringElement(int shape, Size ksize, Point anchor = Point(-1,-1));
열기 연산: 침식 -> 팽창
닫기 연산: 팽창 -> 침식
// 범용 모폴로지 연산 함수
void morphologyEx( InputArray src, OutputArray dst,
int op, InputArray kernel,
Point anchor = Point(-1,-1), int iterations = 1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue() );
래이블링과 외곽선 검출
객체 단위 분석
- 객체를 분할하여 특징을 분석
- 레이블링 & 외곽선 검출
레이블링
- 서로 연결되어 있는 객체 픽셀에 고유한 번호 지정
- 영역 기반 모양 분석
int connectedComponentsWithStats(InputArray image, OutputArray labels,
OutputArray stats, OutputArray centroids,
int connectivity = 8, int ltype = CV_32S);
외곽선 검출
- 각 객체의 외곽선 좌표를 모두 검출
- 외곽선 기반 모양 분석
void findContours( InputArray image, OutputArrayOfArrays contours,
OutputArray hierarchy, int mode,
int method, Point offset = Point());
void findContours( InputArray image, OutputArrayOfArrays contours,
int mode, int method, Point offset = Point());
void drawContours( InputOutputArray image, InputArrayOfArrays contours,
int contourIdx, const Scalar& color,
int thickness = 1, int lineType = LINE_8,
InputArray hierarchy = noArray(),
int maxLevel = INT_MAX, Point offset = Point() );
OpenCV 외곽선 관련 함수
https://docs.opencv.org/4.3.0/dd/d49/tutorial_py_contour_features.html
OpenCV: Contour Features
Goal In this article, we will learn To find the different features of contours, like area, perimeter, centroid, bounding box etc You will see plenty of functions related to contours. 1. Moments Image moments help you to calculate some features like center
docs.opencv.org
'컴퓨터 비전' 카테고리의 다른 글
OpenCV 영상의 기하학적 변환 (0) | 2023.10.30 |
---|---|
OpenCV 영상 분할과 객체 검출 (0) | 2023.10.27 |
OpenCV 영상의 특징 추출 (0) | 2023.10.25 |
OpenCV 컬러 영상 처리 (1) | 2023.10.24 |
OpenCV 특정점 검출과 매칭 (1) | 2023.10.23 |