When will you grow up?

19.convexHull 본문

02. Study/Computer Vision(openframworks&opencv)

19.convexHull

미카이 2016. 11. 7. 23:10

//참고 : http://docs.opencv.org/

Convex Hull이란, 여러 개의 점이 주어졌을 때, 모든 점들을 포함하는 최소 크기의 볼록 다각형입니다

 

(

InputArray points, //벡터 또는 Mat형식의 입력
OutputArray hull,//출력 convexhull(볼록 선체)
bool clockwise = false

,//방향 플래그  true면 시계방향


bool returnPoints = true

 // 연산 플래그 Mat일 경우 볼록 선체점 반환 아니면 인덱스 반환



)




결과



전체코드


#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, COLOR_BGR2GRAY);
	blur(src_gray, src_gray, Size(3, 3));
	const char* source_window = "Source";
	namedWindow(source_window, WINDOW_AUTOSIZE);
	imshow(source_window, src);
	
	// Convex Hull implementation
	Mat src_copy = src.clone();
	Mat threshold_output;
	vector > contours;
	vector hierarchy;

	// Find contours
	threshold(src_gray, threshold_output, 200, 255, THRESH_BINARY);
	findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

	// Find the convex hull object for each contour
	vector >hull(contours.size());
	for (int i = 0; i < contours.size(); i++)
	{
		convexHull(Mat(contours[i]), hull[i], false);
	}

	// Draw contours + hull results
	RNG rng;
	Mat drawing = Mat::zeros(threshold_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, 1, 8, vector(), 0, Point());
		drawContours(drawing, hull, i, color, 1, 8, vector(), 0, Point());
	}

	// Show in a window
	namedWindow("Hull demo", CV_WINDOW_AUTOSIZE);
	imshow("Hull demo", drawing);

	waitKey(0);
	
	
}


//--------------------------------------------------------------
void ofApp::update() {

}

//--------------------------------------------------------------
void ofApp::draw() {

}

void thresh_callback(int, void*)
{
	Mat src_copy = src.clone();
	Mat threshold_output;
	vector > contours;
	vector hierarchy;
	threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY);
	findContours(threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
	vector >hull(contours.size());
	for (size_t i = 0; i < contours.size(); i++)
	{
		convexHull(Mat(contours[i]), hull[i], false);
	}
	Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
	for (size_t i = 0; i< contours.size(); i++)
	{
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		drawContours(drawing, contours, (int)i, color, 1, 8, vector(), 0, Point());
		drawContours(drawing, hull, (int)i, color, 1, 8, vector(), 0, Point());
	}
	namedWindow("Hull demo", WINDOW_AUTOSIZE);
	imshow("Hull demo", drawing);
}


Comments