基于c++/opencv实现的机器视觉标准函数,可以快速应用到qt/MFC等框架

文章目录

  • 前言
  • 功能模块
    • 1.绘制矩形函数
    • 2.opencv图像上捕捉鼠标事件
    • 3.锚点绘制
    • 4.绘制矩形区域
    • 5.仿射变换


前言

此标准函数我是使用dll进行封装,可以快速使用,当然这里放的是源码,封装dll仅仅是给使用者的一个思路


功能模块

1.绘制矩形函数

void DrawRectangle(cv::Mat& img, cv::Rect box)
{
	if(img.channels() == 1)
		rectangle(img, box.tl(), box.br(), cv::Scalar(g_rng.uniform(0, 255)));
	else
		rectangle(img, box.tl(), box.br(), cv::Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)));
}

2.opencv图像上捕捉鼠标事件

void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
	cv::Mat& image = *(cv::Mat*)param;
	switch (event)
	{
	case cv::EVENT_MOUSEMOVE:
	{
		if (g_bDrawingBox)
		{
			g_rectangle.width = x - g_rectangle.x;
			g_rectangle.height = y - g_rectangle.y;
		}
	}
	break;

	case cv::EVENT_LBUTTONDOWN:
	{
		g_bDrawingBox = true;
		g_rectangle = cv::Rect(x, y, 0, 0);//记录起始点
	}
	break;

	case cv::EVENT_LBUTTONUP:
	{
		g_bDrawingBox = false;
		if (g_rectangle.width < 0)
		{
			g_rectangle.x += g_rectangle.width;
			g_rectangle.width *= -1;
		}
		if (g_rectangle.height < 0)
		{
			g_rectangle.y += g_rectangle.height;
			g_rectangle.height *= -1;
		}
		DrawRectangle(image, g_rectangle);
	}
	break;
	}
}

3.锚点绘制

cv::Point recent_Point = cv::Point(0,0);
void DrawTwoline(cv::Mat& img, cv::Point p,int length,cv::Scalar color)
{
	cv::line(img,cvPoint(p.x,p.y-length),cvPoint(p.x,p.y+length),color,1,CV_AA,0);
	cv::line(img,cvPoint(p.x-length,p.y),cvPoint(p.x+length,p.y),color,1,CV_AA,0);
}
void On_mouse(int event, int x, int y, int flags, void*)
{
   
	if (event == cv::EVENT_LBUTTONDOWN) 
	{
		g_bDrawingBox = true;
		recent_Point = cv::Point(x, y);
	}
}

4.绘制矩形区域

static bool g_bDrawingBox2 = false;
cv::Rect g_rectangle2 = cv::Rect(cv::Point(0,0),cv::Point(0,0));
void on_MouseHandle1(int event, int x, int y, int flags, void* param)
{
	cv::Mat& image = *(cv::Mat*)param;//先从空指针转换为Mat指针再解引用
	switch (event)
	{
	case cv::EVENT_MOUSEMOVE:
	{
		if (g_bDrawingBox2)
		{
			g_rectangle2.width = x - g_rectangle2.x;
			g_rectangle2.height = y - g_rectangle2.y;
		}
	}
	break;

	case cv::EVENT_LBUTTONDOWN:
	{
		g_bDrawingBox2 = true;
		g_rectangle2 = cv::Rect(x, y, 0, 0);//记录起始点
	}
	break;

	case cv::EVENT_LBUTTONUP:
	{
		g_bDrawingBox2 = false;
		if (g_rectangle2.width < 0)
		{
			g_rectangle2.x += g_rectangle2.width;
			g_rectangle2.width *= -1;
		}
		if (g_rectangle2.height < 0)
		{
			g_rectangle2.y += g_rectangle2.height;
			g_rectangle2.height *= -1;
		}
		DrawRectangle(image, g_rectangle2);
	}
	break;
	}
}

5.仿射变换

cv::Mat warp_mat;
bool SetCalib(double ImageX[3],double Image_Y[3],double Robot_X[3],double Robot_Y[3])
{
	try{
		cv::Point2f srcTri[3];
		cv::Point2f dstTri[3];
		srcTri[0] = cv::Point2f(ImageX[0],Image_Y[0]);
		srcTri[1] = cv::Point2f(ImageX[1],Image_Y[1]);
		srcTri[2] = cv::Point2f(ImageX[2],Image_Y[2]);
		dstTri[0] = cv::Point2f(Robot_X[0], Robot_Y[0]);
		dstTri[1] = cv::Point2f(Robot_X[1], Robot_Y[1]);
		dstTri[2] = cv::Point2f(Robot_X[2], Robot_Y[2]);
		warp_mat = cv::getAffineTransform(srcTri, dstTri );    
		return true;
	}catch(...){return false;}
}
void GetTransPt(cv::Point2f src, cv::Mat trans, cv::Point2f &dst)
{
	if (trans.empty())
	{
		return;
	}
	int n=trans.channels();

	cv::Point2f p = cv::Point2f(0, 0);
	dst.x = trans.ptr<double>(0)[0] * src.x + trans.ptr<double>(0)[1] * src.y + trans.ptr<double>(0)[2];
	dst.y = trans.ptr<double>(1)[0] * src.x + trans.ptr<double>(1)[1] * src.y + trans.ptr<double>(1)[2];
}

你可能感兴趣的:(机器视觉-opencv,opencv,c++,qt)