Comparison of opencv and GDI draw line efficiency

Source: Internet
Author: User

I,

Because of the project requirements, the original draw line function using GDI, the new project takes into account the problem of crashing the platform, and intends to use opencv for implementation, so we will make a comparison of efficiency.

II,

2. Make a line to test the efficiency.

The same size of the artboard --- 256*256 is used. The function uses the parameter input and gettickcount to implement the timing function.

III,

The main code of GDI is as follows:

Void show_line (INT line_num, int point_num) {ulonag start_time = get_tick_count (); vmgdipolympus Gon * test_polygon = new vmgdipolympus Gon (); int width = 256; int Height = 256; test_polygon-> vmiscleancloth (); test_polygon-> trim (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 <"Number" <"takes" <end_time-start_time <" ms "<Endl; test_polygon-> vmgdigetbitmap ("d :\\ 001.bmp ");}

The test cycle code of opencv is:

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); // draw line point A = point (x_1, y_1); point center = point (X_2, y_2 ); // cout <X_1 <"<Y_1 <Endl; // The parameter is the image, start point, end point, color, width, and line type, 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 ("bottom ", picture); show_info (picture );}

IV,

The number of lines and the format of each line point are designed in the main function.


Time Comparison:


The generated chart is:

Result comparison: the painting efficiency of opencv is the same as that of GDI at 1000 points, and the time used by gettickcount is negligible.

In the overall efficiency comparison, it is obvious that opencv has a higher draw efficiency.

V,

Code of two main functions

Code of the GDI main function:

# Include "vmgdipolympus Gon. 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 () {vmgdipolympus Gon * test_polygon = new vmgdipolympus Gon (); int width = 256; int Height = 256; test_polygon-> vmiscleancloth (); test_polygon-> vmgdiinitbuf (width, height); double pt_0 [3] = {0, 0 }; double pt_2 [3] = {20, 20, 0}; colorref color = 0xffff00; test_polygon-> vmgdisetpencolor (color); test_polygon-> partition (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 (); vmgdipolympus Gon * test_polygon = new vmgdipolympus Gon (); 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 <"Number" <"takes" <end_time-start_time <" ms "<Endl; test_polygon-> vmgdigetbitmap ("d :\\ 001.bmp ");}

Main function code of opencv:

# 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, "comment") # pragma comment (Lib, "comment ") # pragma comment (Lib, "opencv_gpu249d.lib") # pragma comment (Lib, "opencv_highgui249d.lib") # pragma comment (Lib, "comment") # pragma comment (Lib, "comment ") # pragma comment (Lib, "release") # pragma comment (Lib, "opencv_ts249d.lib") # pragma comment (Lib, "release") # pragma comment (Lib, "opencv_nonfree249d.lib ") # pragma comment (Lib, "photo") # pragma comment (Lib, "opencv_photo249d.lib") # pragma comment (Lib, "photo") # pragma comment (Lib, "opencv_superres249d.lib ") # pragma comment (Lib, "publish") # pragma comment (Lib, "publish") # pragma comment (Lib, "opencv_ts249.lib") # pragma comment (Lib, "opencv_video249.lib ") # pragma comment (Lib, "opencv_nonfree249.lib") # pragma comment (Lib, "release") # pragma comment (Lib, "opencv_photo249.lib") # pragma comment (Lib, "release ") # pragma comment (Lib, "opencv_superres249.lib") # pragma comment (Lib, "comment") # pragma comment (Lib, "comment") # pragma comment (Lib, "opencv_contrib249.lib ") # pragma comment (Lib, "opencv_core249.lib") # pragma comment (Lib, "comment") # pragma comment (Lib, "comment") # pragma comment (Lib, "opencv_gpu249.lib ") # pragma comment (Lib, "opencv_highgui249.lib") # pragma comment (Lib, "comment") # pragma comment (Lib, "comment") # pragma comment (Lib, "opencv_ml249.lib ") # include <iostream> # include <opencv2/CORE/core. HPP> # include <opencv2/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 ); // wait 6000 ms before the window closes 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); // draw line point A = point (x_1, y_1); point center = point (X_2, y_2 ); // cout <X_1 <"<Y_1 <Endl; // The parameter is the image, start point, end point, color, width, and line type, 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 ("bottom ", picture); show_info (picture);} // read an image (original game image) // mat IMG = imread ("pic.jpg "); //// create a window named "game source image" // cvnamedwindow ("game source image "); //// display the original game painting in the window // imshow ("original game painting", 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 NTS [] = {100}; polylines (picture, PPT, treaty, 0,255, cv_rgb, 0), 2, 8, 0);} imshow ("bottom plate", 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 );}

Of course, use the configurations of various libraries that require opencv. Opencv download and use, can refer to the http://blog.csdn.net/poem_qianmo/article/details/20911629 of light ink

Opencv tutorials, configuration settings, and other blogs.


Free source code: GDI test code

Draw line test code of opencv


------------- The end --------------

If you have any questions, please do not hesitate to inform us.



Comparison of opencv and GDI draw line efficiency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.