02. Study/Computer Vision(openframworks&opencv)
18.contour(윤곽,외곽선)찾기 findContours
미카이
2016. 11. 7. 22:51
참고 : http://docs.opencv.org/
예제코드 : http://docs.opencv.org/3.1.0/df/d0d/tutorial_find_contours.html
opencv : findContours 함수
->영상(이미지) 내에서 외곽선을 추출하는 함수.
함수 원형입니다.
void cv::findContours | ( | InputOutputArray | image,//원본이미지 |
OutputArrayOfArrays | contours,//외곽선 벡터(배열) | ||
OutputArray | hierarchy, | ||
int | mode,//윤곽 검색 모드 설정 | ||
int | method,//윤곽 근사방법(cv::ContourApproximationModes)참조 | ||
Point | offset = Point() //맥락표현에서 오프셋 | ||
) |
외곽선 그리는 함수
cv::drawContours(result, contours,
-1, // 모든 외곽선 그리기
cv::Scalar(0), // 검게
2); // 두께를 2로
// 세 번째 파라미터가 음수라면 모든 외곽선이 그려짐
// 반면 그려져야 하는 외곽선의 첨자를 지정할 수 있음
//실행결과입니다
//전체적인 코드입니다
#include "ofApp.h" using namespace std; ofImage photo; Mat src; Mat src_gray; int thresh = 100; int max_thresh = 255; RNG rng(12345); /// Function header void thresh_callback(int, void*); void ofApp::setup() { photo.load("3211.bmp"); src = Mat(photo.getHeight(), photo.getWidth(), CV_8UC3, photo.getPixels().getData()); cvtColor(src, src_gray, CV_BGR2GRAY); blur(src_gray, src_gray, Size2i(3, 3)); char* source_window = "Source"; namedWindow(source_window, CV_WINDOW_AUTOSIZE); imshow(source_window, src); createTrackbar(" Canny thresh:", "Source", &thresh, max_thresh, thresh_callback); thresh_callback(0, 0); waitKey(0); } //-------------------------------------------------------------- void ofApp::update() { } //-------------------------------------------------------------- void ofApp::draw() { } void thresh_callback(int, void*) { Mat canny_output; vector> contours; vector hierarchy; /// Detect edges using canny Canny(src_gray, canny_output, thresh, thresh * 2, 3); /// Find contours findContours(canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); /// Draw contours Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3); for (int i = 0; i< contours.size(); i++) { Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point2i()); } /// Show in a window namedWindow("Contours", CV_WINDOW_AUTOSIZE); imshow("Contours", drawing); }