C++ Eclipse OpenCV : .exe file and binaries generated, but no image displayed - opencv

Here's my code (the first DisplayImage.cpp code in the OpenCV documentation)
/*
* DisplayImage.cpp
*
* Created on: Dec 25, 2011
* Author: Arcturus */
#include <iostream>
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv){
Mat image;
image = imread(argv[1], 1);
if(argc!=2 || !image.data){
cout<<"no image data";
return -1;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(10000);
return 0;
}
Build complete, executable generated, binaries generated.
I have my image - blackbuck.bmp- in the DisplayImage Debug folder. To run the code, I go to Run> Run Configurations. Select the DisplayImage Debug exe file, key in blackbuck.bmp (also tried it with absolute path) and run it.
On the top of the console, I get the message : DisplayImage Debug. And it displays no image at all. What could be wrong here?
I am running it on Eclipse, using CDT.
Thank you for your time!
EDIT: Problem solved!!! I had to copy all the dll files from the library folder to the folder in which my executable file was being generated. I still do not understand why, though. After all, the linker was already linking the library folder containing all the dlls. If someone could explain this, it would be of great help for future debugging. Thank you karl and mevotron for your time :)
EDIT 2: From the msdn website:
"A potential disadvantage to using DLLs is that the application is not self-contained; it depends on the existence of a separate DLL module. The system terminates processes using load-time dynamic linking if they require a DLL that is not found at process startup and gives an error message to the user. The system does not terminate a process using run-time dynamic linking in this situation, but functions exported by the missing DLL are not available to the program."
I think this answers my question. Perhaps this means eclipse uses load-time dynamic linking.

How did you compile OpenCV with MinGW (i.e., what were your BUILD_TYPE and SSE* options set to during the CMake configuration)? The reason I ask, is that there is a known bug with SSE optimizations that will cause highgui operations to crash when using MinGW built versions. See my other SO answer here.

Related

Why does my code crash when libvlc_media_new_path() is called?

after several days of trying myself to solve my problem I would like to kindly ask for your help:
I am trying to make the libvlc / SDL 2.0 tutorial working.
I am coding in Visual Studio 2022 in x86 C++ Console.
I have linked the libvlc library path and include path and have added the libvlc.lib file in my project linker settings.
The program compiles without error and crashes when libvlc_media_new_path is called.
You see all different formats of path I have used in my minimal reproducible example below:
My sources:
I downloaded the vlc master from Github to get the headers / include directory.
I downloaded the vlc-3.0.17.4-win32 release and from there took the libvlc.dll.
From the libvlc.dll I created the lib file following a visual studio command prompt procedure.
What i noticed is that the function libvlc_media_new_path() only takes the path as an argument now. All examples i find in the internet are with the libvlc instance AND the path as arguments.
Thank you so much for your help!
#include <stdlib.h>
#include "vlc/vlc.h"
int main(int argc, char* argv[]) {
libvlc_instance_t* libvlc;
libvlc_media_t* m;
libvlc_media_player_t* mp;
libvlc = libvlc_new(0, NULL);
if (NULL == libvlc) {
printf("LibVLC initialization failure.\n");
return EXIT_FAILURE;
}
m = libvlc_media_new_path("/1.mp4");
//m = libvlc_media_new_path("C:\\Programmieren\\PACA\\1.mp4");
//m = libvlc_media_new_path("C:/Programmieren/PACA/1.mp4");
//m = libvlc_media_new_path("C://Programmieren//PACA//1.mp4");
//m = libvlc_media_new_path("C:\Programmieren\PACA\1.mp4");
//m = libvlc_media_new_path("file:///C:/Programmieren/PACA/1.mp4");
mp = libvlc_media_player_new_from_media(libvlc, m);
return 0;
}
If you go to Github and click on the Tags link, you can get the headers for version 3.0.17.4. In there you will see that libvlc_media_new_path takes an instance as an argument.
The other option would be to get or build the 3.0.18 DLL.

why its not easy to install opencv as you can see in tutorials?

