0
点赞
收藏
分享

微信扫一扫

OpenCV图像凸包(15)

凸包

凸包指如果在集合A内连接任意两个点的直线段都在A的内部,则称集合A是凸形的。简单点理解,就是一个多边型,没有凹的地方。凸包(凸壳)能包含点集中所有的点,凸包检测常应用在物体识别、手势识别及边界检测等领域。一个轮廓可以有无数个包围它的外壳,而其中表面积最小的一个外壳,就是凸包。

凸包绘制步骤

  • 图像灰度处理
  • 灰度图二值化处理
  • 轮廓检测得到候选点
  • 凸包API调用,筛选可用点
  • 绘制显示

API介绍

void convexHull( InputArray points, OutputArray hull,bool clockwise = false, bool returnPoints = true );
/*******************************************************************
*			points: 			轮廓点集				
*			hull:	    		凸包点集输出	
*			clockwise:			凸包方向的标志位
*									true:顺时针方向
*								    false:逆时针
*			returnPoints:		返回点个数
*********************************************************************/

综合代码

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
class ConvexHull 
{
public:
	ConvexHull() :img(imread("hand.jpg")) 
	{
		result["img"] = img;
	}
	void TestConvexHull()
	{
		//图像灰度处理
		cvtColor(result["img"], result["Gray"], COLOR_BGR2GRAY);
		//灰度图二值化处理
		blur(result["Gray"], result["Gray"], Size(3, 3));
		threshold(result["Gray"], result["Binary"], 240, 255, THRESH_BINARY_INV);
		//轮廓检测得到候选点
		vector<vector<Point>> contours;
		vector<Vec4i> hierarchy;
		findContours(result["Binary"], contours, hierarchy, RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
		//凸包API调用,筛选可用点
		//绘制显示
		for (int i = 0; i < contours.size(); i++) 
		{
			vector<Point> hull;
			convexHull(contours[i], hull);
			//bool isOk = isContourConvex(contours[i]);  //判断当前点是否为有效点
			int count = hull.size();
			for (int j = 0; j < count; j++) 
			{
				circle(img, hull[j], 5, Scalar(0, 0, 255));
				line(img, hull[j % count], hull[(j + 1) % count], Scalar(255, 0, 0), 2);
			}
		}
	}
	void Show() 
	{
		for (auto& v : result) 
		{
			imshow(v.first, v.second);
		}
		waitKey(0);
	}
private:
	Mat img;
	map<string, Mat> result;
};


int main() 
{
	ConvexHull* p = new ConvexHull;
	p->TestConvexHull();
	p->Show();
	return 0;
}

OpenCV图像凸包(15)_插值

OpenCV图像几何变换

缩放

  • 放大:填充新像素
  • 缩小:丢弃部分像素

void resize( InputArray src, OutputArray dst,Size dsize, double fx = 0, double fy = 0,int interpolation = INTER_LINEAR );
/*******************************************************************
*			src: 			输入图				
*			dst:	        输出图
*			dsize:			尺寸
*			fx:				x上的比例因子
*			fy:		    	y上的比例因子
*			interpolation:  插值算法
*********************************************************************/
//interpolation取值
enum InterpolationFlags{
    INTER_NEAREST        = 0,	//最近邻插值
    INTER_LINEAR         = 1,	//双线性插值
    INTER_CUBIC          = 2,	//双三次插值
    INTER_AREA           = 3,	//像素面积关系重采样
    INTER_LANCZOS4       = 4,	//8x8 邻域的 Lanczos 插值
    INTER_LINEAR_EXACT   = 5,	//位精确双线性插值
    INTER_NEAREST_EXACT  = 6,	//位精确最近邻插值
    WARP_FILL_OUTLIERS   = 8,	//填充所有目标图像像素
    WARP_INVERSE_MAP     = 16	//标志,逆变换
};

翻转

  • 水平翻转
  • 垂直翻转
  • 先水平在垂直翻转

void flip(InputArray src, OutputArray dst, int flipCode);
/*******************************************************************
*			src: 			输入图		
*			dst:	   		输出图
*			flipCode:		翻转方式
*********************************************************************/
//flipCode
//0:垂直
//1:水平反转
//-1: 先垂直 在水平

旋转

  • 顺时针旋转90度
  • 顺时针旋转180度
  • 顺时针旋转270度

void rotate(InputArray src, OutputArray dst, int rotateCode);
/*******************************************************************
*			src: 			输入图		
*			dst:	   		输出图
*			rotateCode:		旋转方式
*********************************************************************/
//rotateCode
enum RotateFlags {
    ROTATE_90_CLOCKWISE = 0, 		// 90
    ROTATE_180 = 1, 		 		// 180
    ROTATE_90_COUNTERCLOCKWISE = 2, // 270
};

仿射

