Opencv學習筆記--使用convexityDefects計算輪廓凸缺陷

     首先介紹今天主角:void convexityDefects(InputArray contour, InputArray convexhull, OutputArray convexityDefects)

     使用時注意,最後一個參數 convexityDefects 是存儲 Vec4i 的向量(vector<varname>),函數計算成功後向量的大小是輪廓凸缺陷的數量,向量每個元素Vec4i存儲了4個整型數據,因爲Vec4i對[]實現了重載,所以可以使用 _vectername[i][0] 來訪問向量 _vactername 的第i個元素的第一個分量。再說 Vec4i 中存儲的四個整形數據,Opencv 使用這四個元素表示凸缺陷,第一個名字叫做  start_index,表示缺陷在輪廓上的開始處,他的值是開始點在函數第一個參數 contour 中的下標索引;Vec4i 第二個元素的名字叫 end_index, 顧名思義其對應的值就是缺陷結束處在 contour 中的下標索引; Vec4i 第三個元素  farthest_pt_index 是缺陷上距離 輪廓凸包(convexhull)最遠的點;Vec4i最後的元素叫 fixpt_depthfixpt_depth/256  表示了 輪廓上以 farthest_pt_index 爲下標的點到 輪廓凸包的(convexhull)的距離,以像素爲單位。

     All is so easy!下面就是簡單的代碼示例(首先計算兩個輪廓的凸包,然後計算兩個輪廓的凸缺陷):


// 計算凸缺陷 convexityDefect
//

#include "stdafx.h"
#include <opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{
	Mat *img_01 = new Mat(400, 400, CV_8UC3);
	Mat *img_02 = new Mat(400, 400, CV_8UC3);
	*img_01 = Scalar::all(0);
	*img_02 = Scalar::all(0);
	// 輪廓點組成的數組
	vector<Point> points_01,points_02;


	// 給輪廓組賦值
	points_01.push_back(Point(10, 10));points_01.push_back(Point(10,390));
	points_01.push_back(Point(390, 390));points_01.push_back(Point(150, 250));
	points_02.push_back(Point(10, 10));points_02.push_back(Point(10,390));
	points_02.push_back(Point(390, 390));points_02.push_back(Point(250, 150));

	vector<int> hull_01,hull_02;
	// 計算凸包
	convexHull(points_01, hull_01, true);
	convexHull(points_02, hull_02, true);

	// 繪製輪廓
	for(int i=0;i < 4;++i)
	{
		circle(*img_01, points_01[i], 3, Scalar(0,255,255), CV_FILLED, CV_AA);
		circle(*img_02, points_02[i], 3, Scalar(0,255,255), CV_FILLED, CV_AA);
	}
	// 繪製凸包輪廓
	CvPoint poi_01 = points_01[hull_01[hull_01.size()-1]];
	for(int i=0;i < hull_01.size();++i)
	{
		line(*img_01, poi_01, points_01[i], Scalar(255,255,0), 1, CV_AA);
		poi_01 = points_01[i];
	}
	CvPoint poi_02 = points_02[hull_02[hull_02.size()-1]];
	for(int i=0;i < hull_02.size();++i)
	{
		line(*img_02, poi_02, points_02[i], Scalar(255,255,0), 1, CV_AA);
		poi_02 = points_02[i];
	}
	
	vector<Vec4i> defects;
	// 如果有凸缺陷就把它畫出來
	if( isContourConvex(points_01) )
	{
		cout<<"img_01的輪廓是凸包"<<endl;
	}else{
		cout<<"img_01的輪廓不是凸包"<<endl;
		convexityDefects(
			points_01,
			Mat(hull_01),
			defects
			);
		// 繪製缺陷
		cout<<"共"<<defects.size()<<"處缺陷"<<endl;
		for(int i=0;i < defects.size();++i)
		{
			circle(*img_01, points_01[defects[i][0]], 6, Scalar(255,0,0), 2, CV_AA);
			circle(*img_01, points_01[defects[i][1]], 6, Scalar(255,0,0), 2, CV_AA);
			circle(*img_01, points_01[defects[i][2]], 6, Scalar(255,0,0), 2, CV_AA);
			line(*img_01, points_01[defects[i][0]], points_01[defects[i][1]], Scalar(255,0,0), 1, CV_AA);
			line(*img_01, points_01[defects[i][1]], points_01[defects[i][2]], Scalar(255,0,0), 1, CV_AA);
			line(*img_01, points_01[defects[i][2]], points_01[defects[i][0]], Scalar(255,0,0), 1, CV_AA);
			cout<<"第"<<i<<"缺陷<"<<points_01[defects[i][0]].x<<","<<points_01[defects[i][0]].y
				<<">,<"<<points_01[defects[i][1]].x<<","<<points_01[defects[i][1]].y
				<<">,<"<<points_01[defects[i][2]].x<<","<<points_01[defects[i][2]].y<<">到輪廓的距離爲:"<<defects[i][3]/256<<"px"<<endl;
		}
		defects.clear();
	}
	if( isContourConvex( points_02 ) )
	{
		cout<<"img_02的輪廓是凸包"<<endl;
	}else{
		cout<<"img_02的輪廓不是凸包"<<endl;
		vector<Vec4i> defects;
		convexityDefects(
			points_01,
			Mat(hull_01),
			defects
			);
		// 繪製出缺陷的輪廓
		for(int i=0;i < defects.size();++i)
		{
			circle(*img_02, points_01[defects[i][0]], 6, Scalar(255,0,0), 2, CV_AA);
			circle(*img_02, points_01[defects[i][1]], 6, Scalar(255,0,0), 2, CV_AA);
			circle(*img_02, points_01[defects[i][2]], 6, Scalar(255,0,0), 2, CV_AA);
			line(*img_02, points_01[defects[i][0]], points_01[defects[i][1]], Scalar(255,0,0), 1, CV_AA);
			line(*img_02, points_01[defects[i][1]], points_01[defects[i][2]], Scalar(255,0,0), 1, CV_AA);
			line(*img_02, points_01[defects[i][2]], points_01[defects[i][0]], Scalar(255,0,0), 1, CV_AA);
			// 因爲 img_02 沒有缺陷所以就懶的寫那些輸出代碼了
		}
		defects.clear();
	}

	imshow("img_01 的輪廓和凸包:", *img_01);
	imshow("img_02 的輪廓和凸包:", *img_02);
	cvWaitKey();
	
	return 0;
}



發佈了31 篇原創文章 · 獲贊 18 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章