Installing opencv + fatal error: 'opencv2/core.hpp' file not found - opencv

I have installed open cv using the official installation process given at:
https://docs.opencv.org/4.5.2/d0/db2/tutorial_macos_install.html
I am doing this on mac and it took ~3 hours for this process. After installation, the installed directory looks like this:
apple#Apples-MacBook-Air.local:~/Learning/openCV/cpp$ pwd
/Users/apple/Learning/openCV/cpp
apple#Apples-MacBook-Air.local:~/Learning/openCV/cpp$ ls
3rdparty CTestTestfile.cmake bin data
opencv_data_config.hpp setup_vars.sh
CMakeCache.txt Makefile cmake_install.cmake doc
opencv_lapack.h test-reports
CMakeDownloadLog.txt OpenCVConfig-version.cmake cmake_uninstall.cmake
include opencv_python_config.cmake tmp
CMakeFiles OpenCVConfig.cmake configured lib
opencv_python_tests.cfg unix-install
CMakeVars.txt OpenCVModules.cmake custom_hal.hpp modules
opencv_tests_config.hpp version_string.tmp
CPackConfig.cmake apps cv_cpu_config.h opencv
python_loader
CPackSourceConfig.cmake basicOp.cpp cvconfig.h opencv2
samples
Now, I am writing a very simple program to test the installation and it looks like that I need to do more things than what I have done so far.
My simple program:
#include<iostream>
#include<opencv2/core.hpp>
int main() {
std::string imagePath = samples.findFile("starry_night.jpg");
cv::Mat imageMatrix = cv::imread(imagePath);
if(imageMatrix.empty()) {
std::cout << "Matrix is empty" << std::endl;
}
}
Compiling this gives error as:
apple#Apples-MacBook-Air.local:~/Learning/openCV/programs$ g++ imageRead.cpp
imageRead.cpp:2:9: fatal error: 'opencv2/core.hpp' file not found
#include<opencv2/core.hpp>
No problem, I can probably understand that due to non-linkage of the open cv libraries.
So, I compiled with:
apple#Apples-MacBook-Air.local:~/Learning/openCV/programs$ g++ imageRead.cpp -I/Users/apple/Learning/openCV/cpp/include -L/Users/apple/Learning/openCV/cpp/lib/
imageRead.cpp:2:9: fatal error: 'opencv2/core.hpp' file not found
#include<opencv2/core.hpp>
And it still give me the same error. Basically, I passed the paths for gcc to consider for linking.
Any suggestions? I can help providing more details on it, if needed.

Did you try #include<opencv2/opencv.hpp> instead of #include<opencv2/core.hpp>?

Related

Yocto: cmake is missing header - how to DEPEND dev-package?

I am including opencv with custom build parameters in my Yocto image. For that I have an opencv_4.1.0.bbappend recipe, in which I set custom options, specifically FFMPEG. The recipe goes something like this:
DEPENDS += "ffmpeg libpng"
EXTRA_OECMAKE_append += "-DWITH_FFMPEG=ON -DWITH_GTK=OFF" # and some other options
During configure I get cmake errors and can't seem to figure out, how to satisfy the header dependencies. The errors go like this (I assume this is the reason for do_configure to fail):
CheckIncludeFile.c:1:10: fatal error: /home/janos/dev/yocto/build/tmp/work/core2-64-poky-linux/opencv/4.1.0-r0/recipe-sysroot/usr/include/libpng/png.h: No such file or directory
1 | #include </home/janos/dev/yocto/build/tmp/work/core2-64-poky-linux/opencv/4.1.0-r0/recipe-sysroot/usr/include/libpng/png.h>
CheckIncludeFile.c:1:10: fatal error: sys/videoio.h: No such file or directory
1 | #include <sys/videoio.h>
Focusing on the missing png.h header first, I am tempted to depend libpng-dev, as I also would apt install it. But there is no package for it.
When I search oe-pkgdata-util list-pkg-files -p libpng, I can find the header in a libpng-dev package:
...
libpng-dev:
/usr/bin/libpng-config
/usr/bin/libpng16-config
/usr/include/libpng16/png.h
/usr/include/libpng16/pngconf.h
/usr/include/libpng16/pnglibconf.h
/usr/include/png.h
...
...
I can also find it in libpng-src and also ffmpeg-src package (oe-pkgdata-util find-path "*png.h" was my friend). But all of these -dev and -src packages I cannot depend on in DEPENDS.
How can I get my recipe to know those headers?
Target machine is raspberrypi4-64, on which the recipe is configuring and compiling well - it fails when I build for qemux86-64, which I use for testing. Namely, my test command is MACHINE="qemux86-64" bitbake opencv.
It doesn't really answer the question which I though was the question - but this is how the opencv recipe is easily configured:
PACKAGECONFIG = "python3 libav libv4l v4l"
Looking into the opencv 4.1.0 recipe (opencv_4.1.0.bb), I could see that FFMPEG gets enabled with the libav configurable option.
As a result of depending FFMEPG, I had to whitelist "commercial" licenses in my local.conf file:
LICENSE_FLAGS_WHITELIST = "commercial"
Looking into ./build/tmp/work/aarch64-poky-linux/opencv/4.1.0-r0/temp/log.do_configure shows that opencv is correctly configured without GUI, with v4l/v4l2:, FFMPEG, python3, etc.
And so python3 in the resulting image:
import cv2
print(cv2.getBuildInformation())

