openCV 和GDI画线效率对比

一、

由于项目需要,原来用GDI做的画线的功能,新的项目中考虑到垮平台的问题,打算用openCV来实现,故此做个效率对比。

二、

2点做一条线,来测试效率。

用了同样的画板大小---256*256的大小,函数通过参数输入,用GetTickCount来实现计时功能。

三、

GDI的主要循代码如下:

void  show_line(int line_num,int point_num)
{

	ULONAG start_time = get_tick_count();
	VMGdiPolygon* test_polygon = new VMGdiPolygon();
	int width = 256;
	int height = 256;
	test_polygon->VMIsCleanCloth();
	test_polygon->VMGdiInitBuf(width,height);
	COLORREF color = 0x0000FF;
	test_polygon->VMGdiSetPenColor(color);
	test_polygon->VMGdiSetPenWidth(2);

	int rangle = width;
	int line_count = line_num;
	for (int i = 0; i < line_count;i++)
	{
		for (int j = 0; j<point_num;j++)
		{
			int x_1 = random_fun(rangle);
			int y_1 = random_fun(rangle);

			int x_2 = random_fun(rangle);
			int y_2 = random_fun(rangle);			

			double  pt_0[3] = {x_1,y_1,0};
			double  pt_2[3] = {x_2,y_2,0};
			test_polygon->VMGdiLine(pt_0,pt_2);
		}
		//test_polygon->VMGdiLine(data,point_num,false);
	}

	ULONAG end_time = get_tick_count();
	cout<<"start time"<<start_time<<"--->end time:"<<end_time<<endl;
	cout<<"the number of "<<line_count<<"  lines "<<"has "<<point_num<<" 个数"<<" takes "<<end_time-start_time<<"ms "<<endl;

	test_polygon->VMGdiGetbitmap("D:\\001.bmp");
}

OpenCV的测试循环代码为:

void test_line(int width,int height,int line_count,int point_num,int line_width)
{
	ULONAG start_time = get_tick_count();
	int pic_width = width;
	int pic_height = height;
	Mat picture(pic_width,pic_height,CV_8UC4,Scalar(255,255,255));

	int rangle = width;
	for (int i = 0; i < line_count;i++)
	{
		for (int j = 0; j<point_num;j++)
		{
			int x_1 = random_fun(rangle);
			int y_1 = random_fun(rangle);

			int x_2 = random_fun(rangle);
			int y_2 = random_fun(rangle);
			//画线
			Point a = Point (x_1,y_1);
			Point center = Point(x_2,y_2);
			//cout<<x_1<<"  "<<y_1<<endl;
			//参数为:承载的图像、起始点、结束点、颜色、粗细、线型
			line(picture,a,center,Scalar(255,0,0),line_width,8);
		}
	}

	ULONAG end_time = get_tick_count();
	cout<<"the number of "<<line_count<<"  lines "<<" takes "<<end_time-start_time<<"ms "<<endl;

	imshow("底板",picture);
	show_info(picture);
}

四、

调用过程有在main函数中设计线的条数和每条线点的格式。

时间对比:

生成的图表为:

结果对比:opencv的画线效率和GDI在1000个点的处理效率是一致的,用gettickcount使用时间的忽略不计的。

而在整体效率比较中,很明显opencv的画线效率更高。

五、

两个main函数的代码

GDI main函数代码:

#include "VMGdiPolygon.h"
using namespace VRMap;

#include <iostream>
using namespace std;
#include "rw_timer.h"

int  random_fun(int rangle);
void show_2_point_line();
void  show_line(int line_num,int point_num,int line_width);
//void  show_polygonline(int line_num,int point_num,int line_width);

void main()
{
	//show_2_point_line();
	int line_number = 1;
	int point_numb = 10;
	int line_width = 2;
	show_line(line_number,point_numb);
	//show_polygonline(line_number,point_numb,line_width);
	system("pause");
	return;
}

int  random_fun(int rangle)
{
	int seed(0);
	int result = rand()%rangle;
	return result;
}

void show_2_point_line()
{
	VMGdiPolygon* test_polygon = new VMGdiPolygon();
	int width = 256;
	int height = 256;
	test_polygon->VMIsCleanCloth();
	test_polygon->VMGdiInitBuf(width,height);

	double  pt_0[3] = {0,0,0};
	double  pt_2[3] = {20,20,0};

	COLORREF color = 0xFFFF00;
	test_polygon->VMGdiSetPenColor(color);
	test_polygon->VMGdiSetPenWidth(2);
	test_polygon->VMGdiLine(pt_0,pt_2);

	test_polygon->VMGdiGetbitmap("D:\\001.bmp");
}

