Gtkmm-3.0 compiled code throwing Gtk+2 symbol error - c++

I'm on Ubuntu 13.10 using gtkmm 3.0 dev package, compiling the following code:
#include <gtkmm.h>
#include <iostream>
#include "opencv2/opencv.hpp"
#include <chrono>
#include <thread>
#include <mutex>
//GASP! global vars!
std::mutex FRAME_MUTEX;
std::thread CV_THREAD;
cv::Mat FRAME, CLEAN;
Glib::Dispatcher dispatcher;
volatile bool THREADRUN;
void cvThread() {
THREADRUN = true;
cv::VideoCapture capture(0);
while(THREADRUN) {
FRAME_MUTEX.lock();
capture >> FRAME;
FRAME_MUTEX.unlock();
dispatcher.emit();
}
}
int main(int argc, char** argv)
{
Gtk::Main gtkMain(argc, argv);
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("main_window.glade");
Gtk::Window *mainWindow;
Gtk::Image *cleanDisplay;
Gtk::Image *evmDisplay;
builder->get_widget("main_window", mainWindow);
builder->get_widget("clean_display", cleanDisplay);
builder->get_widget("evm_display", evmDisplay);
dispatcher.connect([&]() {
FRAME_MUTEX.lock();
cv::cvtColor(FRAME, CLEAN, CV_BGR2RGB);
cleanDisplay->set(Gdk::Pixbuf::create_from_data(CLEAN.data, Gdk::COLORSPACE_RGB, false, 8, CLEAN.cols, CLEAN.rows, CLEAN.step));
cleanDisplay->queue_draw();
FRAME_MUTEX.unlock();
});
CV_THREAD = std::thread(&cvThread);
gtkMain.run(*mainWindow);
return 0;
}
Using the command:
$ g++ test.cpp -o uitest `pkg-config --cflags --libs gtkmm-3.0 opencv` -std=c++11
Compilation works fine, but when I run the executable, it fails with the following error:
(uitest:20300): Gtk-ERROR **: GTK+ 2.x symbols detected.
Using GTK+ 2.x and GTK+ 3 in the same process is not supported
Trace/breakpoint trap (core dumped)
This is my first attempt to delve into using gtkmm, and I don't have the foggiest idea of where to go to start debugging. I checked the glade file, and it has an xml comment at the top indicating that gtk+3 is required by the contents of that file. The answer here didn't help me much; I got the following output from following those instructions:
-lgtkmm-3.0 -latkmm-1.6 -lgdkmm-3.0 -lgiomm-2.4 -lpangomm-1.4 -lgtk-3 -lglibmm-2.4
-lcairomm-1.0 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0
-lcairo-gobject -lpango-1.0 -lcairo -lsigc-2.0 -lgobject-2.0 -lglib-2.0
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann
-lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml
-lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres
-lopencv_ts -lopencv_video -lopencv_videostab
Any suggestions on how to fix this issue?

Till the OpenCV 3 gets released you can use Gtk::Image or Gdk::pixbuf to read/write images and skip linking highgui wich is the cause of the problem.
Here is how you can convert Mat & pixbuf:
cv::Mat mat(cv::Size(pixbuf->get_width(),pixbuf->get_height()),
CV_8UC3, (uchar*)pixbuf->get_pixels(), pixbuf->get_rowstride());
RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data((guint8*)mat.data,
Gdk::COLORSPACE_RGB,false,8,mat.cols,mat.rows,mat.step);
The "false" parameter is for transparency.

It seems that OpenCV uses gtk+2. While there has been some effort to extend support to version 3, the latest on that is that the pull request has not been fully approved. Until then, one can only use gtk+2 when using OpenCV.
For those interested in supporting the effort, visit the pull request page on github.

Related

Unable to locate OpenCV header, but pkg-config contains path