fatal error: zbar.h: No such file or directory

I downloaded the zbar library and cloned the opencv-zbar repo in my home directory. Then I ran the following command $cmake . which gave my the error
-- Could NOT find ZBAR (missing: ZBAR_LIBRARIES ZBAR_INCLUDE_DIR)
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
ZBAR_LIBRARIES
linked by target "zbar_opencv" in directory /home/surabhi/opencv-zbar
-- Configuring incomplete, errors occurred!
See also "/home/surabhi/opencv-zbar/CMakeFiles/CMakeOutput.log".
I then made two changes in my CMakeCache.txt as follows
//Path to a file.
ZBAR_INCLUDE_DIR:PATH=/usr/include/zbar
//Path to a library.
ZBAR_LIBRARIES:FILEPATH=/usr/lib/libzbar.so
because the were set to NOTFOUND.
It then gave me no errors
-- Found ZBAR: /usr/lib/libzbar.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/surabhi/opencv-zbar
But when I run make
It gives the error:
fatal error: zbar.h: No such file or directory
#include <zbar.h>
^
compilation terminated.
I don't understand why I get this error as It says Found ZBAR when I run cmake.
Thanks in advance
You need to do actually use the include dirs somewhere in your CMakeLists.txt so CMake knows that you actually want to use these include paths.
Examples:
include_directories(${ZBAR_INCLUDE_DIR})
Or the more modern, recommended way:
target_include_directories(zbar_opencv PRIVATE ${ZBAR_INCLUDE_DIR})

OpenCV linemod not cv member

