Cannot open camera with OpenCV in Linux - c++

I am using OpenCV 2.4.3 on linux now. And in my /dev/ there is a device called video0 that is working fine with cheese.
Currently I am using the following code to try to access the camera. This code is written in qtcreator and compiled with qmake to include the necessary library. As for the OpenCV library, I downloaded the source code and compiled it according to the instruction on the website on my machine.
++main.cpp:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <cstdlib>
int main(int argc, char** argv) {
int cameraNumber = 0;
if (argc>1)
cameraNumber = atoi(argv[1]);
cv::VideoCapture camera;
camera.open(cameraNumber);
if(!camera.isOpened()) {
std::cerr<<"Error opening camera"<<std::endl;
exit(1);
}
camera.set(CV_CAP_PROP_FRAME_WIDTH,640);
camera.set(CV_CAP_PROP_FRAME_HEIGHT,480);
while(true) {
cv::Mat cameraFrame;
camera>>cameraFrame;
if (cameraFrame.empty()){
std::cerr<<"No frame read from camera"<<std::endl;
exit(1);
}
imshow("Camera",cameraFrame);
char keypress = cv::waitKey(20);
if (keypress==27) {
break;
}
}
return 0;
}
++test.pro
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/release/ -lopencv_core
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/debug/ -lopencv_core
else:unix: LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/ -lopencv_core
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/release/ -lopencv_highgui
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/debug/ -lopencv_highgui
else:unix: LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/ -lopencv_highgui
INCLUDEPATH += $$PWD/../../../../opt/OpenCV-2.4.3/include
DEPENDPATH += $$PWD/../../../../opt/OpenCV-2.4.3/include
However, when running the program it will output Error opening camera and exit, which means the camera is not opened successfully. I have been googled a while and didn't find any possible solutions to solve this issue. Could some one give me some advice on how to solve this problem? Thanks!

Perhaps you are missing some addition libraries that are required for linux.
According to OpenCV install guide, Extra Prerequisites, when using OpenCV on linux, you need to install these libraries: ffmpeg, libgstreamer, libv4l, libxine, unicap, libdc1394 2.x.
On Ubuntu 12.04 , installing the following packages worked for me (*sudo apt-get install __*):
python-opencv
libhighgui2.3
libhighgui-dev
ffmpeg
libgstreamer0.10-0
libv4l-0
libv4l-dev
libxine2
libunicap2
libdc1394-22
Good Luck!

Related

Library Version Mismatch (C++ HDF5 Windows)