void  show_line(int line_num,int point_num)
{

	ULONAG start_time = get_tick_count();
	VMGdiPolygon* test_polygon = new VMGdiPolygon();
	int width = 256;
	int height = 256;
	test_polygon->VMIsCleanCloth();
	test_polygon->VMGdiInitBuf(width,height);
	COLORREF color = 0x0000FF;
	test_polygon->VMGdiSetPenColor(color);
	test_polygon->VMGdiSetPenWidth(2);

	int rangle = width;
	int line_count = line_num;
	for (int i = 0; i < line_count;i++)
	{
		for (int j = 0; j<point_num;j++)
		{
			int x_1 = random_fun(rangle);
			int y_1 = random_fun(rangle);

			int x_2 = random_fun(rangle);
			int y_2 = random_fun(rangle);			

			double  pt_0[3] = {x_1,y_1,0};
			double  pt_2[3] = {x_2,y_2,0};
			test_polygon->VMGdiLine(pt_0,pt_2);
		}
		//test_polygon->VMGdiLine(data,point_num,false);
	}

	ULONAG end_time = get_tick_count();
	cout<<"start time"<<start_time<<"--->end time:"<<end_time<<endl;
	cout<<"the number of "<<line_count<<"  lines "<<"has "<<point_num<<" 个数"<<" takes "<<end_time-start_time<<"ms "<<endl;

	test_polygon->VMGdiGetbitmap("D:\\001.bmp");
}

openCV的main函数代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#include "rw_timer.h"

//
////
#pragma comment(lib,"opencv_ml249d.lib")
#pragma comment(lib,"opencv_calib3d249d.lib")
#pragma comment(lib,"opencv_contrib249d.lib")
#pragma comment(lib,"opencv_core249d.lib")
#pragma comment(lib,"opencv_features2d249d.lib")
#pragma comment(lib,"opencv_flann249d.lib")
#pragma comment(lib,"opencv_gpu249d.lib")
#pragma comment(lib,"opencv_highgui249d.lib")
#pragma comment(lib,"opencv_imgproc249d.lib")
#pragma comment(lib,"opencv_legacy249d.lib")
#pragma comment(lib,"opencv_objdetect249d.lib")
#pragma comment(lib,"opencv_ts249d.lib")
#pragma comment(lib,"opencv_video249d.lib")
#pragma comment(lib,"opencv_nonfree249d.lib")
#pragma comment(lib,"opencv_ocl249d.lib")
#pragma comment(lib,"opencv_photo249d.lib")
#pragma comment(lib,"opencv_stitching249d.lib")
#pragma comment(lib,"opencv_superres249d.lib")
#pragma comment(lib,"opencv_videostab249d.lib")

#pragma comment(lib,"opencv_objdetect249.lib")
#pragma comment(lib,"opencv_ts249.lib")
#pragma comment(lib,"opencv_video249.lib")
#pragma comment(lib,"opencv_nonfree249.lib")
#pragma comment(lib,"opencv_ocl249.lib")
#pragma comment(lib,"opencv_photo249.lib")
#pragma comment(lib,"opencv_stitching249.lib")
#pragma comment(lib,"opencv_superres249.lib")
#pragma comment(lib,"opencv_videostab249.lib")
#pragma comment(lib,"opencv_calib3d249.lib")
#pragma comment(lib,"opencv_contrib249.lib")
#pragma comment(lib,"opencv_core249.lib")
#pragma comment(lib,"opencv_features2d249.lib")
#pragma comment(lib,"opencv_flann249.lib")
#pragma comment(lib,"opencv_gpu249.lib")
#pragma comment(lib,"opencv_highgui249.lib")
#pragma comment(lib,"opencv_imgproc249.lib")
#pragma comment(lib,"opencv_legacy249.lib")
#pragma comment(lib,"opencv_ml249.lib")

#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int  random_fun(int rangle);
void show_info(Mat picture);
void test_line(int width,int height,int line_count,int point_num,int line_width);
void test_polyline(int width,int height,int line_count,int point_num,int line_width);

