Using Opencv 3.1 from CAFFE - opencv

I am trying to use OPENCV 3.1 from inside of the caffe . This is my test code
#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#endif // USE_OPENCV
#include<iostream>
#ifdef USE_OPENCV
int main(){
std::cout<<"USE_OPENCV value is 1\n";
return 0;
}
#else
int main(){
std::cout<<"USE_OPENCV value is 0\n";
return 0;
}
#endif
I am compiling it using following command
g++ -I path_to_caffe/distribute/include/ test3.cpp -std=c++0x -lboost_system
It compiles with giving the following output while executing :
USE_OPENCV value is 0
Before compiling the caffe i make this changes in Makefile.config file:
USE_OPENCV := 1
. . .
OPENCV_VERSION := 3
. . .
USE_PKG_CONFIG := 1
While compiling caffe, I firstly cleand it and then compiled it using following commands:
make clean
make all -j $(($(nproc) + 1))
make test
make runtest
make pycaffe
make distribute
It compiles without giving error , but while testing my test file it does not gives the output that i expect.

All glories goes to Shai. The solution is to add -DUSE_OPENCV flag .
g++ -I path_to_caffe/distribute/include/ test3.cpp -std=c++0x -lboost_system -DUSE_OPENCV

Related

pcap "undefined reference to" error

I'm trying to read in data from pcap files using pcap_open_offline(). I've used #include <pcap/pcap.h> and compiled with no errors after some debugging. Now I've come across another problem I can't seem to figure out. I wrote the following function:
void openPcap(char* filename){
printf("Opening file %s\n", filename);
pcap_t *pcap;
const unsigned char *packet;
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr header;
pcap = pcap_open_offline(filename, errbuf);
if (pcap == NULL){
fprintf(stderr, "%s Malformed packet records in file %s",ER,filename);
}
}
And my pcap_open_offline call gives me about 100 of these errors when I try to compile:
pcap-linux.c:(.text+0xcd4): undefined reference to 'nl_handle_alloc'
pcap-linux.c:(.text+0xce8): undefined reference to 'genl_connect'
pcap-linux.c:(.text+0xcf6): undefined reference to 'genl_ctrl_alloc_cache'
pcap-linux.c:(.text+0xd0e): undefined reference to 'genl_ctrl_search_by_name'
pcap-linux.c:(.text+0xd64): undefined reference to 'nl_handle_destroy'
pcap-linux.c:(.text+0xdd7): undefined reference to 'nl_cache_free'
This is what my makefile looks like:
# -------------------------------
C=/afs/nd.edu/user14/csesoft/new/bin/gcc
CFLAGS=-Wall -std=c11 -I/afs/nd.edu/coursesp.18/cse/cse30341.01/support/gcc-libpcap/include -D_BSD_SOURCE
LD=/afs/nd.edu/user14/csesoft/new/bin/g++
#LD=g++
LDFLAGS=-lpthread
# # ----------------------------
LDFLAGS += -L/afs/nd.edu/coursesp.18/cse/cse30341.01/support/gcc-libpcap/lib -lpcap # Add your own flags here, or leave blank
threadedRE: threadedRE.o
$(LD) $^ $(LDFLAGS) -o $#
threadedRE.o: threadedRE.c
$(C) $(CFLAGS) -c $<
# C compiler
%.o: %.c
$(C) $(CFLAGS) -c $<
.PHONY: clean
clean:
rm -f threadedRE *.o
And my headers are:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/stat.h>
#include <pcap/pcap.h>
Any suggestions would be greatly appreciated.
It isn't a compiling error, but a linking one. At the end of build process you should see something like "ld exited with error".
pcap_open_offline() seems to use nl_handle_alloc() and other functions, but linker can't find object files containing their implementation. Pointing linker to proper library which contains required object files by adding -lnl to LDFLAGS should do the trick.

Glib linking error for g_socket_new

I am trying to learn glib and gObject for a networking project.
Here is the command used to compile (after using pkg-config to get output):
gcc socket1.c -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgobject-2.0 -lglib-2.0
socket1.c, after stripping, to identify the problem is:
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <gio/gio.h>
static GSocket *mySocket;
int main(int argc, char **argv) {
GError *err1 = NULL;
mySocket = g_socket_new ( G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_TCP,
&err1);
}
The error is:
/tmp/ccKIEXOi.o: In function `main':
socket1.c:(.text+0x3d): undefined reference to `g_socket_new'
collect2: error: ld returned 1 exit status
I have tried to use readelf -Ws and nm to see if I could see g_new_socket in any of the .so files linked. I did not see any. Is there a separate library I need to link to? Where/which is it?
You need to link to libgio-2.0.so, which is the third part of GLib (GLib, GObject, GIO). You can get its compiler and linker flags from pkg-config using pkg-config --cflags --libs gio-2.0.
You can tell GSocket is in GIO by looking at the documentation — it’s in the GIO manual: https://developer.gnome.org/gio/stable/GSocket.html.

Is it possible code coverage of a shared library using gcov?