I try to install a ROS indigo package jsk_recognition for chessboard detection and relative position, I have opencv-3.0.0-rc1 on Ubuntu 14.04. When I try to catkin_make the package I receive build errors, quit a lot, the full report is here, but it starts like:
[ 73%] Building CXX object
vision/jsk_recognition-master/jsk_perception/CMakeFiles/roi_to_mask_image.dir/roi_to_mask_image.cpp.o
/home/elod/catkin_ws/src/vision/jsk_recognition-master/jsk_perception/src/linemod.cpp:69:57:
error: ‘linemod’ is not a member of ‘cv’ std::vector<CvPoint>
maskFromTemplate(const std::vector<cv::linemod::Template>& templates,
^ /home/elod/catkin_ws/src/vision/jsk_recognition-master/jsk_perception/src/linemod.cpp:69:57:
error: ‘linemod’ is not a member of ‘cv’
/home/elod/catkin_ws/src/vision/jsk_recognition-master/jsk_perception/src/linemod.cpp:69:78:
error: template argument 1 is invalid std::vector<CvPoint>
maskFromTemplate(const std::vector<cv::linemod::Template>& templates,
^
/home/elod/catkin_ws/src/vision/jsk_recognition-master/jsk_perception/src/linemod.cpp:69:78:
error: template argument 2 is invalid
/home/elod/catkin_ws/src/vision/jsk_recognition-master/jsk_perception/src/linemod.cpp:69:57:
error: ‘linemod’ is not a member of ‘cv’ std::vector<CvPoint>
maskFromTemplate(const std::vector<cv::linemod::Template>& templates,
the problem seams to be with the linemod class, which in the online opencv documentation exist but in my case the compiler does not recognise it. The linemod.h header file can't be find in any opencv folder only in:
./home/elod/catkin_ws/src/vision/jsk_recognition-master/jsk_pcl_ros/include/jsk_pcl_ros/linemod.h
./opt/ros/indigo/include/jsk_pcl_ros/linemod.h
./usr/include/pcl-1.7/pcl/recognition/linemod.h
Is this module missing somhow my OpenCV installation or why cant find it in the cv namespace?
In my old Opencv 2.4.11 (uninstalled) seams to be included this module, but I migrated all my stuff to 3.0.0.
Any ideas how to solve it?
UPDATE
A very important observation is that the above mentioned pkg is for Opencv2.4 and I'm using 3.0! Some stuff was moved or removed from the new version like cvSnakeImage, which also appears in this pkg.
An other question would be: how to replace the cvSnakeImage or with what can be replaced?
Did you build from scratch (clean build directory) ?
You could try adding cv:: namespace into *.hpp
-<linemod::Match>
+<cv::linemod::Match>
And/or try adding "#include <opencv2/rgbd/linemod.hpp>" into the same file.

How to integrate LuaJIT with LuaRocks on Windows?

I downloaded the source of LuaJIT and compiled it with msvc120.dll (VS 2013 x64). When I run it from the command line I have no problems executing some basic lua. Now the LuaJIT installation guide mentions moving luajit.exe and lua51.dll into their own folder. From there it says to create a lua folder and under that a jit folder with the contents of src/jit moved underneath the newly created jit folder.
From my understanding my folder should look like and contain:
luajit.exe
lua51.dll
/lua
/jit
bc.lua
[rest of jit files]
vmdef.lua
Is this correct or am I missing files?
Now after I built my luajit I tried to wire it up into my luarocks to act as my interpreter using
install.bat /LUA C:\LuaJIT\2.0.3\[folder with above content]
However this cannot find the header files. I then copied over what are the header files into the folder above and that wires it up, but I can never actually get anything to compile when pointed over to LuaJIT. Edit: The error I get is the following,
C:\LuaJIT\2.0.3\bin\lua51.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2D0
Error: Failed installing dependency: https://rocks.moonscript.org/luafilesystem-1.6.2-2.src.rock - Build error: Failed compiling module lfs.dll
Is the correct way to handle this to simply point to my lua binaries and from there leverage LuaJIT to run my files or am I doing something wrong with wiring up LuaJIT and luarocks? The former seems to work for the most part, since I only ran into one library compilation issue, lua-cjson.
I've run on exactly the same problem, but they've found a solution right here:
https://github.com/keplerproject/luafilesystem/issues/22
I knew that for "linking DLLs statically" there is a so-called "export" .lib file, which is passed to the linker (and not the DLL itself).
So, for example, when compiling, LuaRocks was doing this:
cl /nologo /MD /O2 -c -Fosrc/mime.obj -ID:/LuaJIT-2.0.4/include/ src/mime.c -DLUA_COMPAT_APIINTCASTS -DLUASOCKET_DEBUG -DNDEBUG -DLUASOCKET_API=__declspec(dllexport) -DMIME_API=__declspec(dllexport) mime.c
link -dll -def:core.def -out:mime/core.dll D:/LuaJIT-2.0.4/bin/lua51.dll src/mime.obj
My LuaJIT was compiled from source in D:\LuaJIT-2.0.4\src, but I made two folders myself: D:\LuaJIT-2.0.4\include with all *.h files copied from src and D:\LuaJIT-2.0.4\bin with luajit.exe, lua51.dll, and then later lua51.exp and lua51.lib. Still same error, but this was the right track.
Fix
Now, check where your LuaRocks configs are:
luarocks.bat help
Scroll down to a section like:
CONFIGURATION
Lua version: 5.1
Configuration files:
System: D:/luarocks/config-5.1.lua (ok)
User : (... snip ...)
Edit the System configuration file, specifically see the part:
variables = {
MSVCRT = 'VCRUNTIME140',
LUALIB = 'lua51.dll'
}
Here! LUALIB should be the .lib file. If your export lib is alongside the DLL, you just need to change to:
variables = {
MSVCRT = 'VCRUNTIME140',
LUALIB = 'lua51.lib' -- here!
}
Verification
And now:
luarocks.bat install luasocket
(...)
link -dll -def:core.def -out:socket/core.dll D:/LuaJIT-2.0.4/bin/lua51.lib src/luasocket.obj (...)
(...)
luasocket 3.0rc1-2 is now built and installed in D:\luarocks\systree (license: MIT)
Note the first argument passed to the linker.

Error linking to OpenCV after restructure of code

I've just reorganised my code (which worked before) and am now getting an error message when I run my executable: The program can't start because opencv_core230d.dll is missing from your computer. The error is accurate, I only have .lib library files for OpenCV, but it worked before and I don't think this should be a problem, should it?
An extract from the cmake file is:
FIND_PACKAGE( OpenCV REQUIRED )
# Define LIBRARY and SRC_FILES to create my library
add_library(${LIBRARY} ${SRC_FILES})
target_link_libraries(${LIBRARY} ${OpenCV_LIBS} )
# Define appName and appFile to create application,
# and link to my library and OpenCV
add_executable(${appName} ${appFile})
target_link_libraries(${appName} ${LIBRARY} ${OpenCV_LIBS})
Am I doing anything obviously wrong, or what else might cause this error?
EDIT: I've now reduced this problem to a minimal example. Inside one directory test I have two files: testApp.cpp and CMakeLists.txt as follows:
testApp.cpp
#include <opencv2/opencv.hpp>
int main(int argc, char* argv[])
{
cv::namedWindow("Test");
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(test)
FIND_PACKAGE( OpenCV REQUIRED )
add_executable(appName testApp.cpp)
target_link_libraries(appName ${OpenCV_LIBS})
message(STATUS "Linking testapp to: ${OpenCV_LIBS}")
With the error as before: opencv_highgui230d.dll is missing.
The problem is that the environment variable PATH does not point to the location where OpenCV's DLLs reside. Search inside OpenCV directories and if you can't find it you'll need to reinstall OpenCV.
Seems this is a common problem which simply needed some more research. To fix, I just added the OpenCV/bin directory to the PATH environmental variable. Still not sure why it was working previously though...

Resources