int main()
{
	int width = 256;
	int height = 256;

	int line_count = 100;
	int point_num = 1000;
	int line_width = 1;

	test_line(width,height,line_count,point_num,line_width);
	//
	//test_polyline(width,height,line_count,100,line_width);

	// 等待6000 ms后窗口自动关闭
	waitKey(12000);
}

void show_info(Mat picture)
{
	if (picture.data == NULL){
		return;
	}
	//IplImage* test_img = cvSaveImage()
	int channels = picture.channels();
	int rows = picture.rows;
	int cols = picture.cols;
	uchar* data = picture.data;
	cout<<"chanels:"<<channels<<" rows:" <<rows<<" cols:"<<cols<<endl;
}

void test_line(int width,int height,int line_count,int point_num,int line_width)
{
	ULONAG start_time = get_tick_count();
	int pic_width = width;
	int pic_height = height;
	Mat picture(pic_width,pic_height,CV_8UC4,Scalar(255,255,255));

	int rangle = width;
	for (int i = 0; i < line_count;i++)
	{
		for (int j = 0; j<point_num;j++)
		{
			int x_1 = random_fun(rangle);
			int y_1 = random_fun(rangle);

			int x_2 = random_fun(rangle);
			int y_2 = random_fun(rangle);
			//画线
			Point a = Point (x_1,y_1);
			Point center = Point(x_2,y_2);
			//cout<<x_1<<"  "<<y_1<<endl;
			//参数为:承载的图像、起始点、结束点、颜色、粗细、线型
			line(picture,a,center,Scalar(255,0,0),line_width,8);
		}
	}

	ULONAG end_time = get_tick_count();
	cout<<"the number of "<<line_count<<"  lines "<<" takes "<<end_time-start_time<<"ms "<<endl;

	imshow("底板",picture);
	show_info(picture);
}

// 读入一张图片(游戏原画)
//Mat img=imread("pic.jpg");
//// 创建一个名为 "游戏原画"窗口
//cvNamedWindow("游戏原画");
//// 在窗口中显示游戏原画
//imshow("游戏原画",img);

int  random_fun(int rangle)
{
	int seed(0);
	//srand( (unsigned)time( NULL ) );
	int result = rand()%rangle;
	return result;
}

void test_polyline(int width,int height,int line_count,int point_num,int line_width)
{
	ULONAG start_time = get_tick_count();
	int pic_width = width;
	int pic_height = height;
	Mat picture(pic_width,pic_height,CV_8UC4,Scalar(255,255,255));	

	line_count = 1;
	int rangle = width;
	Point** test_points = new Point*[line_count];
	int *npts = new int[line_count];
	for (int j = 0;j < line_count;j++)
	{
		Point rook_points[1][100];
		for (int k =0;k<100;k++)
		{
			int x = random_fun(rangle);
			int y = random_fun(rangle);
			rook_points[0][j] = Point( x,y);
		}
		const Point* ppt[1] = { rook_points[0] };
		int npt[] = { 100 };
		polylines(picture,ppt,npt,1,1,CV_RGB(0,255,0),2,8,0);
	}
	imshow("底板",picture);

	//
	//for (int j = 0;j < line_count;j++)
	//{
	//	delete []test_points[j];
	//	test_points[j] = NULL;
	//}
	//delete []test_points;
	//test_points = NULL;

	ULONAG end_time = get_tick_count();
	cout<<"the number of "<<line_count<<"  lines "<<" takes "<<end_time-start_time<<"ms "<<endl;
	show_info(picture);
}

当然使用需要opencv的各种库的配置。opencv的下载和使用,可参考浅墨的http://blog.csdn.net/poem_qianmo/article/details/20911629

opencv教程和配置设置等博客。

源码免费下载地址:GDI测试代码

OPENCV的画线测试代码

-------------THE END--------------

若有问题,请不吝赐教。

时间: 2024-11-05 01:07:27

openCV 和GDI画线效率对比的相关文章

openCV 和GDI画线效率对照

