openCV libraries call local path that does not exist - c++

i am making a simple c++ program with openCV library included. Eclipse IDE recognises openCV commands and library locations, but when i try to build the project, compiler gives external error, referring to opencv.hpp or core.hpp file calling a "opencv2/core.hpp" path, which does not exist in opencv folder. I figured out that the problem is linked to the way core.hpp is called, but the library files are read-only.
From what i saw in opencv.hpp file, this relative "opencv2/[module].hpp" reference is not only for the core, but all other modules as well. There is no opencv2 folder inside the one to where openCV is installed at all, in fact.
I've tried reinstalling and remaking openCV with different making arguments, using a different IDE and including direct search folders in eclipse. The problem, apparently, lies in the files themselves, or the way it maybe gets installed in the system the wrong way. The problem persists on both my main ubuntu machine and the ARMbian orange pi.
i get this error when trying to include any openCV library that contains
#include "opencv2/[opencv module].hpp" in it
as a result, compilation is terminated with the error message stating "/usr/local/include/opencv4/opencv2/opencv.hpp:52:28: fatal error: opencv2/core.hpp: No such file or directory"
edit 1: GCC c++ compiler options are -Iusr/local/include/opencv4/opencv2 -O3 -Wall -c -fmessage-length=0 and linker's options are -L/usr/local/lib.
The code is a simple displayImage
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
edit 2: $ pkg-config --libs opencv does not see openCV as installed in the system, altho i've made sure to run make install and ldconfig on the path. This may be a signal of faulty installation, but this is just a sidenote, not entirely related to main problem. I have tried reinstalls and to different folders, but this also persists as well as a main problem

apparently, #sgarizvi 's comment was the answer. I just needed to set the include path to I/usr/local/include/opencv4 and it worked. After that, the error was fixed.
I am replying to my own question to close the case, as i cannot upvote/veryfy a comment

In your case, since your include path is /usr/local/include/opencv4/opencv2
Replace the first three lines
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
by
#include <opencv.hpp>
#include <imgproc.hpp>
#include <highgui.hpp>

Related

Files/directories to include in Visual Studio C++ to use CUDA?

I want to use CUDA/GPU in OpenCV in Visual Studio. For example, cuda::GpuMat. I successfully build OpenCV with the extra modules with CUDA enabled
I tried the following code
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo/cuda.hpp>
#include <opencv2/photo.hpp>
using namespace std;
using namespace cv;
int main(){
string imageName("input.bmp");
//CPU version
Mat image = imread(imageName.c_str(), IMREAD_GRAYSCALE);
//CUDA version
cuda::GpuMat imageGPU;
cuda::GpuMat downloadGPU;
Mat buff;
imageGPU.upload(image);
downloadGPU.download(buff);
imwrite("gpu.bmp", buff);
return 0;
}
But I get an unhandled exception error.
I originally downloaded OpenCV in C:\Users\me\Downloads\opencv
I then downloaded and installed the latest OpenCV extra modules with CUDA on in
In Property Pages->C/C++->General->Additional Include Directories, I have:
C:\Users\me\Downloads\opencv\build\include\opencv
C:\Users\me\Downloads\opencv\build\include\opencv2
C:\Users\me\Downloads\opencv\build\include\
In Property Pages->Linker->General->Additional Library Directories, I have:
C:\Users\me\Downloads\opencv\build\x64\vc15\lib
and in Property Pages->Linker->Input->Additional Dependencies, I have:
opencv_world343d.lib
opencv_world343.lib
what else am I supposed to include so I can get GpuMat to work properly?
Most of the cases, yes, but you need to know which library you need to add, it may be cufft.lib, cublas.lib, cudnn.lib, etc. It depends of the function you use inside your code.
Opencv includes a cmake include file that would set all of this up for you if you use cmake to build you VS test project. This file will be in the root of the opencv install directory, i.e. after building opencv running cmake --install or the equivalent in VS. The file is OpenCVConfig.cmake, and it would be included in the CMakeLists.txt file for your project. Then you would call FindPackage(OpenCV), which would locate the OpenCV install, setup a few variables, which you would then use to link against your app.
I can post a sample CMakeList.txt file if you feel that would help.

Encountered Segmentation fault in simple OpenCV code