When trying to run a simple program using the hdf5-file format i get these messages and the program crashes:
I found posts to other problems like this, but most of them refer to linux and python. I use C++ (Qt Creator) and Windows (Windows 7 64bit) with the MSVC17 64bit Compiler, so these solutions dont work for me.
I don't understand why this occurs, because i simply donwloaded the HDF5 version 1.10.2 and installed it, i don't know why it says something about the version 1.8.15. Where could this come from?
I guess "library version" (1.8.15) is the version of my hdf5.lib file, right?
What does "header version" (1.10.2) mean? I simply #include <hdf5.h> and #include <H5Cpp.h> in the main.cpp and added paths in the hdf5_test_2.pro (my project) file.
Here is my code:
hdf5_test.pro:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
win32: LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5 -lhdf5_cpp -lhdf5_hl_cpp
INCLUDEPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
DEPENDPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
win32:!win32-g++: PRE_TARGETDEPS += 'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/libhdf5.lib'
else:win32-g++: PRE_TARGETDEPS += 'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/liblibhdf5.a'
main.cpp:
#include <iostream>
#include <hdf5.h>
#include <H5Cpp.h>
using namespace std;
using namespace H5;
int main()
{
hid_t file_id;
herr_t status;
file_id = H5Fcreate("file.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
status = H5Fclose(file_id);
cout << "Hello World!" << endl;
return 0;
}
The code example i tried to use comes from High Level Introduction to HDF5 (Pages 16/17).
Solved (meaning next error)
I think i solved the problem: There was another Version of HDF5 installed (1.8.15) on the system (the one who used this PC before me installed it).
I told my programm to link my lib (1.10.2) and passed the path, but somehow he found the 1.8.15 version previously installed. I now told my program to link the old version and it worked.
My hdf5_test_2.pro (my project) looks now like this:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
win32: LIBS += -LC:/Anaconda2/Library/lib/ -lhdf5 -lhdf5_cpp
INCLUDEPATH += C:/Anaconda2/Library/include
DEPENDPATH += C:/Anaconda2/Library/include
Nice so far, but i get these two warnings now:
Any ideas, what that means?

link error- Qt Creator OpenCV Ubuntu

I am currently having a link error with Qt.
I cant compile my code since i got the error message:
error: undefined reference to `cv::imread(cv::String const&, int)'
Heres the content of my .pro
QT += core
QT -= gui
TARGET = edge_detection
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
Heres my simple code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main(){
cv::Mat img;
img=cv::imread("/home/cgross/Downloads/2.jpg");
return 0;
}
I am not sure of where is the problem
includepath must show opencv and opencv2.just like that:
INCLUDEPATH += C:\\opencv-2.4.6.1\\build-msvc2012\\install\\include
LIBS+=-LC:\\opencv-2.4.6.1\\build-msvc2012\\install\\lib \
-lopencv_calib3d246d \
-lopencv_contrib246d \
-lopencv_core246d \
-lopencv_features2d246d \
-lopencv_flann246d \
lıbs must show .lib file
Your code builds and runs just fine for me on a somewhat antique stock ubuntu 12.10 with opencv 2.3 from the distribution packages.
It picks up a cv::imread(std::string const &, int) from libopencv_highgui.

Undefined reference aiImportfile (assimp) using QT

I have bee trying to get assimp running in QT 5.2 in order to import some 3D objects, but Im having a problem with (I believe) the linker.
I installed it via cmake, first downloading the source files from here http://sourceforge.net/projects/assimp/files/assimp-3.0/, then using cmake to compile and install.
Then, I have tried to run the example they provide in the documentation
#include <assimp/cimport.h> // Plain-C interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
bool DoTheImportThing( const char* pFile)
{
// Start the import on the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll t
// probably to request more postprocessing than we do in this example.
const aiScene* scene = aiImportFile( pFile,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
// If the import failed, report it
if( !scene)
{
// DoTheErrorLogging( aiGetErrorString());
return false;
}
return true;
}
But when trying to compile this piece of code, I get the error
error: undefined reference to `aiImportFile'
error: collect2: error: ld returned 1 exit status
I am using a 32 bit linux mint. Does anybody have any idea why is it not linking? Should I have compiled with cmake using a specific flag? i havent been able to found any special flags in any posts around.
Thanks!
I finally solved it! Apparently, QT has a tool for importing external libraries. I just had to right click on the project, click add library, and add the file located at /user/local/libassimp.a
This added the following lines to my .pro file:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../usr/local/lib/release/ -lassimp
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../usr/local/lib/debug/ -lassimp
else:unix: LIBS += -L$$PWD/../../../../../usr/local/lib/ -lassimp
INCLUDEPATH += $$PWD/../../../../../usr/local/include
DEPENDPATH += $$PWD/../../../../../usr/local/include
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../usr/local/lib/release/libassimp.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../usr/local/lib/debug/libassimp.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../usr/local/lib/release/assimp.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../usr/local/lib/debug/assimp.lib
else:unix: PRE_TARGETDEPS += $$PWD/../../../../../usr/local/lib/libassimp.a
Theres possibly a more elegant way of coding that, but at least it worked.

OpenCV with Qt: The program has unexpectedly finished

I am trying to configure OpenCV with Qt Creator 2.7.0 (Qt 5.0.2) on windows 8 64bit.
While executing my program, I get the following error:
The program has unexpectedly finished.
This is my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
IplImage *image = cvLoadImage("E:\\lena.jpg"); //If this is removed, the program runs OK
return a.exec();
}
My .pro file is
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
unix:!mac {
message("* Using settings for Unix/Linux.")
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib/ \
-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc
}
## OpenCV settings for Mac OS X
macx {
message("* Using settings for Mac OS X.")
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib/ \
-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc
}
## OpenCV settings for Windows and OpenCV
win32 {
message("* Using settings for Windows.")
INCLUDEPATH += "C:\\OpenCV\\opencv\\build\\include" \
"C:\\OpenCV\\opencv\\build\\include\\opencv" \
"C:\\OpenCV\\opencv\\build\\include\\opencv2"
LIBS += -L"C:\\OpenCV\\opencv\\build\\x64\\mingw\\lib" \
-lopencv_core244 \
-lopencv_highgui244 \
-lopencv_imgproc244
}
Environment Variables are:
OPENCV_DIR:C:\OpenCV\opencv\build\x64\mingw Path:
G:\5.0.2\Tools\MinGW\bin;G:\Qt\5.0.2\mingw47_32\bin
What could be the problem ?
I suggest you use the OpenCV 2.x API if possible. The error handling is better.
This would be cv::Mat image = cv::imread("E:\lena.jpg");
If the image is empty, it means you have the wrong path.
Also, make sure the opencv dll are in the path of your executable (core, highgui and imgproc).
You might need to change
-lopencv_core244 \
-lopencv_highgui244 \
-lopencv_imgproc244
to
-lopencv_core244d \
-lopencv_highgui244d \
-lopencv_imgproc244d
note 'd' at the end of each lib (if you are to build in debug)
Have you solved the problem?
Have you tried adding system("PAUSE"); after the return a.exec(); ?
Just some extra notes:
Also try
try
{
...
}
catch (Exception e)
{
...
}
to see if you can find the error
thirdly, check the file permissions for lena.jpg
Lastly, see if you can use the path E:\lena.jpg