一. 因为项目须要,原来用GDI做的画线的功能.新的项目中考虑到垮平台的问题.打算用openCV来实现.故此做个效率对照. 二. 2点做一条线,来測试效率. 用了相同的画板大小---256*256的大小,函数通过參数输入.用GetTickCount来实现计时功能. 三. GDI的主要循代码例如以下: void show_line(int line_num,int point_num) { ULONAG start_time = get_tick_count(); VMGdiPolygon* te

GDI+学习之 ------ 画线、区域填充、写字

<精通GDI编程>里的代码,在学习过程中对它加以总结,以防以后用到,所有代码都是在MFC 单文档中实现的,写在View::OnDraw(CDC */*pDC*/)中 画线/边框(Pen) 1.画单线-------DrawLine [cpp] view plaincopy Pen pen(Color(255,0,0,0),3); PointF L_PTStart(0,0); PointF L_PTEnd(100,10); graphics.DrawLine(&pen,L_PTStart,

Android中Path类的lineTo方法和quadTo方法画线的区别

当我们需要在屏幕上形成画线时,Path类的应用是必不可少的,而Path类的lineTo和quadTo方法实现的绘制线路形式也是不一样的,下面就以代码的实现来直观的探究这两个方法的功能实现区别: 1. Path--->quadTo(float x1, float y1, float x2, float y2): 该方法的实现是当我们不仅仅是画一条线甚至是画弧线时会形成平滑的曲线,该曲线又称为"贝塞尔曲线"(Bezier curve),其中,x1,y1为控制点的坐标值,x2,y2为终

WPF画线问题,几千条以后就有明显的延迟了。

我现在是这么画的,class A { private GeometryGroup _lines; private Path _path; public A() {    _path.Data = _lines; } public Draw() {   LineGeometry line = new LineGeometry(p1, p2);   _lines.Children.Add(line); } }一开始的速度很好,但是线多了以后,就有明显的延迟了. 有什么更快速的方法不? 解决方案 ?

图形学--(中点画线法+Bresenham画线算法)

编程环境:codeblocks+EGE库 用到的函数:putpixel(int x1,int y1,int color)  用某种颜色打亮一个坐标点. 这俩种算法都是用来在计算机上画一条直线的,那么我们为什么不直接用直线方程分别带点再打亮呢,这是因为,计算机中每个坐标点都是整数,而直线是由一个个像素点组合而成的,那么,直接将坐标点再进行四舍五入整数化就好了啊,的确,这是一种方法,但计算机中进行浮点数的四舍五入会使运算效率变差,因此真正画直线时是用到上边这俩种方法的. 1.中点画线法 只考虑当直线

图形学复习4——光栅化(画线画圆扫描线反走样算法)

图形学复习 CH7 光栅化 前几章介绍了几何处理和裁剪变换,接下来的步骤就是光栅化 光栅化是将形式表示的几何图元转换为阵列表示的数据片元的过程,片元中每一个像素对应帧缓冲区中的每一个像素 7.1 线段生成算法 (1)DDA画线算法 设直线表达式为y=mx+b,输入直线两端点坐标(x0,y0)和(xend,yend),可以计算出m=yend?y0xend?x0和b=y0?m?x0 DAA是基于微分运算的线段生成算法,其主要计算式便是δy=mδx: 若|m|≤1则x方向的变化大于y方向的变化,以x方

用GDI+画验证码

1.新建一个窗体应用程序,在上面拖一个pictureBox对象,为其添加单击事件 2.创建GDI对象.产生随机数画入图片中.画线条.最后将图片到pictureBox中,代码如下: 1 private void pictureBox1_Click(object sender, EventArgs e) 2 { 3 //创建GDI对象 4 Bitmap bmp = new Bitmap(150,40); 5 Graphics g = Graphics.FromImage(bmp); 6 7 //产生

opencv-视频处理--画线(越线、拌线)

视频处理中,经常有做一些行人.车辆或者其它运动物体越线检测,越界检测. 原视频流: 下面用opencv介绍两种方式,画直线(越线.拌线): 第一种:固定第一帧,或者暂停视频,在固定的一帧中完成画直线的功能 #include<iostream> using namespace std; #include<opencv2\core\core.hpp> #include<opencv2\highgui\highgui.hpp> #include<opencv2\imgp

GDI+ 画渐变色环

在onpaint() 函数中加入如下代码,本次利用DrawArc来实现. #define PI 3.1415926 int angle=360; int x=(rect.Width()-300)/2; int y=190; int width=300; int hight=300; int wide=34; graphics.SetSmoothingMode(SmoothingModeAntiAlias);//抗锯齿 for(float i = 0.0; i < PI; i += (float)