I try to test an executable which uses OpenCV shared library. When using gcov to know what code lines were covered I only get info about my .cpp files and .hpp of the library. No info is shown about .cpp files of the library.
I compiled and linked with -pg --coverage flags.
Yes, gcov can give coverage information about a shared library. If I remember correctly from the problems I had getting this to work on my project, you're probably not including the --coverage flag on the linking of the dynamic library. Here's the smallest example I could create.
Makefile:
CXXFLAGS += --coverage
LDFLAGS += --coverage
myexec: myexec.cpp libmylib.so
libmylib.so: mylib.o
gcc --coverage -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o
mylib.o: CXXFLAGS += -fPIC
myexec.cpp:
#include "mylib.h"
int main(int argc, char** argv)
{
return is_even(argc);
}
mylib.h
#ifndef MYLIB_H
#define MYLIB_H
int is_even(int num);
#endif
mylib.cpp
#include "mylib.h"
int is_even(int num)
{
if (num % 2)
return false;
else
return true;
}
Output of make (so you can see exactly what the build was):
g++ --coverage -fPIC -c -o mylib.o mylib.cpp
gcc --coverage -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o
g++ --coverage --coverage myexec.cpp libmylib.so -o myexec
I ran the executable using LD_LIBRARY_PATH="." ./myexec a, and then ran gcov mylib.cpp. Here's the contents of mylib.cpp.gcov:
-: 0:Source:mylib.cpp
-: 0:Graph:mylib.gcno
-: 0:Data:mylib.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include "mylib.h"
-: 2:
1: 3:int is_even(int num)
-: 4:{
1: 5: if (num % 2)
#####: 6: return false;
-: 7: else
1: 8: return true;
-: 9:}

opencv mat CV_MAX_DIM in function setSize

I have a small OpenCV code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int
main(int argc, char* argv[])
{
cv::Mat mask_img = cv::imread("image.png");
cv::imshow("window",mask_img);
return 0;
}
Yet when running I get the following error:
OpenCV Error: Assertion failed (0 <= _dims && _dims <= CV_MAX_DIM) in
setSize, file /home/box/OpenCV-2.4.3/modules/core/src/matrix.cpp, line
88 terminate called after throwing an instance of 'cv::Exception'
what(): /home/box/OpenCV-2.4.3/modules/core/src/matrix.cpp:88: error:
(-215) 0 <= _dims && _dims <= CV_MAX_DIM in function setSize
Aborted
I'm using OpenCV 2.4.3 on kubuntu linux, compiled from source.
How can I resolve this error?
I solved it myself, just use
g++ bla2.cpp `pkg-config opencv --cflags --libs` -o bla
Do not use your own linking libraries in addition like -lopencv_core etc
Is your image read properly? Try adding these lines of code after imread: if (image.empty()){ std::cerr << "Failed to read image" << std::endl; return 0;}

Using OpenCV in QTCreator (linking problem)

I have a problem with the linking simpliest test program in QTCreator:
CODE:
#include <QtCore/QCoreApplication>
#include <cv.h>
#include <highgui.h>
#include <cxcore.hpp>
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat M(7,7,CV_32FC2,Scalar(1,3));
return 0;
}
.pro file:
QT -= gui
TARGET = testopencv
CONFIG += console
CONFIG -= app_bundle
INCLUDEPATH += C:/OpenCV2_1/include/opencv
TEMPLATE = app
LIBS += C:/OpenCV2_1/lib/cxcore210d.lib \
C:/OpenCV2_1/lib/cv210d.lib \
C:/OpenCV2_1/lib/highgui210d.lib\
C:/OpenCV2_1/lib/cvaux210d.lib
SOURCES += main.cpp
I've tried to use -L and -l like LIBS += -LC:/OpenCV2_1/lib -lcxcored
And in .pro file:
QMAKE_LIBDIR += C:/OpenCV2_1/lib/Debug
LIBS += -lcxcore210d \
-lcv210d \
-lhighgui210d
The errors are like:
debug/main.o:C:\griskin\test\app\testopencv/../../../../OpenCV2_1/include/opencv/cxcore.hpp:97: undefined reference to cv::format(char const*, ...)'
Could anyone help me? Thanks!
In Visual Studio it works but I need it works in QTCreator..
Qt uses the MinGW compiler and linker. It will happily link OpenCV .libs when you use the C interface. However due to ABI issues, it will not link C++ modules.
You will have to either restrict yourself to the C interfaces only (i.e. no cv::Mat), or get OpenCV to compile with MinGw (or compile Qt in/with VS). Also, see this thread.
This is what my .pro file looks like
INCLUDEPATH += C:\\opencv\\release\\install\\include\
LIBS += -LC:\\opencv\\release\\install\\bin \
-lopencv_core240 \
-lopencv_highgui240 \
-lopencv_imgproc240 \
-lopencv_features2d240 \
-lopencv_calib3d240 \
and replace
#include <cv.h>
with
#include <opencv/cv.h>
Above .pro file works perfect for me. I have used mingw to compile OpenCV and using mingw compiler tool chain in Qt.
It seems that QtCreator cannot locate lib files.
Try specifying the include file and libs as follows.
INCLUDEPATH += C:/OpenCV2_1/build/include/
LIBS += C:/OpenCV2_1/build/gpu/x86/lib/cxcore210d.lib
you should refer to build folder if you're using pre-build version of opencv.
(I recommend pre-build version if you're not that familiar with opencv)

Resources