In order to install OpenCV on my Mac, I opted to use brew install opencv instead of building from source as is specified in the Mac installation tutorials. Homebrew's installation path appears to be /opt/homebrew, and all of the included packages that I have downloaded appear to be contained in the include in that directory.
I wrote a sample program program in order to test the installation:
#include <opencv4/opencv2/imgcodecs.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
VideoCapture cap(0);
Mat img;
while (1) {
cap.read(img);
imshow("Image", img);
if (waitKey(1) == 27) {
break;
}
}
return 0;
}
And, to compile it, I ran:
g++ video.cpp `pkg-config --cflags --libs opencv4`
which returned:
fatal error: 'opencv4/opencv2/imgcodecs.hpp' file not found
I ran pkg-config --cflags --libs opencv4 to check whether it was set correctly, and the following was returned:
-I/opt/homebrew/Cellar/opencv/4.5.2_4/include/opencv4 -L/opt/homebrew/Cellar/opencv/4.5.2_4/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
I think that this implies that the path to OpenCV is set for linking the package during compilation, so I was wondering about why the linker couldn't find it. Also, my end-goal is to write a program involving pcl, and I also installed pcl via brew. Since it is also contained in opt/homebrew/, do I have to create a specific reference to its path outside of /usr/ when making the cake file? Thanks!
Thank you #Alan Birtles. It was just a problem with my includes. For those reading, #include <opencv4/opencv2/imgcodecs.hpp>, as well as all of the other OpenCV packages, had to refer directly to OpenCV2. So, for example, it had to be #include <opencv2/imgcodecs.hpp>

program comiled ok using pkg-config --cflgas opencv and pkg-config --ldflag but not executable (ubuntu 16.04)

Recently I installed opencv on my ubuntu 16.04 machine(I've done this many times before and used it ok). I tried compiling yolo program, it compiled ok and ran ok. When I set OPENCV=1 in the Makefile, it compiles the program with opencv. To set the compile and link flags it has these two lines.
LDFLAGS+= `pkg-config --libs opencv` -lstdc++
COMMON+= `pkg-config --cflags opencv`
My pkg-config commands work correctly. See the output below.
ckim#chan-ubuntu:~/YOLOV2/darknet$ pkg-config --cflags opencv
-I/home/ckim/opencv_install/installation/OpenCV-3.4/include/opencv -I/home/ckim/opencv_install/installation/OpenCV-3.4/include
ckim#chan-ubuntu:~/YOLOV2/darknet$ pkg-config --libs opencv
-L/home/ckim/opencv_install/installation/OpenCV-3.4/lib -lopencv_stitching -lopencv_videostab -lopencv_superres -lopencv_surface_matching -lopencv_dnn_objdetect -lopencv_line_descriptor -lopencv_aruco -lopencv_img_hash -lopencv_xobjdetect -lopencv_dpm -lopencv_freetype -lopencv_stereo -lopencv_face -lopencv_objdetect -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_saliency -lopencv_hfs -lopencv_ccalib -lopencv_tracking -lopencv_datasets -lopencv_plot -lopencv_optflow -lopencv_ximgproc -lopencv_fuzzy -lopencv_bioinspired -lopencv_highgui -lopencv_videoio -lopencv_text -lopencv_dnn -lopencv_reg -lopencv_hdf -lopencv_bgsegm -lopencv_rgbd -lopencv_sfm -lopencv_xfeatures2d -lopencv_calib3d -lopencv_shape -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_ml -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
A couple of days ago, I tried compiling a simple opencv pgrogram that I find on a web page and tried compiling it. (Iwanted to test a basic display window's behavior). The program was like this. very typical)
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/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], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // 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(1); // Wait for a keystroke in the window
return 0;
}
When I compile and link it with command
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test
it gives me no error and I see the file test but it's not executable. When I run it after setting chmod +x test, I get
ckim#chan-ubuntu:~/opencvtest$ ./test
bash: ./test: cannot execute binary file: Exec format error
ckim#chan-ubuntu:~/opencvtest$ file test
test: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
What could be wrong?
As per the comment you're command line...
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test
includes the -c flag which tells the compiler to compile only -- don't link. Also. as you're code is c++ rather than c you need to link with the correct runtime libraries. That means using g++ rather than gcc. So the command should be...
g++ test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test

Running OpenCV in Mac OS terminal with opencv_contrib