Qt C++ ffmpeg cannot find library

I use Qt 5.0.2 with Mingw 4.7 on windows.
I'm working on a c++ program and i'm trying to get the duration of video files. I found ffmpeg. Now i tried to compile a simple program but I think it fails with the libraries. I've tried to include both the shared and dev but they both will fail. ( Zeranoe FFmpeg builds)
This is my pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH += C:\programming\ffmpeg-20130606-git--win32-shared\bin
LIBS += -LC:\programming\ffmpeg-20130606-git--win32-shared\bin -lavcodec-55.dll - lavformat-55.dll -lavutil-52.dll
i get errors like:
":-1: error: cannot find -lavcodec-55.dll"
":-1: error: cannot find -lavformat-55.dll"
I've also tried:
INCLUDEPATH += C:\programming\ffmpeg-20130606-git--win32-dev\include
INCLUDEPATH += C:\programming\ffmpeg-20130606-git--win32-dev\lib
LIBS += -LC:\programming\ffmpeg-20130606-git--win32-dev\lib -lavcodec -lavformat -lavutil
LIBS += -LC:\programming\ffmpeg-20130606-git--win32-dev\lib -llibavcodec.dll.a -llibavformat.dll.a -llibavutil.dll.a
Here it gives a error "During startup program exited with code 0x0000135'
I even tried to include each library individually. But no results..
This is my main file:
#include <iostream>
using namespace std;
extern "C" {
#include <libavcodec/avcodec.h> // required headers
#include <libavformat/avformat.h>
}
int main(int argc, char**argv) {
av_register_all(); // offending library call
return 0;
}
A program simple as this will have 2 outcomes:
the program runs but crashes if i call 'av_register_all()'
It just tells me he cannot find the libraries.
Can someone tell me what it is i'm doing wrong? Or even give me a hint? I can't really find a lot of good documentation on this one.
Thanks in advance!
Configure PRO-file as shown below:
INCLUDEPATH += C:\programming\ffmpeg-20130606-git--win32-dev\include
LIBS += -LC:\programming\ffmpeg-20130606-git--win32-dev\lib
LIBS += -lavcodec -lavformat -lavutil
Then, build the project, copy the all dll libraries from C:\programming\ffmpeg-20130606-git--win32-shared\bin to exe output path and run application.
UPD
Maybe you need to copy some of the following libraries from Qt\5.0.2\mingw47_32\bin\:
libgcc_s_sjlj-1.dll
libstdc++-6.dll
libwinpthread-1.dll