Visual Studio double execution just adding OpenCV directory - c++

i noted that when i'm working with OpenCv and i run the debug it runs two times! Even if the code is just an empty main(). I configured VS in this way:
C/C++>>General>>Additional Include Directories: $(OPENCV_DIR)\Include
Linker>>General>> Additional library Directories: $(OPENCV_DIR)\x86\vc12\lib
Linker>>input>> Additional dependancies
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_ml249d.lib
opencv_nonfree249d.lib
opencv_objdetect249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_videostab249d.lib
then i've done the same in the release using the .lib files without 'd'. Just doing like that i've the double execution, also with the following minimal code (note that the code is all under comment):
//#include <opencv2/core/core.hpp>
//#include <opencv2/highgui/highgui.hpp>
//#include <iostream>
//#include "opencv2/imgproc/imgproc.hpp"
//using namespace cv;
//using namespace std;
int main(int argc, char* argv[])
{
///Mat M(10, 5, CV_16UC1, Scalar::all(0));;
/// cout << M;
}
Another clue: in a more complex code, i use waitKey(0) and when i run the debug it runs the first time... after few seconds it stops and runs the second time... I noted that in the first run i cannot stop using the toolbar of VS (the 'stop' squared red button or the 'pause' one) while in the second run i can stop it using the toolbar. I tried creating a simple "hello world" code WITHOUT the OpenCv configurations and it runs just one time. So that i think that there is something wrong in my configurations.

Related

How to deal with "DNN module was not built with CUDA backend; switching to CPU" warning in C++?

I am trying to run YOLOv3 on Visual Studio 2019 using CUDA 10.2 with cuDNN v7.6.5 on Windows 10 using NVidia GeForce 930M. Here is part of the code I used.
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace dnn;
using namespace std;
int main()
{
// Load names of classes
string classesFile = "coco.names";
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line)) classes.push_back(line);
// Give the configuration and weight files for the model
String modelConfiguration = "yolovs.cfg";
String modelWeights = "yolov3.weights";
// Load the network
Net net = readNetFromDarknet(modelConfiguration, modelWeights);
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA);
// Open the video file
inputFile = "vid.mp4";
cap.open(inputFile);
// Get frame from the video
cap >> frame;
// Create a 4D blob from a frame
blobFromImage(frame, blob, 1 / 255.0, Size(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
// Sets the input to the network
net.setInput(blob);
// Runs the forward pass to get output of the output layers
vector<Mat> outs;
net.forward(outs, getOutputsNames(net));
}
Although I add $(CUDNN)\include;$(cudnn)\include; to Additional Include Directories in both C/C++ and Linker, added CUDNN_HALF;CUDNN; to C/C++>Preprocessor Definitions, and added cudnn.lib; to Linker>Input, I still get this warning:
DNN module was not built with CUDA backend; switching to CPU
and it runs on CPU instead of GPU, can anyone help me with this problem?
I solved it by using CMake, but I had first to add this opencv_contrib then rebuilding it using Visual Studio. Make sure that these WITH_CUDA, WITH_CUBLAS, WITH_CUDNN, OPENCV_DNN_CUDA, BUILD_opencv_world are checked in CMake.
I had a similar issue happen to me about a week ago, but I was using Python and Tensorflow. Although the languages were different compared to C++, I did get the same error. To fix this, I uninstalled CUDA 10.2 and downgraded to CUDA 10.1. From what I have found, there might be a dependency issue with CUDA, or in your case, OpenCV hasn't created support yet for the latest version of CUDA.
EDIT
After some further research it seems to be an issue with Opencv rather than CUDA. Referencing this github thread, if you installed Opencv with cmake, remove the arch bin version below 7 on the config file, then rebuild/reinstall Opencv. However, if that doesn't work, another option would be to remove CUDA arch bin version < 5.3 and rebuild.

Struggling with dlib, linker errors and save_jpeg in XCode

First, I am new to C++ and dlib but I have successfully built the examples and started working on my own project. Things have been progressing smoothly until I try to save a jpeg. Attempting to compile code using dlib::save_jpeg throws a linker error and I cannot track down the solution. I have attempted to add #define DLIB_JPEG_SUPPORT above and below my #includes but no luck. I am using XCode and used cmake -G "Xcode" .. when I compiled the examples. Relevant code below. Since I am on a Mac, I have added header and library search paths for X11 (for dlib gui), OpenCV, and DLIB. I have libjpeg.dylib and linked that to my project with and without #define DLIB_JPEG_SUPPORT in main.cpp. Is there some other build setting I need to specify? Thank you in advance for your help.
Finally, I have seen other questions and pages about dlib and libjpeg issues but no luck yet. And yes I have source.cpp included in the project.
// the standard stuff
#include <string>
#include <iostream>
#include <unistd.h>
// opencv mat object
#include <opencv2/opencv.hpp>
// dlib>
#include <dlib/opencv.h>
#include <dlib/image_io.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
int main(int argc, const char * argv[]) {
// retrieving images from a TCP connection
// decode data stream
img = cv::imdecode(rawImage, CV_LOAD_IMAGE_COLOR);
// perform image processing
dlib::cv_image<dlib::bgr_pixel> d_image(img);
// finally save the result to jpg
std::string fname = argv[1] + std::to_string(image_id) + ".jpg";
dlib::save_jpeg(d_image, fname); // <- line that won't compile
return 0;
}
After quit a bit of struggling and side-by-side comparisons I finally found the issue. In XCode go to to Build Settings and modify Other Linker Flags, Run Search Paths, and Other C++ Flags to match the compiled and working face_ex example. I wholesale copied all of those flags and included a missing libjpeg.dylib and was able to get things running. It should look something like this for the C++ flags . Hope this helps the next person.

Using Qt to configure OpenCV library failed

I'm new to Qt creator. Yesterday, I followed the official instructions to configure OpenCV library but it failed. I tried everything on the Internet but it just didn't work. Detailed are listed as belows:
test code is simple, I only want to ensure whether the library works:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello World!" << endl;
return 0;
}
My project configuration is like below :
I thought there might be a problem in debugger. I configure the debugger and I'm quite sure it's ok. The picture is here:
But it just doesn't work. When I click build and run, it says:
C1083: cannot open containing files: "opencv2/opencv.hpp": No such
file or directory.
What's strange is when I include <files> in the editor, the automatic code completion can detect the existence of the OpenCV library and hint after <opencv2/> that there are opencv.hpp, core.hpp .etc. and in the Include Hierarchy, the opencv.hpp exists.
So what might be the problem?

