一、

因为项目须要,原来用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的画线效率更高。

五、

两种情况均保存成bmp格式图像。

效果对照:

GDI效果:

OpenCV效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FydHpoYW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

放大效果:

GDI效果:

OpenCV放大:

眼下看来。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--------------

若有问题,请指教。

最新文章

  1. 实现一个类 RequireJS 的模块加载器 (二)
  2. 使用jstack分析cpu消耗过高的问题
  3. webpack 使用教程--实时刷新测试
  4. javax.servlet.ServletException: com.ibatis.sqlmap.client.SqlMapException: There is no statement named...问题
  5. ASP.NET 连接MySQL数据库 详细步骤
  6. 基于Microsoft Azure、ASP.NET Core和Docker的博客系统
  7. linux上安装shell编辑器与linux运维面试题
  8. 10分钟学会Linux
  9. activemq学习
  10. Jetty入门(1-2)eclipse集成jetty插件并发布运行应用
  11. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件
  12. day12_python_1124
  13. pytorch中,不同的kernel对不同的feature map进行卷积之后输出某一个channel对应的多个feature map如何得到一个channel的feature map
  14. C#编程经验-VS Debug
  15. cocos creator 的scorllview 滑动事件和 子内容触摸事件会产生冲突
  16. BZOJ1051或洛谷2341 [HAOI2006]受欢迎的牛
  17. Git &amp; GitHub 的安装配置
  18. SVG DOM常用属性和方法介绍(1)
  19. 《实战突击:PHP项目开发案例整合(第2版)(含DVD光盘1张)》
  20. MapReduce分析流量汇总

热门文章

  1. html5学习之第一步:认识标签,了解布局
  2. [ZJOI2015]诸神眷顾的幻想乡 广义后缀自动机_DFS_语文题
  3. LCT笔记
  4. KVM 日常使用命令
  5. 关于 Error: No PostCSS Config found in 的错误
  6. Python格式化字符串、占位符、合并数组
  7. Object-C学习比较费劲的3点原因
  8. NYIST 1006 偷西瓜
  9. 采药 水题 dp 01背包问题 luogu1048
  10. Java 学习(11): 面向对象编程—继承(super,this)