installing some libraries like opencv is hard unfair game with this level:(but it's seems easy when you are watching youtube:) )
Error 1 : strcpy_s and strcat_s are not declared in this namespace. To resolve it use the function strncpy and strncat respectively. The format of latter is different from previous one.
strncpy( szKey, icvWindowPosRootKey, 1024 );
strncat( szKey, name, 1024 );
Error 2 : time was not found in this scope.
Error 3 :modules\videoio\CMakeFiles\opencv_videoio.dir\build.make:146: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj' failed
Error 4 : recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
mingw32-make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all
Error 5 :recipe for target 'all' failed
Error 6 : ‘M_PI’ was not declared in this scope
Error 7 : ‘posix_memalign’ was not declared in this scope
Error 8 :‘D3D11_TEXTURE2D_DESC’ was not declared in this scope
Error 9 :opencv 'nullptr' was not declared in this scope
Error 10 : 'mutex' in namespace 'std' does not name a type
Error 11 :expected unqualified-id before '>' token
Error 12 :limpc-3.dll was not found opencv
Error 13 :recipe for target pch_Generate_opencv_core.dir/all' failed
Error 14 :'modules/core/precomp.hpp.gch/opencv_core_Release.gch' failed opencv 4
fallow these 2 tutorials if have some problems with opencv and test different vertion of mingw cmake and codeblock :
https://blog.faltutech.com/2018/c-plus-plus-c/09/03/compile-and-integrate-opencv-3-4-1-into-codeblocks-17-12-with-gcc-5-1-0/#
https://blog.huihut.com/2018/07/31/CompiledOpenCVWithMinGW64/
second tutorials is more important(blog.huihut.com)
:::::
(this is first tutorials)
Needed Tools:
OpenCv 3.4.1 Windows self-extracting archive Download
CodeBlocks 17.12 Without Mingw Download
Cmake 3.12 .msi Download
TDM-GCC-64 (tdm64-gcc-5.1.0-2.exe) Download //i use this vertion :x86_64-5.3.0-release-posix-seh-rt_v4-rev0
Common Sense Download 🙂
Steps:
Install and open Cmake
Download and Extract Opencv (exe is a type of package. It will ask you the path when you open the opencv.exe) in directory c:\Opencv
Create new directory “opencv_codeblocks” in C:\opencv
Download and Install TDM-GCC-64 in directory C:\TDM-GCC-64 and add the path “C:\TDM-GCC-64\bin” to the environment variables. Watch below video to know how to add the the path to environment variables.
Open CMake.
Select path “C:/Opencv/opencv/sources” in Option Where is the source code
Select path “C:/Opencv/Opencv_codeblocks” in Option Where to build the binaries. These are same folder we have created earlier.
Click Build.
Now you will see some options in table like structure. Few will be checked and few are not. So make sure about these four :
PRECOMPILED _HEADERS : Uncheked
MFMS : Unchecked
IPP : Unchecked
Builed Opencv world : Checked
Click build again and after that click generate.
Now in directory Opencv_codeblocks a codeblock project file will be created with extension cbp. Double click on it.
Before building we have to configure Codeblocks.
Go to Settings->compiler
From Drop Down Select GNU GCC Compiler
Select option Toolchain executables
In option compiler’s installation directory choose the path “C:\TDM-GCC-64” Which is the path of installation of GCC compiler .
Click on Auto Detect. And click on OK in popup.
Go to tab Compiler Flags and Select option “Have g++ follow C++11 ISO C++ language standard [-std=c++11]”
Click OK
Now From Build option in menu bar click on Build. Or See for following symbols.
There Will Few Errors those will be shown while building :
Error 1 : strcpy_s and strcat_s are not declared in this namespace. To resolve it use the function strncpy and strncat respectively. The format of latter is different from previous one.
strncpy( szKey, icvWindowPosRootKey, 1024 );
strncat( szKey, name, 1024 );
The Error1 will be shown 4 or 5 times. So change the functions to the equivalents. Save the changes each time using Ctrl+s and click build every time you make changes to files.
Error2 : time was not found in this scope. So resolve it add header file time.h (#include )to the file top in which the error is shown. Save and Click Build again.
Go to : Build->Select Target and select “install” option.
Click Build option Again. The above process will take some time. On Successful build continue to following steps. Otherwise try to remove them if you can.
Do Some copying :
Copy opencv and opencv2 folders from “C:\opencv\build_codeblocks\install\include” to “C:\TDM-GCC-64\x86_64-w64-mingw32\include”
Copy libopencv_world343.dll.a (number 343 can change with new release of Opencv) from “C:\opencv\build_codeblocks\install\x64\mingw\lib” to “C:\TDM-GCC-64\x86_64-w64-mingw32\lib”
Copy Everything from “C:\opencv\build_codeblocks\install\x64\mingw\bin” to “C:\TDM-GCC-64\bin”
Now create a new project in codeblocks and write your code with the inclusion of required header files by your code (Read Documents of OpenCV). example code :
#include <opencv2/core/core.hpp> //additional space in header address could cause "not found" problems
#include <opencv2/highgui.hpp >
#include <iostream>
using namespace cv;
using namespace std;
int main() {
Mat image1, image2;
image1 = imread("cross.png", CV_LOAD_IMAGE_COLOR);
if (!image1.data) {
cout << "could not find image1" << endl;
}
image2 = imread("cross.png", CV_LOAD_IMAGE_GRAYSCALE);
if (!image2.data) {
cout << "could not find image2" << endl;
}
cout << "opencv test" << endl;
namedWindow("Color Image", WINDOW_AUTOSIZE);
imshow("Color Image", image1);
namedWindow("Gray Scale Image", WINDOW_AUTOSIZE);
imshow("GRAY Scale Image", image2);
waitKey(0);
destroyAllWindows();
return 0;
}
Before building right click on project in left side pane and select build options.
Go to linker settings and click Add.
Write “opencv_world343” in input box and click ok. Note that 343 can change as said above. So 343 should same as the file you copied above.
Now you can build and run the code.//you can also add opencv lib and include folder in search directory like me
If you do not have to code in C++, you can install OpenCV in your Python environment using pip install opencv-python or pip install opencv-contrib-python if you also want contib module. The detailed information is here. You may also use Anaconda for installing OpenCV with command conda install -c conda-forge opencv. In my opinion, using Anaconda makes things a lot easier and I recommend it.

How to make use of OpenCV source codes instead of shared libraries

I have a project at hand which I want to use one of the opencv modules (specifically dnn).
Instead of building the dnn module I want to use the source code of this modules in my project. by doing so, I can change the source code live and see the results at the same time.
I have a very simple scenario with one only source file:
main.cpp
#include "iostream"
#include <opencv2/dnn.hpp>
int main(int argc, char *argv[])
{
std::string ConfigFile = "tsproto.pbtxt";
std::string ModelFile = "tsmodel.pb";
cv::dnn::Net net = cv::dnn::readNetFromTensorflow(ModelFile,ConfigFile);
return 0;
}
now this function "cv::dnn::readNetFromTensorflow" is in dnn module. I tried many different methods to embedded the dnn source codes inside my project but all of them failed !
for example, the first time I tried to include every cpp and hpp file in the module/dnn/ folder of opencv in my project but I ended up in errors like
/home/user/projects/Tensor/tf_importer.cpp:28: error: 'CV__DNN_EXPERIMENTAL_NS_BEGIN' does not name a type
#include "../precomp.hpp" no such file or directory
HAVE_PROTOBUF is undefined
and ....
I tried to solve these errors but some more errors just happened, more undefined MACROs and more undefined hpp files !
#include "../layers_common.simd.hpp" no such file or directory
and many many more errors !
It seems that I'm stuck in a while(true) loop of errors !!! Is it really that hard to use opencv modules source code ?
P.S.
For those who are asking about why I want to use opencv source code instead of using the shared libraries I have to say that I want to import a customized tensorflow model which opencv read function doesn't support and I want to know where exactly it crashesh so I can fix it.
By the way, I am only using c++11 functions and gcc as compiler in Ubuntu 16.04

How to use opencv cuda module log function

I'm trying to work with Opencv CUDA module, specially refer to cv::cuda::log function.
First, I'll give to details Opencv compilation.
I compiled Opencv with WITH_CUDA flag on, took the libs and dlls from the compilation, however I copied the headers files from the downloaded opencv folder, since the compilation folder does't include headers by default.
I wonder, whether is this the right thing to do ?
Second, I tried to use the cv::cuda:: function.
I include the cuda.hpp header
#include "opencv2/core/cuda.hpp"
cv::cuda::GpuMat source, dest;
GpuMat compiles great for me, However I don't know which file should I include in order to work with the log function. when I write the following line
cv::cuda::log(source, dest);
I kept on getting the error message:
error: C2039: log in not a member of cv::cuda
Windows 7, Visual studio 2013, Opencv 3.0.0, platform: 64 bit, CUDA toolkit 6.5
Third, I'd like to know about Opencv CUDA implementation, does it utilize npp functionality? Opencv vs npp, which one is better to use ?
I could easly write my code using npp, however I'd like to know the opencv CUDA module.
Thanks
After couple days of searching, I'd like to share my knowledge
First thing I was doing wrong, was taking the headers from Opencv compilation, the right thing to do is taking the header from all the Opencv modules (each module individually).
Second, After Opencv compilation with CUDA flag everything worked great.
Third, Several opencv CUDA functions does utilize NPP
Forth, Use github
This code should work for OpenCV 3.1:
#include <opencv2/opencv.hpp>
#include <opencv2/cudaarithm.hpp>
int main()
{
cv::Mat img = cv::imread("img.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat img_32f;
img.convertTo(img_32f, CV_32F);
//To avoid log(0) that is undefined
img_32f += 1.0f;
cv::cuda::GpuMat gpuImg, gpuImgLog;
gpuImg.upload(img_32f);
cv::cuda::log(gpuImg, gpuImgLog);
cv::Mat imgLog, imgLog_32f;
gpuImgLog.download(imgLog_32f);
double min, max;
cv::minMaxLoc(imgLog_32f, &min, &max);
imgLog_32f.convertTo(imgLog, CV_8U, 255.0/(max-min), -255.0*min/(max-min));
cv::imshow("img", img);
cv::imshow("imgLog", imgLog);
cv::waitKey(0);
return 0;
}

VideoCapture OpenCV 2.4.2 error in windows

I have a problem using VideoCapture class with OpenCV 2.4.2 under windows XP 32bits.
It doesn't open any file or camera and fixing it's being a pain.
Im using visual studio 2010 but i have also tried the code in QTcreator with the same result.
The testing code is the following:
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace cv;
using namespace std;
int main()
{
const char* videoPath = "C:/video/";
string videoName = string(videoPath) + "avi.avi";
VideoCapture cap(videoName);
if(!cap.isOpened())
{
std::cout<<"Fail"<<std::endl;
return -3;
}
return 0;
}
The output is always '-3'.
Qt Creator shows a
warning: Error opening file (../../modules/highgui/src/cap_ffmpeg_impl.hpp:361)
I debugged it and the problem appears in the first line of:
CvCapture* cvCreateFileCapture_FFMPEG_proxy(const char * filename)
{
CvCapture_FFMPEG_proxy* result = new CvCapture_FFMPEG_proxy;
if( result->open( filename ))
return result;
delete result;
#if defined WIN32 || defined _WIN32
return cvCreateFileCapture_VFW(filename);
#else
return 0;
#endif
}
in the cap_ffmpeg.cpp internal file.
I have tested the same code in a mac under snow leopard and it works. No surprises here since it must be a library issue.
I have opened the avi file with the same path route using the c-function cvCapture easy and fast.
I got all the dlls of 'C:\opencv\opencv\build\x86\vc10\bin'
included in mi debug file. I got the tbb.dll and all the 'C:\opencv\opencv\3rdparty\ffmpeg' content included too.
This is drving me crazy so any help would be appreciated.
Thanks in advance.
In my case, the same problem was resolved after deleting all opencv_***.dll files in C:\Windows\System32. So, I use the dll files just through the path like "%PATH%;C: \Program Files \OpenCV2.4.2\build\x86\vc10/bin". Please try it.
I also faced with this problem and solved it by correct the path of the function:
VideoCapture cap(videoName);
If the AVI file of videoName does't exist, it will be an error:
(../../modules/highgui/src/cap_ffmpeg_impl.hpp:XXX)
where XXX represents the line number.
I had the same issue with the open method whilst running under Windows 8 (64bit), opencv 2.4.10. IDE is running in x86.
I found that running the application in release configuration solved the problem.
Stumbled across the answer because I had the same issue with imread. Issue is presented in the this thread.
imread not working in Opencv
See the fix I found below, for mp4 files.
I faced the same issue on Windows 7, using OpenCV 2.4.9. I am using the java wrapper for opencv.
Matthias Krings has done a lot of research for this. See this. Apparently this is an issue based on the video file type. With .avi files, it seems to work for a lot of people. Unfortunately his solution of setting OPENCV_DIR did not work for me. But his comments in the bug listing gave me a hint to fix the issue.
You have to do two things:
Set java.library.path to include the directory {opencv\install\dir}opencv-2.4.9\build\x86\vc10\bin. You can set the variable using the -D option on the java command line: java -Djava.library.path=PATH_TO_YOUR_DLL .... Also fetch this variable from your environment, using System.getProperty(...), and print it before calling loadLibrary(), to verify that the path setting is working.
And in your java class, load the ffmpeg dll using System.loadLibrary("opencv_ffmpeg249");. The loadLibrary() function should be invoked from within a static block in java.
There is a file named opencv_ffmpeg249.dll in the java.library.path that we set.
This works on ubuntu also, for .so files.
I too faced the same issue and resolved after pointing to the correct location of the input video.

Resources