我对顶帽滤镜的速度有问题。在IEEE交易文件中,在具有8 GB内存和4 GHz Intel i7处理器的PC上,使用MATLAB软件估算应用于分辨率为320×256的图像的大礼帽的运行时间为0.0062秒。 table_time我在具有6 GB内存和2.6 GHz Intel i5处理器的笔记本电脑上使用openCV运行以下代码,该图像具有相同的分辨率: enter image description here
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(){
double t0 = (double)getTickCount();
Mat src,dst;
src=imread("E:/tree.jpg",0);
Mat element = getStructuringElement(MORPH_ELLIPSE,Size(15,15));
morphologyEx(src,dst,MORPH_TOPHAT,element,Point(-1,-1));
double elapsed=((double)getTickCount()-t0)/getTickFrequency();
cout<<elapsed<<"second"<<"\n";
return -1;}
需要.05375秒。尽管C ++和openCV很快,但为什么大礼帽的运行时间在matlab中为6 ms,在opencv中为54 ms。我怎么能加速呢?
答案 0 :(得分:1)
首先也是最重要的一点是,您正在计算从磁盘加载的图像,这通常是程序中最慢的部分。如果你想与纸张进行比较,只需要时间上的tophat功能。
此外,还有许多其他编译选项可以实现这一点。您可以尝试使用各种特定于处理器的优化,CUDA等编译OpenCV。这可以产生很好的效果。
答案 1 :(得分:1)
这是我在笔记本电脑上的测试结果(英特尔(R)_Core(TM)_ i5-3230M_CPU _ @ _ 2.60GHz)
0.00129578秒!!
我认为最重要的是你是如何编译OpenCV的。有很多选择。我附上了getBuildInformation()
测试代码(代码的改进版本)
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
Mat src, dst;
src = imread("tree.jpg", 0);
Mat element = getStructuringElement(MORPH_ELLIPSE, Size(15, 15));
double elapsed = 0;
for (int i = 0; i < 100; i++)
{
double t0 = (double)getTickCount();
morphologyEx(src, dst, MORPH_TOPHAT, element, Point(-1, -1));
elapsed += ((double)getTickCount() - t0) / getTickFrequency();
}
cout << elapsed / 100 << " seconds" << "\n";
cout << getBuildInformation();
imshow("result", dst);
waitKey();
return -1;
}
输出测试代码
0.00129578 seconds
General configuration for OpenCV 3.1.0-dev =====================================
Version control: unknown
Extra modules:
Location (extra): D:/git/opencv_contrib/modules
Version control (extra): unknown
Platform:
Timestamp: 2016-05-27T19:59:05Z
Host: Windows 10.0.10586 AMD64
CMake: 3.5.0-rc3
CMake generator: Visual Studio 14 2015 Win64
CMake build tool: C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
MSVC: 1900
C/C++:
Built as dynamic libs?: YES
C++ Compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe (ver 19.0.23506.0)
C++ flags (Release): /DWIN32 /D_WINDOWS /W4 /GR /EHa /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:AVX /Oi /wd4251 /wd4324 /wd4275 /wd4589 /MP4 /MD /O2 /Ob2 /D NDEBUG
C++ flags (Debug): /DWIN32 /D_WINDOWS /W4 /GR /EHa /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:AVX /Oi /wd4251 /wd4324 /wd4275 /wd4589 /MP4 /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
C Compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:AVX /Oi /MP4 /MD /O2 /Ob2 /D NDEBUG
C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:AVX /Oi /MP4 /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
Linker flags (Release): /machine:x64 /INCREMENTAL:NO
Linker flags (Debug): /machine:x64 /debug /INCREMENTAL
Precompiled headers: NO
Extra dependencies: comctl32 gdi32 ole32 setupapi ws2_32 D:/git/pthreadVC2.lib
3rdparty dependencies: zlib libjpeg libpng libtiff libjasper IlmImf libprotobuf
OpenCV modules:
To be built: core flann imgproc ml photo reg surface_matching video dnn fuzzy imgcodecs shape videoio highgui objdetect plot superres ts xobjdetect xphoto bgsegm bioinspired cvblobslib dpm face features2d line_descriptor saliency text calib3d ccalib datasets rgbd stereo structured_light videostab xfeatures2d ximgproc aruco optflow stitching tracking ayvos java
Disabled: world contrib_world
Disabled by dependency: -
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev python2 python3 viz cvv hdf matlab sfm
Windows RT support: NO
GUI:
QT: NO
Win32 UI: YES
OpenGL support: NO
VTK support: NO
Media I/O:
ZLib: build (ver 1.2.8)
JPEG: build (ver 90)
WEBP: NO
PNG: build (ver 1.6.19)
TIFF: build (ver 42 - 4.0.2)
JPEG 2000: build (ver 1.900.1)
OpenEXR: build (ver 1.7.1)
GDAL: NO
Video I/O:
Video for Windows: NO
DC1394 1.x: NO
DC1394 2.x: NO
FFMPEG: YES (prebuilt binaries)
codec: YES (ver 56.41.100)
format: YES (ver 56.36.101)
util: YES (ver 54.27.100)
swscale: YES (ver 3.1.101)
resample: NO
gentoo-style: YES
GStreamer: NO
OpenNI: NO
OpenNI PrimeSensor Modules: NO
OpenNI2: NO
PvAPI: NO
GigEVisionSDK: NO
DirectShow: YES
Media Foundation: NO
XIMEA: NO
Intel PerC: NO
Parallel framework: Concurrency
Other third-party libraries:
Use IPP: NO
Use IPP Async: NO
Use Eigen: NO
Use Cuda: NO
Use OpenCL: YES
Use custom HAL: NO
OpenCL: <Dynamic loading of OpenCL library>
Include path: D:/git/opencv/3rdparty/include/opencl/1.2
Use AMDFFT: NO
Use AMDBLAS: NO
Python 2:
Interpreter: C:/Python27/python.exe (ver 2.7.11)
Python 3:
Interpreter: NO
Python (for build): C:/Python27/python.exe
Java:
ant: C:/Program Files/apache-ant/bin/ant.bat (ver 1.9.6)
JNI: C:/Program Files/Java/jdk1.8.0_74/include C:/Program Files/Java/jdk1.8.0_74/include/win32 C:/Program Files/Java/jdk1.8.0_74/include
Java wrappers: YES
Java tests: NO
Matlab: NO
Tests and samples:
Tests: NO
Performance tests: YES
C/C++ Examples: YES
Install path: D:/opencv-build/install
cvconfig.h is in: D:/opencv-build
-----------------------------------------------------------------