Opencv Mingw Build

After trying without succed to install opencv with Mingw and codeblocks, i finnaly give up and download the precompiled libs from :
https://github.com/drewcrawford/drew-face/tree/master/opencv/build
But when i try to run some opencv code, my application crashed when it start and i have this message :
"The application was unable to start correctly (0xc0000005). Click OK to close the application."
Her is my code test :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
cv::Mat image;
image = cv::imread("4010194A00101a_090.bmp",0);
return 0;
}

Fatal error in starting up OpenCV 2.4.6 on VS 2008, file not found

I am using this tutorial to get started with OpenCV 2.4.6 on VS 2008:
http://docs.opencv.org/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html
I followed all instructions (my OpenCV is not in the default Program Files (x86) folder, it's in
C:\opencv_built
Unlike in the tutorial, I put very simple code, just to make sure all included files are reachable and if it builds successfully etc:
#include "stdafx.h"
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
using namespace std;
using namespace cv;
double getPSNR ( const Mat& I1, const Mat& I2);
Scalar getMSSIM( const Mat& I1, const Mat& I2);
int main(int argc, char *argv[])
{
return 0;
}
But I get a fatal error when I try to build:
fatal error C1083: Cannot open include file: 'opencv2/imgproc/imgproc.hpp': No such file or directory c:\Users\Administrator\Documents\Visual Studio 2008\Projects\firstopencv\firstopencv\firstopencv.cpp 17
This is obviously referring to this line:
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
I don't know where to find the dll files, or what to do next? I know this must be really easy but I've searched for any of the dll files, eg.
opencv_core243d.lib
but I get no search results.
1. Check your path to \vc10 folder. It should be either:
C:\opencv_built\build\x86\vc10
or
C:\opencv_built\x86\vc10
2. Go to
Start>Edit environment variables for your account>Under System variables > New...
Variable Name: OPENCV_DIR
Variable Value: Insert your path from step 1 here.
3. Open Visual Studio, make new project, go to Property Pages
4. Under C/C++ > Additional Include Directories
Insert $(OPENCV_DIR)\..\..\include
5. Under Linker > General > Additional Include Directories
Insert $(OPENCV_DIR)\lib
6a. (For DEBUG property!) Under Linker > Input > Additional Dependencies
Insert
opencv_core246d.lib
opencv_imgproc246d.lib
opencv_highgui246d.lib
opencv_ml246d.lib
opencv_video246d.lib
opencv_features2d246d.lib
opencv_calib3d246d.lib
opencv_objdetect246d.lib
opencv_contrib246d.lib
opencv_legacy246d.lib
opencv_flann246d.lib
6b. (For RELEASE property!) Under Linker > Input > Additional Dependencies
Insert
opencv_core246.lib
opencv_imgproc246.lib
opencv_highgui246.lib
opencv_ml246.lib
opencv_video246.lib
opencv_features2d246.lib
opencv_calib3d246.lib
opencv_objdetect246.lib
opencv_contrib246.lib
opencv_legacy246.lib
opencv_flann246.lib
This should be enough. If you get missing .dll window after running code, copy desired .dll from your C:\opencv_built\build\x86\vc10\bin or C:\opencv_built\x86\vc10\bin to your project folder.
It's not a dll file, it's a header file. The file is called imgproc.hpp
If you compiler can't find it it's either because it isn't there, or it's because you haven't told your compiler where to find it.
The important part for VS is the “Additional Include Directories”, look at that part again on the web page you followed.
Okay the fix was this:
Under Linker -> General -> Additional Library Dependencies
I put:
C:\opencv_built\lib\Debug