c_cpp 研究opencv3 3.3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 研究opencv3 3.3相关的知识,希望对你有一定的参考价值。

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

#define WINDOW_NAME "【程序窗口】"

void on_MouseHandle(int event, int x, int y, int flags, void* param);
void DrawRectangle(cv::Mat& img,cv::Rect box);
void ShowHelpText();

Rect g_rectangle;
bool g_bDrawingBox = false; //是否进行绘制
RNG g_rng(12345);

int main(int argc, char** argv)
{
	//1. 准备参数
	g_rectangle = Rect(-1, -1, 0, 0);
	Mat srcImage(600, 800, CV_8UC3), tempImage;
	srcImage.copyTo(tempImage);
	g_rectangle = Rect(-1, -1, 0, 0);
	srcImage = Scalar::all(0);

	//2 设置鼠标操作回调函数
	namedWindow(WINDOW_NAME);
	setMouseCallback(WINDOW_NAME, on_MouseHandle,(void*) &srcImage);


	Mat r = Mat(10, 3, CV_8UC3);
	randu(r, Scalar::all(0), Scalar::all(255));

	cout << " r (OpenCV 默认风格) = " << r << ";" << endl << endl;

	//Python 风格
	cout << " r (Python 风格) = " << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl;

	//逗号分隔风格(csv)
	cout << " r (逗号分隔风格) = " << format(r, Formatter::FMT_CSV) << ";" << endl << endl;

	//Numpy风格
	cout << " r (Numpy风格) = " << format(r, Formatter::FMT_NUMPY) << ";" << endl << endl;

	//C语言风格
	cout << " r (c语音风格) = " << format(r, Formatter::FMT_C) << "; \n" << endl << endl;

	//二维点定义和输出
	Point2f p(6, 2);
	cout << " 【二维点】p = " << p << "; \n" << endl << endl;
	
	//三维点定义和输出
	Point3f p3f(8, 2, 0);
	cout << " 【三维点】p3f = " << p3f << "; \n" << endl << endl;

	//定义和输出 基于 Mat的std::vector
	vector<float> v;
	v.push_back(3);
	v.push_back(5);
	v.push_back(7);

	cout << " 【基于Mat的vector】 shortvec = " << Mat(v) << "; \n" << endl << endl;

	vector<Point2f> points(20);
	for (size_t i = 0; i < points.size(); i++)
	{
		points[i] = Point2f((float)(i * 5),(float)( i % 7));
	}
	cout << "二维点向量 points = " << points << "; \n" << endl << endl;

	//3. 程序主循环,当进行绘制的标识为真时,进行绘制
	while (1)
	{
		srcImage.copyTo(tempImage); //复制原图到临时变量
		//如果为真,就进行绘制
		if (g_bDrawingBox) DrawRectangle( tempImage, g_rectangle);
		imshow(WINDOW_NAME, tempImage);
		if (waitKey(10) == 27) break;
	}
	return 0;
}

//描述: 鼠标回调函数,根据不同的鼠标事件进行不同的操作
void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
	Mat& image = *(cv::Mat*) param;
	switch (event)
	{
		//鼠标移动消息
	case EVENT_MOUSEMOVE:
	{
		//如果是否进行绘制的标识符为真,则记录下长和宽到RECT变量中
		if (g_bDrawingBox)
		{
			g_rectangle.width = x - g_rectangle.x;
			g_rectangle.height = y - g_rectangle.y;
		}
	}
	break;
	//左键按下消息
	case EVENT_LBUTTONDOWN:
	{
		g_bDrawingBox = true;
		g_rectangle = Rect(x, y, 0, 0);//记录起始点
	}
	break;
	//左键抬起消息
	case EVENT_LBUTTONUP:
	{
		g_bDrawingBox = false; //设置绘制标识符为false
		//对宽和高小于0的处理
		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;
	default:
		break;
	}

}

//自定义的矩形绘制函数
void DrawRectangle(cv::Mat& img, cv::Rect box)
{
	rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255),g_rng.uniform(0 ,255)));
}

以上是关于c_cpp 研究opencv3 3.3的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 研究opencv3 4.3

c_cpp 研究opencv3 1.6

c_cpp 研究opencv3 3.2.1

c_cpp 研究opencv3 2.6.2

OpenCV 3.3正式发布啦

OpenCv 3.3 Cuda Medianfilter 生成图像黑色的 2/3