Tools
Platform : 64-bit Windows
Compiler chain: mingw with Qt
Make system: CMake
Libraries: C++ 11, OpenCV 4, Qt 5
Problem (Updated)
The following simple program segment should compile and display the generated image in OpenCV. However, it always SIGSEGVs in DEBUG mode only(Backtrace at the end). However, it works just fine in RELEASE mode.
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
void testOPENCV()
{
cv::Mat output(480, 640, CV_8UC3, cv::Scalar(255,0,100));
cv::namedWindow( "Test", cv::WINDOW_AUTOSIZE );
cv::imshow("Test",output);
cv::waitKey(0);
}
int main(int argc, char** argv)
{
testOPENCV();
return 0;
}
I have a CMake script that builds only the required OpenCV modules and links these to the dependencies. The relevant part:
build_external_project(opencv "https://github.com/opencv/opencv.git" "4.2.0" "-DCMAKE_INSTALL_PREFIX=${THIRDPARTY_INSTALLFOLDER} - DCMAKE_BUILD_TYPE=${THIRDPARTY_BUILDTYPE} -DBUILD_LIST=core,imgproc,imgcodecs,highgui")
target_link_libraries(OpenVideo ${OpenCV_LIBS})
The binary can be run with no missing dll errors. Dependency walker also indicates the same.
Here is the backtrace:
Given that OpenCV works fine in Release mode, I suggest rebuilding the Debug version of the library.
Previous answer:
There are a few potential problems with your code:
Using Qt is unnecessary on this example and it adds a complexity that you don't need right now. Remove it from the project and its libraries on the link instruction in the CMake script. Later, you can bring it back to see if it causes of the crash. Right now you need to pinpoint if the problem is in OpenCV or Qt.
An image can only be displayed on a window if cv::waitKey() is invoked;
The directory separator on Windows is usually \\ and not /;
This is the full source code to test your OpenCV build:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
int main()
{
std::string file_name("C:\\Images\\1.jpg");
cv::Mat original_image = cv::imread(file_name, cv::IMREAD_COLOR);
if (original_image.empty())
{
std::cout << "!!! image not found" << std::endl;
return -1;
}
cv::imshow( "Display window", original_image );
cv::waitKey(0);
return 0;
}
Few things to check here.
First, where do the OpenCV library come from? Is it compiled for your CPU? Looks like it crashed in AVX instructions. Might be that CPU does not support them.
Second, not obvious at all, happened to me with .png files. Same segfault during runtime. Turned out that OpenCV was built without png support. Please check if your OpenCV built with -DWITH_JPEG=ON.
https://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html
You forgot to create window
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
Adding to karlphillips answer: Deubg and Release behave quite differently on Windows than on Linux (due to runtime selection).
Especially if you link against release libraries on Windows but your libs or executable are being built in debug. If they use different runtimes you will be very likely run into issues and segfaults. So check the flags of both projects (typical culprits are flags like multithreading (debug) being present on one but not the other).

Mingw-64 will not compile openCV code after building and installing

I built and installed openCV using Cmake and mingw32-make. Afterwards I copied the produced "opencv2" source folder to the "include" folder of my installed mingw-64 compiler. I then copied the produced files from "lib" and "bin" to the corresponding folders of my installed compiler. I finally tried to compiler the following example code to ensure proper installation:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
Mat image;// new blank image
image = cv::imread("test.png", 0);// read the file
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// create a window for display.
imshow( "Display window", image );// show our image inside it.
waitKey(0);// wait for a keystroke in the window
return 0;
}
I tried to compile the code with the following command line command:
g++ -o helloWorld helloWorld.cpp
Which produced the following error:
helloWorld.cpp: In function 'int main()':
helloWorld.cpp:10:36: error: 'CV_WINDOW_AUTOSIZE' was not declared in this scope
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// create a window for display.
I suspect I did not properly install openCV, but when I search tutorials online for solving this issue it only is with respect to using codeblocks with mingw. I only wish to use mingw, not codeblocks.
Are there linker options that I am missing? Did I put the ".dll"s and ".dll.a"s in the wrong location?
Thanks
OpenCV was indeed installed correctly, the problem was that CV_WINDOW_AUTOSIZE is a constant used by the C implementation of openCV. When swapped with WINDOW_AUTOSIZE, the code then notified me that I did not link the proper libraries. For openCV 4.2.0, I needed to append "420" to the end of the needed libraries (for example: "-lopencv_core420").
The command line arguments to compile after these changes were made is:
g++ -o helloWorld helloWorld.cpp -lopencv_core420 -lopencv_highgui420 -lopencv_imgcodecs420

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.

OpenCV and convexityDefects

So i am really new to openCV and all the image recognition stuff. So i use monodevelop and i installed OpenCV using apt-getand i included these files
include "opencv2/highgui/highgui.hpp"
include "opencv2/imgproc/imgproc.hpp"
include <opencv2/opencv.hpp>
include <opencv2/imgproc/imgproc.hpp>
include<iostream>
include<vector>
include<algorithm>
include <X11/Xlib.h>
include <X11/Xutil.h>
but this function convexityDefects() shows as undefined
EDIT
So the problem is that the compiler shows that function convexityDefects is not defined in this scope
Here is the full code that i cant get to work -> Code
Could you please point me out!?
Thank you.
So i found the answer i was looking for.....
The libraries i used to compile my project where in wrong location.
so i used pkg-config --libs --cflags opencv to find exactly where are my libs....
After that i included these libraries in Code-Blocks and everything magicly started to work.