4、基于JZ2440之编写测试代码处理(处理图片识别人脸)

1、代码如下:

void detectAndDisplay(Mat image)
{
    CascadeClassifier ccf;      //创建脸部对象
    //ccf.load(xmlPath);           //导入opencv自带检测的文件
    if(!ccf.load(xmlPath))     //导入opencv自带检测的文件
    {
        cout<<"无法加载xml文件"<<endl;
        return 0;
    }    
    cout<<"成功导入opencv自带检测的文件"<<endl;
    vector<Rect> faces;         
    Mat gray;                       
    cvtColor(image,gray,CV_BGR2GRAY);
    imwrite("灰度.jpg", gray);
    cout<<"结束cvtColor"<<endl;
    equalizeHist(gray,gray);
    imwrite("直方图均匀化.jpg", gray);
    cout<<"结束equalizeHist"<<endl;
    ccf.detectMultiScale(gray,faces,1.1,3,0,Size(50,50),Size(500,500));
    cout<<"开始detectMultiScale"<<endl;
    for(vector<Rect>::const_iterator iter=faces.begin();iter!=faces.end();iter++)
    {
        rectangle(image,*iter,Scalar(0,0,255),2,8); //画出脸部矩形
    }
    Mat image1;
    cout<<"开始detectMultiScale111111111"<<endl;
    for(size_t i=0;i<faces.size();i++)
    {
        Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
        image1= image(Rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height));     
    }
    cout<<"开始detectMultiScale1111111112222222222"<<endl;
    //imshow("1",image);
    imwrite("2.jpg", image);
    //imshow("2",image1);
    imwrite("3.jpg", image1);
    //cvWaitKey(0);
    
}

int main() {
    //VideoPlayer();
    string path="1.jpg";//以检测图片1.jpg为例
    Mat image =imread(path,-1);
    
    //CascadeClassifier a;     //创建脸部对象
    detectAndDisplay(image);// 检测人脸
    return 0;    
}

2、编译指令如下:

  arm-linux-g++ video_capture.cpp -I/usr/local/arm/include/opencv -lopencv_objdetect -lopencv_highgui -lopencv_imgproc -lopencv_core -lpthread -lrt -o video_capture

3、结果如下:

猜你喜欢

转载自www.cnblogs.com/liusiluandzhangkun/p/9047357.html