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

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

Related

opencv program compiled, but it does not execute ( c++ )

I found a below program in c++ on internet,
#include <opencv2/opencv.hpp>
using namespace cv;
int main(void) {
// Read image in GrayScale mode
Mat image = imread("emily.jpg", 0);
// Save grayscale image
imwrite("emilyGray.jpg", image);
// To display the image
imshow("Grayscale Image", image);
waitKey(0);
return 0;
}
Then I compiled the above program with the following,
g++ opencv1.cpp -o opencv1.exe -IC:\\opencv\\build\\install\\include -LC:\\opencv\\build\\install\\x64\\mingw\\lib -lopencv_calib3d452 -lopencv_core452 -lopencv_highgui452 -lopencv_imgcodecs452
After running opencv1.exe, theres no change, no image being created.
Note:- I am using mingw for compiling and my opencv is built with mingw and cmake.
Actually I did not set the path to opencv dlls after I built opencv using mingw32-make.
After setting C:\opencv\build\bin to PATH variable of environment variables I could see the output.
Regards

MinGW C++ compiler cant find OpenCV in the system path

So I'm getting an error stating "fatal error: opencv2\core\mat.hpp: No such file or directory" when trying to compile a small c++ file from .cpp with MinGW. It happens in one form or another in Sublime Text 3, Atom, and on the command line with "gcc myfile.cpp". I should note that I'm on Windows 10. Here's the code:
#include <iostream>
#include <opencv2\core\mat.hpp>
#include <opencv2\core.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\core\types.hpp>
#include <opencv2\imgproc.hpp>
int main()
{
std::cout << "Hello World!\n";
cv::Mat img = cv::imread("despair.jpg");
cv::cvtColor(img, img, cv::COLOR_RGBA2GRAY, 1);
cv::imshow("1", img);
cv::waitKey(0);
cv::Mat mask = cv::Mat(img.rows, img.cols, CV_8UC1, cv::Scalar(255));
cv::Rect_ <char> roi = cv::Rect_ <cv::Point>(cv::Point(0, 0), cv::Point(10, 10));
cv::rectangle(mask, roi, cv::Scalar(0), -1);
cv::Scalar colors = cv::mean(img, mask);
}
Things I've done and tried:
Adding C:\OpenCV\opencv\build\include\ to the system path.
Adding the bin and lib folders in x64/vc15 folder to the system path.
Messing and experimenting with the system path in general to try to find anything that might work. (I've tried so many combinations at this point I've forgotten most of them)
Adding everything to my user specific path.
Changing the include directory in the .cpp file to be the absolute path to each file I need to include (This works for the exact files I need, but then fails when those files try to include other files they need, like when Mat.hpp tries to include Matx.hpp)
Reinstalling MinGW64.
Installing MinGW to different folders and updating the system path to reflect the new location.
Changing which text editor I'm using
Compiling from the command line outside of a text editor
Things in my system path right now:
C:\OpenCV\opencv\build\x64\vc15\lib
C:\OpenCV\opencv\build\x64\vc15\bin
C:\OpenCV\opencv\build\include\
C:\MinGW\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin
I took this code out of a larger project to test what was going on with it and at this point I'm at my wits end trying to figure out what's wrong. Please send help.
Add -I option with path to opencv includes and -L with path to opencv libraries, like this:
gcc -IC:\OpenCV\opencv\build\include -LC:\OpenCV\opencv\build\x64\vc15\lib test_opencv.cpp

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).

openCV libraries call local path that does not exist

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>

OpenCV 3.4.0 - zlibd1.dll was not found

I have the following program, which is the same as this tutorial page for OpenCV 3.4.0. I am using Visual Studio 2017 Community on a 64-bit laptop with Windows 10 Enterprise 64-bit.
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", 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;
}
With the includes and linker directories and libraries set up for this project, the solution builds just fine. But when I run the program, I get the following error:
"The code execution cannot proceed because zlibd1.dll was not found. Reinstalling the program may fix this problem."
From my initial research, it's not necessarily coming from Visual Studio 2017. When I try to create a new project, I do not have the option to select "Win32 Console Application." The project this source code sits in is of type Windows Console Application under Visual C++, supporting .NET Framework 4.5.
What am I missing here?
Based on the tutorial, I should be using only the libraries and the dynamic link libraries OpenCV 3.4.0 provides. I remember at one point using NuGET to try to install OpenCV 3.4.0 again, as explained here, when I was trying to solve the compiler error regarding fopen from a file within OpenCV 3.4.0 itself.
Okay, apparently it was a problem regarding my system path. It was not set right, Before, what I had was the path variable set to the following:
%OPENCV_DIR%\lib
%OPENCV_DIR%\bin
with $(OPENCV_DIR) being just the build directory of OpenCV 3.4.0.
But the directories there do not exist, and hence the libraries couldn't be found. So, I replaced them with this:
%OPENCV_DIR%\x64\vc15\bin
%OPENCV_DIR%\x64\vc15\lib
and the program now runs. What was I thinking back there?
Either way, important lesson to note: When you are getting a popup message saying the program cannot be opened because a library is missing, and is part of OpenCV 3.4.0, be sure that your system path in the Windows 10 System Advanced Settings is an OpenCV directory that exists.
This package has a problem to run under debug and result in zlibd1.dll problem. Switch to release if you can or use different nuget package.