opencv mat CV_MAX_DIM in function setSize - opencv

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;}

Related

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.

Using Opencv 3.1 from CAFFE

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

Cannot grab image from Xtion Pro Live with Opencv code

I am using OpenCV 2.4.10 and I want to take image from my Asus Xtion Pro Live. When I'm trying to execute the code below, I get this error: "Can not open capture."
I tried everything, like Sensor update, opencv with openni compiling and opencv re-installing (even the version 2.4.6).
OpenNI and Sensor are working properly since I am able to run examples such as NiViewer. But the example openni_capture.cpp (on opencv-2.4.10/samples/cpp) cannot run properly.
The code:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture capture;
capture.open(CV_CAP_OPENNI_ASUS);
if ( !capture.isOpened() )
{
cout << "Error opening capture" << endl;
return -1;
}
if( !capture.grab() )
{
cout << "Can not grab image" << endl;
}
return 0;
}
The compiling is done with the following command:
g++ capture.cpp -o capture pkg-config --cflags opencv --libs opencv
How can i fix this error? Is there any problem with the opencv version that i use?
I did what is being said on Can not grab image from VideoCapture OpenCV with Asus Xtion Pro Live
but the problem still exists.
What serial ports are listed when you print out the list as you're doing ? Have you checked to see what serial port your camera is connected to ? On Linux you can list mounted devices with :
lsusb

Error when linking C executable to OpenCV

I'm compiling OpenCV under Ubuntu 13.10 using cMake.
i've already compiled c++ programs and they works well.
now i'm trying to compile a C file using this cMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (hello)
find_package (OpenCV REQUIRED)
add_executable (hello src/test.c)
target_link_libraries (hello ${OpenCV_LIBS})
here is the test.c file:
#include <stdio.h>
#include <stdlib.h>
#include <opencv/highgui.h>
int main (int argc, char* argv[])
{
IplImage* img = NULL;
const char* window_title = "Hello, OpenCV!";
if (argc < 2)
{
fprintf (stderr, "usage: %s IMAGE\n", argv[0]);
return EXIT_FAILURE;
}
img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (img == NULL)
{
fprintf (stderr, "couldn't open image file: %s\n", argv[1]);
return EXIT_FAILURE;
}
cvNamedWindow (window_title, CV_WINDOW_AUTOSIZE);
cvShowImage (window_title, img);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&img);
return EXIT_SUCCESS;
}
it returns me this error whene running cmake . then make to the project:
Linking C executable hello
/usr/bin/ld: CMakeFiles/hello.dir/src/test.c.o: undefined reference to symbol «lrint##GLIBC_2.1»
/lib/i386-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [hello] Erreur 1
make[1]: *** [CMakeFiles/hello.dir/all] Erreur 2
make: *** [all] Erreur 2
Add -lm /lib/i386-linux-gnu/libm.so.6to your linked library
$ gcc `pkg-config --cflags opencv` -o <output> <input> `pkg-config --libs opencv` -lm /lib/i386-linux-gnu/libm.so.6
I had the same errors and it worked with me
Edit your CMakeLists.txt as follows:
cmake_minimum_required(VERSION 2.8)
project(test)
find_package( OpenCV REQUIRED )
add_executable( test test.c )
target_link_libraries( test ${OpenCV_LIBS} )
Then, $ cmake .
Then $ make and finally $./test

G++: fatal error: no input files

Hi Im trying to display the lena.jpg which is the first demostration for Instant OpenCV Starter Book.
I can build and play 'hello world' easy enough.
The problem code
// opencv header files
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
// namespaces declaration
using namespace cv;
using namespace std;
// create a variable to store the image
Mat image;
int main( int argc, char** argv )
{
// open the image and store it in the 'image' variable
// Replace the path with where you have downloaded the image
// image=imread("<path to image">/lena.jpg");
image=imread("/home/nigel/Documents/ffmpeg/tests/lena.jpg");
// create a window to display the image
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
// display the image in the window created
imshow( "Display window", image );
// wait for a keystroke
waitKey(0);
return 0;
}
The error
g++ -Wall -fexceptions 'pkg-config --cflags opencv' -g "pkg-config --cflags
opencv' -c/home/nigel/Drone/Test2/main.cpp -o obj/Debug/Drone/Test2/main.o
g++:fatal error:no input files
compilation terminated
process terminated with status 1 (0 minutes, 0 seconds)
I have tried different paths for the lena.jpg, I changed the lena.jpg from lena.pnm not sure if I have the path right maybe ?
New to using OpenCv would appreciate some help
This error occurs when no source code file is passed in to g++. This leads me to believe there is an error in your command line arguments.
Some spots to look at:
It looks like there may be a stray quote before the second pkg-config in the line
'pkg-config --cflags opencv' -g "pkg-config --cflags opencv'
It also looks like you are missing a space between the -c and the /home/.../main.cpp

Resources