I'm trying to read the png image with Gdk::Pixbuf::create_from_resource:
#include <iostream>
#include <gtkmm.h>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
Gtk::Window window;
window.set_default_size(100, 100);
try {
Glib::RefPtr<Gdk::Pixbuf> image
= Gdk::Pixbuf::create_from_resource("image.png");
} catch (const Glib::Error &error) {
std::cerr << "Failed to load an image: " << error.what() << std::endl;
}
return app->run(window);
}
But an error occurs:
$ ./a.out
Failed to load an image: The resource at “image.png” does not exist
$ ls -l
total 128
-rwxrwxr-x. 1 user user 36528 Jul 18 15:01 a.out
-rw-r--r--. 1 user user 88792 Jul 18 15:00 image.png
-rw-r--r--. 1 user user 449 Jul 18 15:00 main.cpp
gtkmm version 3.24.6
If you want to load an image file directly into your program, instead of:
Glib::RefPtr<Gdk::Pixbuf> image
= Gdk::Pixbuf::create_from_resource("image.png");
you would use the following statement:
Glib::RefPtr<Gdk::Pixbuf> image
= Gdk::Pixbuf::create_from_file("image.png");
If you do want to use the image file as a resource, you would need to first generate a resource file with the "glib-compile-resources" function using a resource definition XML file. For example.
glib-compile-resources --target=image.c --generate-source resource.xml
In your XML file, you probably would have some type of definition such as the following.
<gresources>
<gresource prefix="Image">
<file preprocess="xml-stripblanks">image.png</file>
</gresource>
</gresources>
Then in your program, your creation statement would be similar to the following.
Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_resource("Image/image.png");
Finally, you would revise your compile command to include the generated resource file with your "main.cpp" file to create your program.
g++ -Wno-format -o a.out main.cpp image.c `pkg-config --cflags --libs gtkmm-3.0` `pkg-config --cflags --libs gdkmm-3.0`
Hope that clarifies things.
Regards.
Related
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
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.
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\`
Background
I'm running linux... and I'm trying to write a basic little c++ program that connects to a postgresql database.
I'm trying to follow this article
http://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm
Problem
I've been able to compile the library... and I can see now that I have the following folder on my computer
/usr/local/include/pqxx
But when i try to write some basic code and compile it, I get the following error:
devbox2:/var/abus# g++ testdb.cpp -lpqxx -lpq
testdb.cpp:2:22: fatal error: pqxx/pqxx: No such file or directory
#include <pqxx/pqxx>
^
compilation terminated.
Source Code
Here's what the code looks like:
1 #include <iostream>
2 #include <pqxx/pqxx>
3
4 using namespace std;
5 using namespace pqxx;
6
7 int main(int argc, char* argv[])
8 {
9 try{
10 connection C("dbname=testdestination user=testuser password=testpassword \
11 hostaddr=127.0.0.1 port=5432");
12 if (C.is_open()) {
13 cout << "Opened database successfully: " << C.dbname() << endl;
14 } else {
15 cout << "Can't open database" << endl;
16 return 1;
17 }
18 C.disconnect ();
19 }catch (const std::exception &e){
20 cerr << e.what() << std::endl;
21 return 1;
22 }
23 }
What I've tried so far:
I've been poking around the /usr/local/include/pqxx folder and I can see that there is a file called pqxx... but it doesn't have any extension on it.
Here's a snippet from the ls -lah command for that folder:
-rw-r--r-- 1 root root 637 Dec 8 21:42 pipeline
-rw-r--r-- 1 root root 7.5K Dec 8 21:42 pipeline.hxx
-rw-r--r-- 1 root root 1.1K Dec 8 21:42 pqxx
-rw-r--r-- 1 root root 728 Dec 8 21:42 prepared_statement
-rw-r--r-- 1 root root 8.2K Dec 8 21:42 prepared_statement.hxx
I've also made sure that my PATH includes the /usr/local/include/pqxx folder. This is what my PATH looks like:
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/gcc:/usr/local/include/pqxx:/usr/local/include'
I'm not sure what else I should check. Any suggestions would be appreciated.
Thanks.
To find the include files, you must add an -I option, e.g.
g++ -I/usr/local/include testdb.cpp -lpqxx -lpq
Adding directories to PATH doesn't help here, PATH is for locating executables from the shell.
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