您的位置:首页 > 编程语言 > C语言/C++

C与C++在opencv中的区别

2015-06-28 20:20 357 查看
Opencv官方文档中提供的接口类型有c的,也有C++的,其实这两者之间并没有什么区别,或者更精确地说,区别很小,我所知道的只有一个,下面会介绍。

首先,这里是一个关于用C还是C++的问题(http://stackoverflow.com/questions/11376368/opencv-c-and-c-performance-comparison),回答问题的高人表示,这两种接口其实没什么区别(there is not much difference between the C and C++ interface in OpenCV. Actually, some of the code is written in C, and has a C++ wrapper interface, and some viceversa.),并且提供了几个用于提高程序效率的方法:

Advice for optimization

A. Profile your app. Do it first on your computer, since it is much easier. Use visual studio profiler, and see what functions take the most. Optimize them. Never ever optimize because you think is slow, but because you measure it. Start with the slowest function, optimize it as much as possible, then take the second slower.

B. First, focus on algorithms. A faster algorithm can improve performance with orders of magnitude (100x). A C++ trick will give you maybe 2x performance boost.

Classical techniques:

Resize you video frames to be smaller. many times, you can extract the information from a 200x300px image, instead of a 1024x768. The area of the first one is 10 times smaller.

Use simpler operations instead of complicated ones. Use integers instead of floats. And never use double in a matrix or a for loop that executes thousands of times.

Do as little calculation as possible. Can you track an object only in a specific area of the image, instead of processing it all for all the frames? Can you make a rough/approximate detection on a very small image and then refine it on a ROI in the full frame?

C. In for loops, it may make sense to use C style instead of C++. A pointer to data matrix or a float array is much faster than mat.at or std::vector<>. But change only if it’s needed. Usually, a lot of processing (90%) is done in some double for loop. Focus on it. It doesn’t make sense to replace vector<> all over the place, ad make your code look like spaghetti.

D. Some OpenCV functions convert data to double, process it, then convert back to the input format. Beware of them, they kill performance on mobile devices. Examples: warping, scaling, type conversions. Also, color space conversions are known to be lazy. Prefer grayscale obtained directly from native YUV.

E. ARM processors have NEON. Learn and use it. It is powerfull!

A small example:

float* a, *b, *c;
// init a and b to 1000001 elements
for(int i=0;i<1000001;i++)
c[i] = a[i]*b[i];


can be rewritten as follows. It’s more verbose, but trust me it’s faster.

float* a, *b, *c;
// init a and b to 1000001 elements
float32x4_t _a, _b, _c;
int i;
for(i=0;i<1000001;i+=4)
{
a_ = vld1q_f32( &a[i] ); // load 4 floats from a in a NEON register
b_ = vld1q_f32( &b[i] );
c_ = vmulq_f32(a_, b_); // perform 4 float multiplies in parrallel
vst1q_f32( &c[i], c_); // store the four results in c
}
// the vector size is not always multiple of 4 or 8 or 16.
// Process the remaining elements
for(;i<1000001;i++)
c[i] = a[i]*b[i];


另外一个问题(http://stackoverflow.com/questions/11768223/different-result-with-opencv-c-and-c-api-border-interpolation-difference)描述了这篇文章开始所说的C与C++接口的一个区别,那就是设定ROI的区别,C接口设定完ROI后就不再管之前的图片了,也就是说它把ROI图片当做一张独立于母图片的图片处理。而C++接口会参考原来的图片(in ‘C++’, I have found that it always refers back to the original image for pixels beyond left/right/top/bottom most pixel. Hence, the border pixel extrapolation method mentioned doesn’t affect your output if there are pixels in the original image around your ROI.)。这个我也不知道是不是一个bug,或者opencv现在应该已经修复好了。

总之C接口与C++接口没什么区别,性能效率上也没有什么区别。这篇文章的可取之处在于文章前半段那个高人给的关于提高代码性能的建议。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: