Futures again

To see how useful the previously described future is, I tried to retrofit some existing code. Here are some lines of test code before futures came in:

 1 obc::image orig(filename);
 2 obc::image img = obc::rgb2gray(orig);
 3 
 4 obc::show_image(img, "original");
 5 obc::show_image(obc::contrast_incr(img, 0.15), "contrast_incr");
 6 
 7 timespec timer_s, timer_e;
 8 clock_gettime(CLOCK_REALTIME, &timer_s);
 9 
10 CvSeq* lines = obc::hough_transform(img);
11 
12 obc::image temp(img);
13 obc::image disp = obc::gray2rgb(temp);
14 obc::draw_hough_lines(disp, lines);
15 
16 clock_gettime(CLOCK_REALTIME, &timer_e);
17 cout << "elapsed time = " << (time_diff(timer_s, timer_e) / 1000000.0) << endl;
18 
19 obc::show_image(disp, "result");
20 
21 cvReleaseMemStorage(&lines->storage);

The calculation of the Hough transform (line 10) takes a while so considering that each show_image() function needs user feedback which is quite slow it would make sense to trigger the calculation of the transform before the available images are shown. Thus line 5 in the code sample below contains the future that calculates asynchronously the Hough transform while we look at the images presented to us in the show_image() functions.

 1 obc::image orig(filename);
 2 obc::image img = obc::rgb2gray(orig);
 3 
 4 obc::image contr = obc::contrast_incr(img, 0.15);
 5 concur::future<CvSeq*> lines_f(bind(obc::hough_transform, img));
 6 
 7 obc::show_image(img, "original");
 8 obc::show_image(contr, "contrast_incr");
 9 
10 timespec timer_s, timer_e;
11 clock_gettime(CLOCK_REALTIME, &timer_s);
12 
13 obc::image temp(img);
14 obc::image disp = obc::gray2rgb(temp);
15 obc::draw_hough_lines(disp, lines_f());
16 
17 clock_gettime(CLOCK_REALTIME, &timer_e);
18 cout << "elapsed time = " << (time_diff(timer_s, timer_e) / 1000000.0) << endl;
19 
20 obc::show_image(disp, "result");
21 
22 cvReleaseMemStorage(&lines_f()->storage);

Now you may have recognized the little timing code and it comes as no surprise that the gains are significant. The old version takes about 2.8 seconds whereas the new one uses only 0.4 seconds for the covered code paths. A little reordering and renaming and voila – a nice result.

Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: