您的位置:首页 > 运维架构

ubuntu 下用 opencv驱动笔记本摄像头问题解决方法

2013-11-22 12:28 489 查看
配置的环境还是: ubuntu 13.04+opencv 2.4.0 +qtcreator 4.7.2~

问题:

比较坑的是解决完无效指针问题后发现opencv 可以正常工作,由于在opencv下视频还有音频没有什么问题,且用cheese还有camorama都可以正常打开笔记本的摄像头。这么说来,摄像头硬件没有问题,网上找到一大堆资料,关于ubuntu下摄像头opencv的问题没有很多,驱动没装,没道理啊~cheese都可以打开了。参考http://www.ideasonboard.org/uvc/又将驱动重新装了一次发现还没有解决问题是。最后查到是
libv4l库与摄像头读取有关,要装这个库,但是在终端输入 locate libv4l发现已经装了。

解决方法:

最后的最后,师兄那里发来一份安装教程,说可能是opencv的信赖库没有装好,重新装一次。但是重装一次感觉还是比较麻烦的,而且教程中有不少库还是装过了的,就找那些没有装过的库给装上,发现还是不行。最后重新从开又装了一次,开头没起好,问题一大堆啊,教训教训。

The Installation Procedure

To install and configure OpenCV 2.4.1, complete the following steps. The commands shown in

each step can be copy and pasted directly into a Linux command line.

1.

Remove any installed versions of ffmpeg and x264.

sudo apt-get remove ffmpeg x264 libx264-dev

2.

Get all the dependencies for x264 and ffmpeg.

sudo apt-get update

sudo apt-get install build-essential checkinstall git cmake

libfaac-dev libjack-jackd2-dev libmp3lame-dev

libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev

libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev

libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev

3.

Download and install gstreamer.

sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev

gstreamer0.10-tools gstreamer0.10-plugins-base

libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good

gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad

gstreamer0.10-ffmpeg

4.

Download and install gtk

sudo apt-get install libgtk2.0-0 libgtk2.0-dev

5.

Download and install libjpeg.

sudo apt-get install libjpeg8 libjpeg8-dev

6.

Create a directory to hold source code

cd ~

mkdir src

7.

Download and install install x264.

1. Download a recent stable snapshot of x264

from ftp://ftp.videolan.org/pub/videolan/x264/snapshots/. The exact version does not

seem to matter. To write this guide, I used version

x264-snapshot-20120528-2245-stable.tar.bz2, but I have used previous versions

too.cd ~/src

wget
ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-
snapshot-20120528-2245-stable.tar.bz2

tar xvf x264-snapshot-20120528-2245-stable.tar.bz2

cd x264-snapshot-20120528-2245-stable

2. Configure and build the x264 libraries.

./configure --enable-static

make

sudo make install

IMPORTANT: If you are running a 64-bit version of Ubuntu, you must configure

x264 as shown in the following command:

./configure --enable-shared --enable-pic

The -shared and -pic options might also be required when you compile for some

other architectures, such as ARM. You know you need these options if you get the

following error when compiling OpenCV:

[ 25%] Building CXX object

modules/highgui/CMakeFiles/opencv_highgui.dir/src/bitstr

m.cpp.o

Linking CXX shared library ../../lib/libopencv_highgui.so

/usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o):

