1. 程式人生 > >Linux下opencv環境搭建,opcv2.4.9

Linux下opencv環境搭建,opcv2.4.9

最近做一個arm移植opencv的專案,在Linux虛擬機器上搭建了opencv環境,尋找了幾個教程,這個很靠譜;
OpenCV is the most popular and advanced code library for Computer Vision related applications today, spanning from many very basic tasks (capture and pre-processing of image data) to high-level algorithms (feature extraction, motion tracking, machine learning). It is free software and provides a rich API in C, C++, Java and Python. Other wrappers are available. The library itself is platform-independent and often used for real-time image processing and computer vision.

1.移除先前安裝的ffmpeg和x264

sudo apt-get -qq remove ffmpeg x264 libx264-dev

2.安裝依賴項

sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg cmake qt5-default checkinstall

3.下載並解壓OpenCV

1 wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download
2 unzip OpenCV-$version.zip
3 cd opencv-$version

4.編譯原始碼,構建OpenCV

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j2
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

5.測試OpenCV是否安裝成功

自動編譯opencv檔案程式碼:

#!/bin/bash
echo "compiling $1"
if [[ $1 == *.c ]]
then
    gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`;
elif [[ $1 == *.cpp ]]
then
    g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`;
else
    echo "Please compile only .c or .cpp files"
fi
echo "Output file => ${1%.*}"

將此程式碼儲存成指令碼檔案,在呼叫時把要編譯的opencv檔案作為引數傳進去;

附上編寫的一個簡單的人臉檢測程式;

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <iostream>
using namespace std;
using namespace cv;  
void Face_detect(Mat& img,CascadeClassifier& face_cascade,CascadeClassifier& eye_cascade,double scale);
int main(int argc,char *argv[])
{
	if(argc<2)
	{
		cout<<"please input your 'scale'\n"<<endl;
		return -1;
	}
	if(atoi(argv[1])>9)
	{
		cout<<"please input the number between 0&9\n"<<endl;
		return -1;
	}
	//定義聯機分類器類;
	CascadeClassifier face_cascade,eye_cascade;
	//載入人臉檢測和眼鏡檢測模型;
	face_cascade.load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml");
	eye_cascade.load("/usr/share/opencv/haarcascades/haarcascade_eye.xml");
	//開啟攝像頭
	VideoCapture cap(0);	
	if(!cap.isOpened())
	{
		return -1;
	}
	
	//定義Mat變數
	Mat frame;
	//迴圈進行人臉描框和顯示;
	bool stop=0;
	while(!stop)
	{
		//提出圖片
		cap>>frame;
		//檢測
		Face_detect(frame,face_cascade,eye_cascade,atoi(argv[1]));
		//如果有按鍵按下,退出;
		if(waitKey(30)>=0)
			stop=1;
		//顯示;
		imshow("face",frame);
	}
	return 0;  	
}
//人臉檢測程式;
void Face_detect(Mat& img,CascadeClassifier& face_cascade,CascadeClassifier& eye_cascade,double scale)
{
	const static Scalar colors[] = {
    CV_RGB(0,0,255),
    CV_RGB(0,128,255),
    CV_RGB(0,255,255),
    CV_RGB(0,255,0),
    CV_RGB(255,128,0),
    CV_RGB(255,255,0),
    CV_RGB(255,0,0),
    CV_RGB(255,0,255)} ;
	int i=0;
	vector<Rect> faces;
	//初始化灰度影象和縮小影象,定義scale(縮小尺寸)
	Mat gray, smallImg(cvRound(img.rows/scale),cvRound(img.cols/scale),CV_8UC1);
	//將影象轉換成灰度影象;
	cvtColor(img,gray,CV_BGR2GRAY);
	//將灰度圖縮小;
	resize( gray,smallImg,smallImg.size(),0,0,INTER_LINEAR);
	//直方圖均值化處理;
	equalizeHist( smallImg, smallImg );
	//檢測人臉;
	face_cascade.detectMultiScale( smallImg, faces,1.1, 2, 0|CV_HAAR_SCALE_IMAGE,Size(10, 10));
	
	//將檢測到的每個人臉框出來;
	//r是指向faces矩形的指標;
	for(vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++,i++)
	{
		Point vertex1,vertex2;	
		Scalar color = colors[i%8];
		Mat ImgROI;//眼睛區域;
		vector<Rect> eyes;
		
		
		//儲存人臉矩形的兩個頂點,等比例放大;
		vertex1.x=r->x*scale;
		vertex1.y=r->y*scale;
		
		vertex2.x=(r->x+r->width-1)*scale;
		vertex2.y=(r->y+r->height-1)*scale;
		
		rectangle(img,vertex1,vertex2,color,2);	//將人臉框出;
		
		if(eye_cascade.empty())
			continue;
		ImgROI=smallImg(*r);//*r是(縮小灰度)人臉矩形;
		//檢測眼睛;
		eye_cascade.detectMultiScale(ImgROI,eyes,1.1,2,CV_HAAR_SCALE_IMAGE,Size(5,5));
		for(vector<Rect>::const_iterator er=eyes.begin();er!=eyes.end();er++)
		{
			vertex1.x=(r->x+er->x)*scale;
			vertex1.y=(r->y+er->y)*scale;
			
			vertex2.x=(r->x+er->x+er->width-1)*scale;
			vertex2.y=(r->y+er->y+er->height-1)*scale;
			rectangle(img,vertex1,vertex2,color,2);	//將眼睛框出;
		}
	}
	
}

在呼叫此程式時,需要傳入一個引數,0-9的雙數,表示用來檢測的圖片的縮小規格;
在呼叫此函式時,注意將虛擬機器上攝像頭開啟;
虛擬機器攝像頭