I'm trying to compile and run OpenCV through the command line. I can get the main Open CV libraries to work however I think my problem is with using the opencv_contrib libraries.
I have installed Open CV with home brew.
I can compile and run the below code fine from another SO question:
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
Mat src = Mat(Size(320,240),CV_64F);;
namedWindow("test");
cout << "press any key to close" << endl;
while(true){
randn(src,0,1.0);
imshow("test",src);
if(waitKey() > 0) break;
}
}
This is compiled like this:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -lopencv_core -lopencv_highgui -o cv
Then ran ./main
However when I try and run anything with the opencv_contrib libraries I get this error when compiled like this:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv
error:
Undefined symbols for architecture x86_64:
"cv::xfeatures2d::SURF::create(double, int, int, bool, bool)", referenced from:
_main in cv-f48298.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I believe this is likely due to not specifying the opencv_contrib libraries. I understand they come installed with homebrew. In my Cellar directory I have a directory for opencv and opencv_contrib. I'm not sure if the opencv_contrib directory needs to be located within the opencv directory.
But I believe everything is there as with this:
pkg-config --libs --cflags opencv
It outputs:
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
But then if I add that to my command like:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" pkg-config --libs --cflags opencv -o cv
I get this:
clang: error: unsupported option '--libs'
clang: error: unsupported option '--cflags'
clang: error: no such file or directory: 'pkg-config'
clang: error: no such file or directory: 'opencv'
I'm able to compile the same code in Xcode and I only needed to add the search paths for the openCV directory and add the libs within there. I can run the code by opening the products folder in finder and running ./cv with images passed in. But I'm struggling with doing this all through the command line.
Any help would be great!
You want to add the output of the pkgconfig tool (you listed it above: -I/usr/local/include/opencv ...) to your compilation command line to compile correctly.
You could just manually copy this output text into your compiler invocation command:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv
This is usually automated by calling pkgconfig inline:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" `pkg-config --libs --cflags opencv` -o cv
Notice the backticks in this though - they are missing in your command line. With the backticks pkgconfig will be executed and the output (which you know what it looks like) will be inserted into the command line.

Compiling simple openCV project

Attempting to compile a very simple program to test out my recent installation of OpenCV 3.2.0. My program opencvtest.cpp is in a folder called cpp, which also holds the opencv-3.2.0 installation directory.
Here's my program.
//Include file for every supported OpenCV function
#include <opencv2/opencv.hpp>
int main( int argc, char** argv ) {
cv::Mat img = cv::imread( argv[1], -1 );
if( img.empty() ) return -1;
cv::namedWindow( "Example 2-1", cv::WINDOW_AUTOSIZE );
cv::imshow( "Example 2-1", img );
cv::waitKey( 0 );
cv::destroyWindow( "Example 2-1" );
return 0;
}
Below I've included the call to pkg-config to show what parameters I'm using, and my actual g++ command to try and compile my file.
asif.ahmed:~/cpp $ pkg-config opencv --cflags --libs
-I/usr/include/opencv -I/usr/include/opencv2 -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -lopencv_core -lopencv_ml -lopencv_features2d -lopencv_objdetect -lopencv_flann -lopencv_video -lopencv_highgui
asif.ahmed:~/cpp $ g++ opencvtest.cpp `pkg-config opencv --cflags --libs` -o main
opencvtest.cpp:2:10: fatal error: 'opencv2/opencv.hpp' file not found
#include <opencv2/opencv.hpp>
^
1 error generated.
Can anyone else suggest anything to get this to compile? I don't have a great grasp of command line compiling C++.
Check path to opencv.hpp (is it really /usr/include/opencv2/opencv2/opencv.hpp?) - If you use -I/usr/include/opencv2 perhaps you need #include "opencv.hpp" instead of #include <opencv2/opencv.hpp>, or better use -I/usr/include/ and #include "opencv2/opencv.hpp"

OpenCV trying to integrate ARtoolkit with OpenCV

I am new to C++, openCV and Artoolkit
I am trying to build a motion tracker devices
right now, I am following the tutorial
https://artoolkit.org/blog/2016/05/opencv-with-artoolkit
However I meet some problem when I trying to implement this on SimpleTest on the Linux machine.
The error I get is like this:
"clang++ -c -O3 -fPIC -march=core2 -DHAVE_NFT=1 -I/usr/include/x86_64-linux-gnu -pthread -I/usr/include/gstreamer-0.10 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/libxml2 -I../../include simpleTest.c
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated In file included from simpleTest.c:79: In file included from ../../include/linux-x86_64/opencv2/opencv.hpp:59:/usr/include/opencv2/contrib/contrib.hpp:273:23: error: no template named
'vector'; did you mean 'std::vector'?"
simpleTest code
I added line like this
#include <linux-x86_64/opencv2/opencv.hpp>
#include <linux-x86_64/opencv2/opencv_modules.hpp>
using namespace std;
using namespace cv;
in the make file:
I add some this:
LIBS= -lARgsub -lARvideo -lAR -lARICP -lAR -lglut -lGLU -lGL -lX11 -lm -lpthread -ljpeg -pthread -lgstreamer-0.10 -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lxml2 -lglib-2.0 -ldc1394 -lraw1394 -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_viz -lippicv -lopencv_core
and
CC = clang++
There are some similar issues found on SO here.....
Linking C from C++ in OS X Mavericks
Warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated
There's a mismatch between C++ and C.
To get around this, it would be best if you used ARWrapper for ARToolkit within a C++ framework.