relocation R_X86_64_32S against `av_destruct_packet' can

not be used when making a shared object; recompile with -fPIC

/usr/local/lib/libavcodec.a: could not read symbols: Bad

value

8.

Download and install install ffmpeg.

Download ffmpeg version 0.11.x from http://ffmpeg.org/download.html.
cd ~/src

wget http://ffmpeg.org/releases/ffmpeg-0.11.tar.bz2
tar xvf ffmpeg-0.11.tar.bz2

cd ffmpeg-0.11Configure and build ffmpeg.

./configure --enable-gpl --enable-libfaac

--enable-libmp3lame --enable-libopencore-amrnb

--enable-libopencore-amrwb --enable-libtheora

--enable-libvorbis --enable-libx264 --enable-libxvid

--enable-nonfree --enable-postproc --enable-version3

--enable-x11grab

make

sudo make install

IMPORTANT: Just like with x264 in the previous step, you must configure ffmpeg

with the -shared option if you are running a 64-bit version of Ubuntu or some other

architectures, such as ARM. The -pic option is not necessary for ffmpeg.

./configure --enable-gpl --enable-libfaac

--enable-libmp3lame --enable-libopencore-amrnb

--enable-libopencore-amrwb --enable-libtheora

--enable-libvorbis --enable-libx264 --enable-libxvid

--enable-nonfree --enable-postproc --enable-version3

--enable-x11grab --enable-shared

9.

Download and install install a recent version of v4l (video for linux)

from http://www.linuxtv.org/downloads/v4l-utils/. For this guide I used version 0.8.8.

cd ~/src

wget
http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.8.
tar.bz2

tar xvf v4l-utils-0.8.8.tar.bz2

cd v4l-utils-0.8.8

make

sudo make install

10.

Download and install install OpenCV 2.4.1.

Download OpenCV version 2.4.1 from http://sourceforge.net/projects/opencvlibrary/files/
cd ~/src

wget
http://downloads.sourceforge.net/project/opencvlibrary/o
pencv-unix/2.4.1/OpenCV-2.4.1.tar.bz2

tar xvf OpenCV-2.4.1.tar.bz2Create a new build directory and run cmake:

cd OpenCV-2.4.1/

mkdir build

cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE ..

Verify that the output of cmake includes the following text:

1. found gstreamer-base-0.10

2. GTK+ 2.x: YES

3. FFMPEG: YES

4. GStreamer: YES

5. V4L/V4L2: Using libv4l

1. Build and install OpenCV.

make

sudo make install

11.

Configure Linux.

Tell linux where the shared libraries for OpenCV are located by entering the following

shell command:

export LD_LIBRARY_PATH=/usr/local/lib

Add the command to your .bashrc file so that you don’t have to enter every time your

start a new terminal.

Alternatively, you can configure the system wide library search path. Using your

favorite editor, add a single line containing the text /usr/local/lib to the end of

a file named/etc/ld.so.conf.d/opencv.conf. In the standard Ubuntu

install, the opencv.conf file does not exist; you need to create it. Using vi, for

example, enter the following commands:

sudo vi /etc/ld.so.conf.d/opencv.conf

G

o

/usr/local/lib

<Esc>

:wq!

After editing the opencv.conf file, enter the following command:

sudo ldconfig /etc/ld.so.conf

.Using your favorite editor, add the following two lines to the end of /etc/bash.bashrc:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfi

g

export PKG_CONFIG_PATH

After completing the previous steps, your system should be ready to compile code that

uses the OpenCV libraries. The following example shows one way to compile code for

OpenCV:

g++ `pkg-config opencv --cflags` my_code.cpp

my_code `pkg-config opencv --libs`

-o

建议不要全部照做,容易因为缺软件出错,最好在 root 下先装好以下软件(来自旧版 opencv

官网 http://opencv.willowgarage.com/wiki/InstallGuide : Debian)

apt-get install build-essential

apt-get install cmake

apt-get install pkg-config

apt-get install libpng12-0 libpng12-dev libpng++-dev libpng3

apt-get install libpnglite-dev libpngwriter0-dev libpngwriter0c2

apt-get install zlib1g-dbg zlib1g zlib1g-dev

apt-get install libjasper-dev libjasper-runtime libjasper1

apt-get install pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools

apt-get install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-prog

apt-get install ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev

apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev

apt-get install libxine1-ffmpeg libxine-dev libxine1-bin

apt-get install libunicap2 libunicap2-dev

apt-get install libdc1394-22-dev libdc1394-22 libdc1394-utils

apt-get install swig

apt-get install libv4l-0 libv4l-dev

apt-get install python-numpy以下可选

If you also want to use OpenCV from Python, add this:

apt-get install libpython2.6 python-dev python2.6-dev # Only if you want to use

python

If your system has trouble with building "libjpeg.so", you may need to build it

manually, or try this:

apt-get install libjpeg-progs libjpeg-dev

On Ubuntu 10.10 this apt-get was needed to recognize gstream-app and gstreamer-vid

development headers

apt-get install libgstreamer-plugins-base0.10-dev

反正我是都装上了,就不容易有错。

另外,用 cmake 时因为预设的有 cuda,可能不需要,如果不熟悉如何更改 cmake

文件的话就可以安装 cmake-gui,之后图形界面操作。

可能的错误:

1. fatal: Not a git repository (or any of the parent directories): .git

解决方法

git init

git add .

git commit -m 'Initial commit'

2. Traceback (most recent call last):File "<string>", line 1, in <module>ImportError:

No module named numpy.distutils

或者 ImportError: No module named numpy.distutils

原因:没装 python-numpy

3. 最无语的 ld: cannot find -lcv(以及 -lhighgui, -lcxcore 等等)

原因:找不到库函数 libcv, libhighgui,libcoxe

我的环境是 eclipse,是因为新版 opencv 的一些新函数所以进行了升级

但是筒子们注意了,现在(2.4.1)函数库的名字都改成 libopencv_xx 了,所以应

该写成 opencv_highgui 等。在网上找了链接错误啊之类的原因,折腾了一天,才发现原来已经配置好了,所

以下次不要过于依赖软件,先用命令行检查:

g++ `pkg-config opencv --cflags` my_code.cpp -o my_code `pkg-config opencv --libs`

当然自己先写好 my_code.cpp,如果生成了可执行文件,说明 opencv 配置已经成功了。

最后,运行

#include <cv.h>

#include <opencv/highgui.h>

#include<stdio.h>

#include<opencv2/opencv.hpp>

using namespace cv;

int main( int argc, char** argv )

{

CvCapture* pCapture = cvCreateCameraCapture(0);

cvNamedWindow("Video", 1);

while(1)

{

IplImage* pFrame=cvQueryFrame( pCapture );

if(!pFrame)break;

cvShowImage("Video",pFrame);

char c=cvWaitKey(33);

if(c==27)break;

}

cvReleaseCapture(&pCapture);

cvDestroyWindow("Video");

return 0;

}

终于可以啦~幸福来之不易,好好珍惜。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