Yocto recipe for an application that uses opencv and cpp - c++

I created a recipe for a cpp application using opencv to stream an image. But The build fails with following error :
Log data follows:
| DEBUG: Executing shell function do_compile
| shot.cpp:1:10: fatal error: core.hpp: No such file or directory
| 1 | #include "core.hpp"
| | ^~~~~~~~~~
| compilation terminated.
| WARNING: exit code 1 from a shell command.
Although, I added the meta-openembedded/meta-oe that includes opencv folder in which I found core.hpp to bblayers.
Here is the recipe:
DESCRIPTION = " this is an application display an image"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://shot.cpp"
DEPENDS = "opencv"
S = "${WORKDIR}"
do_compile () {
${CXX} shot.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_videoio
}
do_install () {
install -d 0755 ${D}/${bindir}
install -m 0755 ${D}/${bindir}
}
When I ran the application using the opencv I installed on my ubuntu 18.04. it works fine (using cmake).
I tried adding manually the core folder next to the recipe and it didn't work of course, I also added the path of the core.hpp to the recipe in the SRC_URI variable but no luck whatsoever
I don't know what to do specially that I'm new to both yocto and opencv; help please
Here is the source code of the application:
#include "core.hpp"
#include "highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
Mat image;
// LOAD image
image = imread("image1.jpg"); // Read the file "image.jpg".
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
//DISPLAY image
namedWindow( "window"); // Create a window for display.
imshow( "window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}

I think you need some modifications on your recipe:
[...]
inherit pkgconfig
DEPENDS = "opencv"
[...]
And you will need to add a RDEPENDS and FILES variable:
[...]
DEPENDS = "opencv"
# Add the runtime dependencies
RDEPENDS_${PN} = "libopencv-core libopencv-imgproc libopencv-highgui libopencv-videoio libopencv-imgcodecs"
[...]
# At the end, ship the files
FILES_${PN} = "${bindir}"
RDEPENDS_${PN} will tell Yocto that your recipe package ${PN} needs a list a runtime dependencies. These dependencies are requiered to run your application on your board (typically libraries).
FILES_${PN} will tell Yocto that your recipe package ${PN} will contains files. Without this line, Yocto will tell you that
${PN} is by default your recipe file name (without the version and .bb)
There is also some errors.
The compilation had some issues, lopencv_imgcodecs was not added, and there was no output name of your binary (I have named it shot):
do_compile () {
${CXX} ${WORKDIR}/shot.cpp -o ${WORKDIR}/shot -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs ${LDFLAGS}
}
The second install command is not right:
install -m 0755 ${WORKDIR}/shot ${D}/${bindir}
Your application does not have the correct headers:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
At the end, the recipe looks like:
DESCRIPTION = " this is an application display an image"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://shot.cpp"
inherit pkgconfig
DEPENDS = "opencv"
RDEPENDS_${PN} = "libopencv-core libopencv-imgproc libopencv-highgui libopencv-videoio libopencv-imgcodecs"
S = "${WORKDIR}"
do_compile () {
${CXX} ${WORKDIR}/shot.cpp -o ${WORKDIR}/shot -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs ${LDFLAGS}
}
do_install () {
install -d 0755 ${D}/${bindir}
install -m 0755 ${WORKDIR}/shot ${D}/${bindir}
}
FILES_${PN} = "${bindir}"
And the application:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
Mat image;
// LOAD image
image = imread("image1.jpg"); // Read the file "image.jpg".
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
//DISPLAY image
namedWindow( "window"); // Create a window for display.
imshow( "window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
The recipe was working on my Yocto warrior meta.

Related

How can I fix my OpenCV4 installation on Ubuntu

I need some help. I want to install Opencv4 on my computer (which runs on Ubuntu) and use it with VSCode.
A lot of tutorial explains how to do it, so here is one of thoses I followed: https://vitux.com/opencv_ubuntu/
I, next, took a program my teacher sent me to check installation:
#include <iostream>
#include <string>
#include <opencv4/opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main(void)
{
string imageName("/home/baptiste/Documents/M1/infographie/images/lena.jpg"); // path to the image
Mat img;
img = imread(imageName, IMREAD_COLOR); // Read the file as a color image
if( img.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
Mat img_gray;
img_gray = imread(imageName,IMREAD_GRAYSCALE); //Read the file as a grayscale image
if( img_gray.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
imshow( "Image read", img ); // Show our color image inside the window.
imshow("Grayscale image read", img_gray); //Show our grayscale image inside the window.
waitKey(0); // Wait for a keystroke in the window
imwrite("/home/baptiste/Documents/M1/infographie/images/lena_gray.jpg", img_gray); //Save our grayscale image into the file
return 0;
}
I also wrote a makefile:
CC=g++
read_image: read_image.cpp
$(CC) read_image.cpp -o read_image `pkg-config --libs --cflags opencv4`
clean:
rm -f read_image
But when I am compiling, I got this:
g++ read_image.cpp -o read_image `pkg-config --libs --cflags opencv4`
In file included from read_image.cpp:3:
/usr/local/include/opencv4/opencv2/highgui.hpp:46:10: fatal error: opencv2/core.hpp: Aucun fichier ou dossier de ce type
46 | #include "opencv2/core.hpp"
| ^~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [makefile:4 : read_image] Erreur 1
I also spot a problem with my opencv installation: in /usr/local/include, I have a opencv4 folder, which contains...a opencv2 folder, and then the whole installation.
I am interpreting this as: the inclusion made by the modules are not reading at the good place. My program actually recognize the location of highgui.hpp, but that module does not recognize the good location for core.hpp
I did not saw anyone having that problem so it might be quite weird but can someone help me with that ?
Also, I added "/usr/local/include/opencv4/**" into my VSCode configuration.
Thank you for your answer, and for the time you will invest.
EDIT 1:
Here is my c_cpp_properties.json:
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}
And how do I the "tasks.json" file ?
I am interpreting this as: the inclusion made by the modules are not reading at the good place. My program actually recognize the location of highgui.hpp, but that module does not recognize the good location for core.hpp
nice & correct analysis, for a noob !!
your teacher's test program is already incorrect
#include <opencv4/opencv2/highgui.hpp>
should be:
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp> // added for clarity
#include <opencv2/imgcodecs.hpp> // same
and you should probably drop the (opaque and badly supported) pkg-config parts from your makefile, and add paths / libs manually, so you at least "know, what you're doing":
CC=g++
INC=/usr/local/include/opencv4
LIB=/usr/local/lib
LIBS=-lopencv_core -lopencv_highgui -lopencv_imgcodecs
read_image: read_image.cpp
$(CC) -I$(INC) read_image.cpp -o read_image -L$(LIB) $(LIBS)
clean:
rm -f read_image

main.cpp:1:10: fatal error: opencv2/highgui.hpp: No such file or directory

I have install opencv in ubuntu 18.04 and it was installed successfully, I have tried this command:
$ pkg-config --modversion opencv
and its output is: 4.0.1-dev
after this i have tried to rum c++ code:
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std;
int main( int argc, char** argv ) {
cv::Mat image;
image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR);
if(! image.data ) {
std::cout << "Could not open or find the image" << std::endl ;
return -1;
}
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
cv::imshow( "Display window", image );
cv::waitKey(0);
return 0;
}
with this command: :~/cpp_test$ g++ main.cpp -o output pkg-config --cflags --libs opencv
but it throws a fatal error:
main.cpp:1:10: fatal error: opencv2/highgui.hpp: No such file or directory
#include <opencv2/highgui.hpp>
^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
I have reviewed some similar questions but i did not find my answer, i think this is because of environment variables and i do not know which variables i have to set.
In the compiling command add a "4" next to "opencv" (or the number of your version of OpenCV):
$ g++ main.cpp -o output \`pkg-config --cflags --libs opencv4\`

Can't Run simple program Opencv 3.1 with Eclipse (Linking error)

I am configuring openCV on my eclipse and can't run the simple example given in the openCV tutorial, i followed the exact steps as in here
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
The project settings are
It gives me the following errors:
cannot find -lopencv_contrib C/C++ Problem
make: *** [projectname] Error 1 C/C++ Problem
recipe for target 'projectname' failed makefile /faa/Debug line
45 C/C++ Problem
When i removed opencv_contrib the errors become:
./src/projectname.o: undefined reference to symbol
'_ZN2cv6imreadERKNS_6StringEi' C/C++ Problem
make: *** [projectname] Error 1 C/C++ Problem
recipe for target 'projectname' failed makefile /projectname/Debug line 45 C/C++ Problem
I removed the opencv and reinstalled it again but this time i used Debug instead of Release in
cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=/usr/local ..
and it worked.

OpenCV: Error for failure to load library libopencv_calib3d.2.4.dylib

I am trying to set up openCV (installed with homebrew) on my mac and ran the following the command.
g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test& ./test
However, I got this error:
[1] 7834
dyld: Library not loaded: lib/libopencv_calib3d.2.4.dylib
Referenced from: /usr/local/Cellar/opencv/2.4.9/include/./test
Reason: image not found
Trace/BPT trap: 5
I'm not sure what to do take care of this issue very novice to openCV and C++.
The following shows the file structure. The test.cpp file is 2nd from the right along with the picture. I don't see how the program isn't finding the file as the name and location seem to be correct.
test.cpp:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
cout << "test output line" << endl;
Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
Figured it out. Took the .dylib files from /usr/local/Cellar/opencv/2.4.9/lib duplicated and pasted them into /usr/lib.
copy all lib file ie in .dll format past it into your project folder.it will read all the neccessar file from that folder

How to link to shared lib in c++

Could someone help me with linking to a shared lib, specifically libzmq, in C++?
all: clean compile
clean:
rm bin *.o -f
compile:
g++ -g -Wall -I/usr/local/include -L/usr/local/lib main.cpp -lzmq -o bin
I've installed libzmq using the following steps:
git clone https://github.com/zeromq/libzmq.git
cd libzmq
./autogen.sh
./configure
make && sudo make install
Here's my main.cpp
#include <iostream>
#include <string>
#include <zmq/zmq.h>
// Required by fork routine
#include <sys/types.h>
#include <unistd.h>
// Required by wait routine
#include <sys/wait.h>
#include <stdlib.h> // Declaration for exit()
#include <cstdio> // printf
using namespace std;
int global_variable = 2;
int main(int argc, char** argv){
const short int FORK_FAILED = -1;
const short int FORK_SUCCESS = 0;
int stack_variable = 20;
pid_t pid;
string status_identifier;
switch (pid = fork()){
case FORK_SUCCESS:
printf("Child changing global and stack variables\n");
global_variable++;
stack_variable++;
break;
case FORK_FAILED:
cerr << "Failed! -- Failed to fork: " << pid << endl;
exit(1);
default:
printf("Child process (pid=%d) created successfully.\n", pid);
wait(0);
break;
}
printf("[pid=%d] Global: %d\n", pid, global_variable);
printf("[pid=%d] Stack: %d\n", pid, stack_variable);
return 0;
}
And, here's the error msg:
bitcycle # ubuntu64vm ~/git/test $ make
rm bin *.o -f
g++ -g -Wall -I/usr/local/include -L/usr/local/lib main.cpp -lzmq -o bin
main.cpp:4:23: fatal error: zmq/zmq.hpp: No such file or directory
compilation terminated.
make: *** [compile] Error 1
The error is pretty straight forward, but I've yet to find a solution. Any ideas?
My goal is to do something like this with multiple child processes.
Update I'm just going to install it system-wide in ubuntu: sudo apt-get install libzmq-dev, and that resolved the issue. It doesn't teach me anything about how to identify a shared lib and header file on disk and link to it... but I guess I can move that to another day.
C++ wrapper for ZeroMQ (zmq.hpp) is no longer part of ZeroMQ. There is no zmq.hpp in current libzmq master or in latest stable 3.2.x.