  • 一个任意的仿射变换都可以表示为:乘以一个矩阵(线性变换),加上一个向量(平移)
  • 仿射变换可以用来表示的操作有:旋转(线性变换),平移(向量加),缩放操作(线性变换)

void warpAffine( InputArray src, OutputArray dst,InputArray M, Size dsize,int flags = INTER_LINEAR,int borderMode = BORDER_CONSTANT,const Scalar& borderValue = Scalar());
/*******************************************************************
*			src: 			输入图			
*			dst:	    	输出图	
*			M:				变换矩阵
*			dsize:			输出图大小
*			flags:		    插值方式
*			borderMode:		边界模式
*			borderValue:	边界值
*********************************************************************/
//变换矩阵获取
//1.旋转的变换矩阵获取
Mat getRotationMatrix2D(Point2f center, double angle, double scale);
/*******************************************************************
*			center: 		旋转中心坐标			
*			angle:	    	角度	
*			scale:			缩放
*********************************************************************/
//2.变形的变换矩阵获取
Mat getAffineTransform( const Point2f src[], const Point2f dst[] );
/*******************************************************************
*			src: 		原图点集			
*			dst:	    目标点集		
*********************************************************************/

透视变换

透视变换在图像还原的上的应用很广泛,他是将成像投影到一个新的视平面。比如两个摄像头在不同的角度对统一物体进行拍照,物体上的同一个点在两张照片上的坐标是不一样的,为了实现两张图片同一个点的对应关系映射,透视变换就实现了此功能

void warpPerspective( InputArray src, OutputArray dst,InputArray M, Size dsize,int flags = INTER_LINEAR,int borderMode = BORDER_CONSTANT, const Scalar& borderValue = Scalar());
/*******************************************************************
*			src: 			输入图			
*			dst:	    	输出图	
*			M:				变换矩阵
*			dsize:			输出图大小
*			flags:		    插值方式
*			borderMode:		边界模式
*			borderValue:	边界值
*********************************************************************/
Mat getPerspectiveTransform(const Point2f src[], const Point2f dst[], int solveMethod = DECOMP_LU);
/*******************************************************************
*			src: 			原图点集			
*			dst:	    	目标点集
*			solveMethod:	算法方式
*********************************************************************/
enum DecompTypes {
    DECOMP_LU       = 0,	//高斯消元法
    DECOMP_SVD      = 1,	//奇异值分解(SVD)法
    DECOMP_EIG      = 2,	//特征值分解法
    DECOMP_CHOLESKY = 3,	//乔列斯基分解法
    DECOMP_QR       = 4,	//QR分解法
    DECOMP_NORMAL   = 16	//正态法
};

综合代码

#include <string>
#include <iostream>
#include <map>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
class Geometry
{
public:
	Geometry() :img(imread("geometry.jpg"))
	{
		result["img"] = img;
	}
	void ReSize() 
	{
		int width = img.cols;
		int height = img.rows;
		resize(img, result["max1"], Size(width * 2, height * 2), 0, 0, INTER_NEAREST);
		resize(img, result["min"], Size(width / 2, height / 2), 0, 0, INTER_NEAREST);
	}
	void Flip() 
	{
		flip(img, result["垂直"], 0);
		flip(img, result["水平"], 1);
		flip(img, result["垂水"], -1);
	}
	void Rotate() 
	{
		rotate(img, result["90"], 0);
		rotate(img, result["180"], ROTATE_180);
		rotate(img, result["270"], 2);
	}
	void WarpAffine() 
	{
		//旋转:45
		double angle = 45;
		Point2f center(img.rows / 2.0, img.cols / 2.0);
		Mat rmat = getRotationMatrix2D(center, angle, 1);
		warpAffine(img, result["放射旋转"], rmat, img.size());
		//自定义变化
		Point2f src[3];
		Point2f dst[3];

		src[0] = Point2f(0, 0);
		src[1] = Point(0, img.cols);
		src[2] = Point(img.rows, img.cols);

		dst[0] = Point2f(10, 10);
		dst[1] = Point2f(50, img.cols / 2.0);
		dst[2] = Point2f(img.rows / 2.0, img.cols / 2.0);

		Mat rmat2 = getAffineTransform(src, dst);
		warpAffine(img, result["自由放射"], rmat2, img.size());
	}
	void WarpPerspective() 
	{
		Point2f src[4];
		Point2f dst[4];
		src[0] = Point2f(0, 0);
		src[1] = Point2f(0, img.cols);
		src[2] = Point2f(img.rows, 0);
		src[3] = Point2f(img.rows, img.cols);

		dst[0] = Point2f(10, 10);
		dst[1] = Point2f(50, img.cols);
		dst[2] = Point2f(img.rows, 50);
		dst[3] = Point2f(img.rows, img.cols);

		Mat rmat = getPerspectiveTransform(src, dst);
		warpPerspective(img,result["透视变换"], rmat, img.size()*2);
	}
	void Show() 
	{
		int i = 0;
		for (auto& v : result) 
		{
			imshow(v.first, v.second);
			moveWindow(v.first, 50 * i++, 100);
		}
		waitKey(0);
	}
	
protected:
	Mat img;
	map<string, Mat> result;
};
int main() 
{
	unique_ptr<Geometry> p(new Geometry);
	p->ReSize();
	p->Flip();
	p->Rotate();
	p->WarpAffine();
	p->WarpPerspective();
	p->Show();
	return 0;
}

OpenCV图像凸包(15)_点集_02

举报

相关推荐

0 条评论