opencv例子(显示图片,YUV找人脸)

1 装各种库
yum install  gtk+-devel zlib-devel libtiff-devel libjpeg-devel libpng-devel gstreamer-devel libavc1394-devel libraw1394-devel libdc1394-devel jasper-devel jasper-utils  python-devel numpy gstreamer-plugins-base-devel


2 装ffmpeg
CentOS 6和7安装方法是不一样的,下面分别说明:
安装前都需要先安装epel扩展源
yum -y install epel-release
CentOS 6比较简单,安装yum源之后直接安装即可:
su -c 'yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-6.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-6.noarch.rpm'
yum -y install ffmpeg ffmpeg-devel
而CentOS 7需额外安装扩展源:
su -c 'yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm'

rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum -y install ffmpeg ffmpeg-devel

3 make make install


4 opencv 安装

cd opencv
mkdir build
cd build
cmake ..
make -j 10
make install

如果执行cmake时候出现如下错误信息
FATAL: In-source builds are not allowed.

则应该是在代码根目录下直接执行过cmake,导致根目录下生成了CMakeCache.txt,需要删除CMakeCache.txt再次执行编译即可。

5 例子  显示图片
#include "cv.h"
#include "highgui.h"
int main(int argc, char **argv)
{
    IplImage* pImage;
    if(argc==2 && (pImage=cvLoadImage(argv[1],1))!=0)
    {
        cvNamedWindow("Image",1);
        cvShowImage("Image",pImage);
        cvWaitKey(0);
        cvDestroyWindow("Image");
        cvReleaseImage(&pImage);
        return 0;
    }
    return -1;
}

g++ `pkg-config --cflags opencv` -o show show.c `pkg-config --libs opencv`

YUV找人脸

    Mat yuvImg;
    yuvImg.create(height, width , CV_8UC1);
    const int framesize = width * height ;
    memcpy(yuvImg.data, pYuvBuf, framesize*sizeof(unsigned char));
    vector faces;

    faceCascade.detectMultiScale(yuvImg, faces, 1.1, 3, 0, Size(0, 0));

if(faces.size()>0){
       printf("%d\n", faces.size());
       for(int i =0; i           printf("x:%d,y:%d,w:%d,h:%d\n", faces[i].x, faces[i].y,faces[i].width, faces[i].height);

           rectangle(yuvImg, Point(faces[i].x, faces[i].y), Point(faces[i].x +faces[i].width, faces[i].y + faces[i].height),

                      Scalar(0, 255, 0), 1, 8);

       }
      imshow("FacesOfPrettyGirl", yuvImg);
      cvWaitKey(0);
    }

YUV2RGB找人脸

CascadeClassifier faceCascade;

faceCascade.load("xxx/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_alt.xml"); 

 

int facedectect_yuv(u_char* pYuvBuf, int width, int height)

{

   Mat yuvImg;

   yuvImg.create(height*3/2, width, CV_8UC1);

   const int framesize = width * height * 3 / 2;

   memcpy(yuvImg.data, pYuvBuf, framesize*sizeof(unsigned char));

   Mat rgbImg;

   //yuv 2 rgb

   cvtColor(yuvImg, rgbImg, CV_YUV2BGR_I420);

    Mat imgGray;

   vector faces;

   if(rgbImg.empty()){

       return 1;

    }

   if(rgbImg.channels() ==3){

       cvtColor(rgbImg, imgGray, CV_RGB2GRAY);

   }else{

       imgGray = rgbImg;

    }

   faceCascade.detectMultiScale(imgGray, faces, 1.2, 6, 0, Size(0, 0));

   return faces.size();

   if(faces.size()>0){

       printf("%d\n", faces.size());

       for(int i =0; i

           printf("x:%d,y:%d,w:%d,h:%d\n", faces[i].x, faces[i].y,faces[i].width, faces[i].height);

           rectangle(rgbImg, Point(faces[i].x, faces[i].y), Point(faces[i].x +faces[i].width, faces[i].y + faces[i].height),

                      Scalar(0, 255, 0), 1, 8);

       }

    }

   imshow("FacesOfPrettyGirl", rgbImg);

   cvWaitKey(0);

   return 0;

}

JPG找人脸

  // 【1】加载分类器  
    CascadeClassifier cascade;  
    cascade.load("haarcascade_frontalface_alt2.xml");  
 
    Mat srcImage, grayImage,dstImage;  
    // 【2】读取图片  
    srcImage = imread("image.jpg");  
    dstImage = srcImage.clone();  
    imshow("【原图】", srcImage);  
 
    grayImage.create(srcImage.size(), srcImage.type());  
    cvtColor(srcImage, grayImage, CV_BGR2GRAY); // 生成灰度图,提高检测效率  
    vector rect;  
    cascade.detectMultiScale(grayImage, rect, 1.1, 3, 0);  // 分类器对象调用  
    printf("检测到人脸个数:%d\n", rect.size()); 



问题:

opencv直接用haarcascade_frontalface_alt2找人脸不是很准。如:

opencv例子(显示图片,YUV找人脸)_第1张图片

你可能感兴趣的:(opencv)