After unzip opencv3.1.0, I was cmake with:
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D INSTALL_C_EXAMPLES=ON –D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_OPENGL=ON -D WITH_V4L=ON –D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_TBB=ON ..
But when I run opencv-3.1.0/sample/gpu/video_reader.cpp with compile :
sudo g++ $(pkg-config --libs --cflags opencv) -o stream video_reader.cpp
I have received notice:
OpenCV was built without CUDA Video decoding support
I'm using Raspberry 3. Thanks all !
Raspberry Pi 3 has no NVIDIA GPU, therefore CUDA is not supported.
From NVIDIA page:
CUDA® is a parallel computing platform and programming model invented by NVIDIA. It enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU). - See more at: http://www.nvidia.com/object/cuda_home_new.html#sthash.5wWaGnZI.dpuf
From answer related to similar question:
Q: Is it possible to use the GPU for calculations? (e.g. CUDA/OpenCL)
A: Not at present - there is only a framebuffer interface for display purposes. There is no OpenCL and no plans for it nor is there documentation available to create OpenCL. CUDA is Nvida only so isn't applicable. Once an OpenGL driver becomes available you may be able to engineer some calculations via the GPU but how useful that will be remains to be seen.
Instead you can use OpenGL ES or OpenVG to write code using GPU. Raspberry PI video api
Related
I was reading this article on how to create compact builds for applications using OpenCV.
But, this article includes only GCC related optimizations. I am looking for equivalent options in Visual Studio, but am able to find only the option to minimize size (/Os) and the option /LTCG.
I am building a very simple OpenCV console application (code is below) using OpenCV static libraries built from source. The issue is the size of executable is too large (37 MB in Debug build and 19 MB in Release build). I want to reduce that size to a few MBs and I am looking for ways to do that - I found that article while researching about this.
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
int main()
{
cv::Mat m = cv::imread("sample.jpg");
cv::imwrite("output.jpg", m);
std::cout << "Hello World!\n" << m.rows << " " << m.cols;
return 0;
}
Just in case anyone wants to know, I am including following libraries (the below are for Release mode):
opencv_core430.lib
opencv_imgproc430.lib
opencv_imgcodecs430.lib
libjpeg-turbo.lib
libpng.lib
libtiff.lib
libwebp.lib
ippiw.lib
zlib.lib
IlmImf.lib
libjasper.lib
ittnotify.lib
ippicvmt.lib
If anyone has any ideas what equivalent options are in Visual Studio, it would be of great help.
Also, is there anyone who was successful to reduce the executable size using static OpenCV libraries on Visual Studio?
I encountered the same issue and I found two things which make a big difference: performance libraries/frameworks and unnecessary file types.
Since you are building from source, you should be able to use cmake-gui to adjust the configuration to disable these:
WITH_IPP
WITH_ITT
WITH_OPENCL
Then, disable these additional image types if you need only JPG:
WITH_JASPER
WITH_PNG
WITH_OPENJPEG
WITH_WEBP
WITH_TIFF
WITH_OPENEXR
WITH_IMGCODEC_HDR
WITH_IMGCODEC_SUNRASTER
WITH_IMGCODEC_PXM
WITH_IMGCODEC_PFM
Since imread and imwrite determine the file type based on the given string, the linker has no way of knowing that the code for the other unnecessary image formats can be removed.
When I apply these changes and compile your program using Visual Studio 2022 it comes out to about 1.7 MB in release mode. (Starting with the default configuration and disabling only BUILD_SHARED_LIBS, I had seen the same 19 MB executable that you had reported.)
You can build as per your modules requirements, details in the blog.
The simplest way to build is using OpenCV cmake example:
cmake
-D WITH_CUDA=OFF \
-D WITH_MATLAB=OFF \
-D BUILD_ANDROID_EXAMPLES=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_SHARED_LIBS=OFF \
-D BUILD_opencv_objdetect=OFF \
-D BUILD_opencv_video=OFF \
-D BUILD_opencv_videoio=OFF \
-D BUILD_opencv_features2d=OFF \
-D BUILD_opencv_flann=OFF \
-D BUILD_opencv_ml=OFF \
-D BUILD_opencv_photo=OFF \
-D BUILD_opencv_python=OFF \
-D BUILD_opencv_shape=OFF \
-D BUILD_opencv_stitching=OFF \
-D BUILD_opencv_superres=OFF \
-D BUILD_opencv_ts=OFF \
-D BUILD_opencv_videostab=OFF \
-D BUILD_opencv_highgui=ON \
-D BUILD_opencv_imgproc=ON \
..
For static linking, you can pick and choice the module(s) by un-archiving .a to .o and bundle them in single .a.
CAUTION: be very sure that you do not break the dependencies.
Example:
to unarchive a .a using ar -x libopencv_imgproc.a and to archive again
ar -rc libopencv_custom.a *.o
I'm processing RGB images and doing the same thing for each channel (R+G+B) so I've been looking for parallel functions that could help me improve my code and run it (3*?) faster. Now Im using forEach function like so:
unsigned char lut[256];
for (int i = 0; i < 256; i++)
lut[i] = cv::saturate_cast<uchar>(pow((float)(i / 255.0), fGamma) * 255.0f); //pow: power exponent
dst.forEach<cv::Vec3b> //dst is an RGB image
(
[&lut](cv::Vec3b &pixel, const int* po) -> void
{
pixel[0] = lut[(pixel[0])];
pixel[1] = lut[(pixel[1])];
pixel[2] = lut[(pixel[2])];
}
);
But when I use htop to see the number of threads running, I only find one or two threads on..
Am I doing something wrong or forEach isn't suppose to run on multi-threading? Do you have any resources to help me with to get to multi-threading computations?
I'm running my code on ubuntu with this:
g++ -std=c++1z -Wall -Ofast -march=native test3.cpp -o test3 `pkg-config --cflags --libs opencv`
Have you taken a look in TBB already? Threading Building Blocks is an apache licensed lib for parallel computing which you can use just compiling OpenCV with the flag -D WITH_TBB=ON
See this example of parallel_for: http://www.jayrambhia.com/blog/opencv-with-tbb
If you decide to adopt TBB follow these steps:
1 - Rebuild OpenCV with TBB support. If you are running into a Linux machine just do:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON BUILD_TBB=ON ..
2 - Rewrite your program to use TBB
See the answers there: Simplest TBB example focusing in the most recent ones.
I want to know how much my code is faster with CUDA, so I compile OpenCV with below switches (including cuda)
cmake CMAKE_BUILD_TYPE=RELEASE \
CMAKE_INSTALL_PREFIX=/usr/local \
WITH_CUDA=ON \
ENABLE_FAST_MATH=1 \
CUDA_FAST_MATH=1 \
WITH_CUBLAS=1 \
INSTALL_PYTHON_EXAMPLES=OFF \
OPENCV_EXTRA_MODULES_PATH=/home/saeed/opencv_contrib/modules \
BUILD_EXAMPLES=OFF ..
and I'm working on Linux based machine and with dedicated GPU on my system so how I can measure gpu usage for opencv code? I did that with using
$ nvidia-smi
command,which give me general information about gpu usage and etc (in bash) .
for trace this output I use
$ watch -n 0.1 nvidia-smi
command,which updates informations every 0.1 second(0.1 second is minimum)
So I have a code like below in opencv(You can see I don't use any cuda function in it)
#include "opencv2/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
int main()
{
cv::Mat src(1500,1500,CV_8UC3),res;
for (int var = 0; var < 70; ++var)
{
Canny(src,res,50,150,5);
}
}
So when i run above code I take a screen shot in my nvidia-smi page(to know how much gpu ram is in use)
As you can see there is 23 MiB is in use !!! so why this happen !!if it is normal how we could stop that !? How to stop GPU for this problem
You can make the CUDA runtime indicate that there are no available GPUs with the following environment variable:
CUDA_VISIBLE_DEVICES="" ./my_opencv_code_that_wont_use_gpu
If you want OpenCV to actually not do anything with the GPU, my best guess would be to compile it without CUDA support:
cmake CMAKE_BUILD_TYPE=RELEASE \
CMAKE_INSTALL_PREFIX=/usr/local \
WITH_CUDA=ON \ ********MODIFY
ENABLE_FAST_MATH=1 \
CUDA_FAST_MATH=1 \ ********MODIFY
WITH_CUBLAS=1 \ ********MODIFY
INSTALL_PYTHON_EXAMPLES=OFF \
OPENCV_EXTRA_MODULES_PATH=/home/saeed/opencv_contrib/modules \
BUILD_EXAMPLES=OFF ..
I am running OpenCV 2.4.13.
I am using CvRTrees.train() and CvRTrees.predict() for a classification application.
Currently, the CvRTrees.train() function only uses one CPU core. I can see this in htop: one CPU is at 100% and the others hover around 3% when train() is called.
The docs says you have to explicitly enable parallelization support, so I have rebuilt the libaries with OpenMP support and reinstalled. However, the behavior is the same. Only one CPU is utilized by train().
Does anyone know what I am doing wrong? Are my expectations inaccurate?
Here are the steps I use to build and install:
Enabled OpenMP in cmake
$ mkdir release && cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_OPENMP=ON -D WITH_FFMPEG=OFF ..
Make and install
$ make && sudo make install
Run ldconfig so that the linker cache gets updated
$ sudo ldconfig
Rebuild my program and run ldd on the executable to ensure that it's using my newly installed libraries in /usr/local/lib
$ ldd ./build/release/classify | grep opencv
libopencv_core.so.2.4 => /usr/local/lib/libopencv_core.so.2.4 (0x00007f6e9d525000)
libopencv_ml.so.2.4 => /usr/local/lib/libopencv_ml.so.2.4 (0x00007f6e9d29e000)
Another note: If I add syntax errors around line 261 below, the build breaks, so I know I have the correct compiler flags set.
modules/core/src/parallel.cpp
-----------------------------
259 #elif defined HAVE_OPENMP
260
261 #pragma omp parallel for schedule(dynamic)
262 for (int i = stripeRange.start; i < stripeRange.end; ++i)
263 pbody(Range(i, i + 1));
Another note: ldd tells me that my application links libgomp.so, so I am reasonably sure that OpenMP is enabled.
I'm getting this error:
OpenCV-2.4.3/modules/features2d/src/freak.cpp:437: error: unable to
find a register to spill in class 'GENERAL_REGS'
After doing:
tar xfj OpenCV-2.4.3.tar.bz2
cd OpenCV-2.4.3
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..
make
The same procedure works on another machine. Any ideas?
You need to dig into the Makefiles to remove -O for freak.cpp.
UPDATE:
This is exactly how one should do it (tested with 2.4.3 and 2.4.4).
you need to edit
YOUR_BUILD_DIR/modules/features2d/CMakeFiles/opencv_features2d.dir/build.make
Search for freak.cpp. You find three blocks: Building CXX..., Preprocessing CXX..., and Compiling CX.... I just needed to change the Building part. The last line of that block looks like this:
.... YOUR_COMPILER $(CXX_DEFINES) $(CXX_FLAGS) ...
and if you check you find out that CXX_FLAGS has a -O3 in it. If you add -O0 after CXX_FLAGS it suppresses the O3. So your lines should look like this.
.... YOUR_COMPILER $(CXX_DEFINES) $(CXX_FLAGS) -O0 ...
This is at least working here!
I struggled with this for quite a few hours as well on my CentOS 5.x boxen, and here's my solution.
It's apparent you need to update 'gcc' but natively upgrading via RPM or just grabbing RPM's at random causing some serious config mgmt issues on your server. I don't have time to compile gcc/g++ via source right now either. After grazing out in the repo for a while, I found that there is, indeed, an 4.x release of gcc in the base repo.
Do this (or someone with 'root' to do it in case of OP who doesn't have access):
# yum install gcc44 gcc44-c++ -y
...CentOS/RHEL have bundled a preview RPM of gcc-4.4.6.
Then when you go to do 'cmake' to build your release environment, do at least the following (your cmake params may vary):
# cd /path/to/OpenCV-2.4.3
# mkdir release && cd release
# env CC=/usr/bin/gcc44 CXX=/usr/bin/g++44 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/place/to/install/ -D BUILD_PYTHON_SUPPORT=ON /path/to/OpenCV-2.4.3/
That will give you a successful build of OpenCV-2.4.3 natively with CenOS/RHEL 5.x.
Had the same problem and solved it like wisehippy with one slight change:
# yum install gcc44 gcc44-c++ -y
# mkdir release && cd release
# cmake -D CMAKE_BUILD_TYPE=RELEASE -DCMAKE_CXX_COMPILER=/usr/bin/g++44 -DCMAKE_C_COMPILER=/usr/bin/gcc44 <OpenCV_Dir>
I found the problem to be solved once I updated my c++ to point to g++44, instead of the default g++ which was 4.1.
As root verify that the files are the same before doing this step, it may not be necessary for you.
diff /usr/bin/c++ /usr/bin/g++
There should be nothing returned if the files are the same. If this is the case, continue.
Backup your old file. You can delete the file as well because it's the same as g++, but I like to be careful.
mv /usr/bin/c++ /usr/bin/c++4.1
Create a link so that C++ points to your g++44. You could use symbolic link here as well.
ln /usr/bin/g++44 /usr/bin/c++
Done.