c++ - 如何以及在何处使用带有 QT 的 basler 相机实现 opencv 人脸检测代码

标签 c++ qt opencv camera face-detection

我在尝试使用我的 basler cam 使 opencv 人脸检测在 QT 中工作时遇到了一些麻烦;我尝试了许多不同的方法来让它工作,使用许多不同的在线示例代码。我似乎根本无法让它工作;此外,我所做的尝试降低了我的帧率。

我用来使用 basler cam 拍摄视频的代码运行良好,我只是在实现人脸检测部分时遇到了问题。我将在下面粘贴我目前为相机和 opencv 编写的代码。代码确实让我时不时出现几个红框,但它并不稳定。我也遇到了这个错误

Failed to load OpenCL runtime

我不确定我做错了什么,也有没有办法在不降低帧率的情况下实现人脸检测,因为它已经很慢了

  #include "mainwindow.h"
#include "ui_mainwindow.h"

#include <opencv2/opencv.hpp>

#include <pylon/PylonIncludes.h>
//#include <pylon/PylonGUI.h>
//#ifdef PYLON_WIN_BUILD
//#include <pylon/PylonGUI.h>
//#endif

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include<time.h>
#include<stdlib.h>


using namespace cv;

// Namespace for using pylon objects.
using namespace Pylon;


// Namespace for using cout.
using namespace std;


static const uint32_t c_countOfImagesToGrab = 100;


cv::CascadeClassifier faceCade;

String faceCascadeName = "/usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml";
String FaceDetectWindow = "Face Detector Window";
String FaceDetectGrayWindow = "Face Detector Gray Window";
size_t i;

vector<Rect> faces;

  cv::Mat camFrames, grayFrames;

int main()
{


    // The exit code of the sample application.
    int exitCode = 0;

    // Automagically call PylonInitialize and PylonTerminate to ensure
    // the pylon runtime system is initialized during the lifetime of this object.
    Pylon::PylonAutoInitTerm autoInitTerm;

faceCade.load( faceCascadeName );

    CGrabResultPtr ptrGrabResult;
    namedWindow("CV_Image",WINDOW_AUTOSIZE);



        CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
          cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
           camera.Open();

           GenApi::CIntegerPtr width(camera.GetNodeMap().GetNode("Width"));
               GenApi::CIntegerPtr height(camera.GetNodeMap().GetNode("Height"));
               Mat cv_img(width->GetValue(), height->GetValue(), CV_8UC3);

               camera.StartGrabbing();
                  CPylonImage image;
                  CImageFormatConverter fc;
                   fc.OutputPixelFormat = PixelType_BGR8packed;

                   while(camera.IsGrabbing()){
                      camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded()){

    fc.Convert(image, ptrGrabResult);


                   cv_img = cv::Mat(ptrGrabResult->GetHeight(),     ptrGrabResult->GetWidth(), CV_8UC3,(uint8_t*)image.GetBuffer());





                   //cvtColor(cv_img, grayFrames, cv::COLOR_BGR2GRAY);
                    //equalizeHist(grayFrames, grayFrames);
                    faceCade.detectMultiScale(cv_img, faces, 1.1, 2, 0, Size(160, 160));




                    for (int i = 0; i < faces.size(); i++)
                  {
                        //Mat faceROI = grayFrames(faces[i]);
                        rectangle(cv_img, Rect(faces[i].x - 25,faces[i].y - 25,faces[i].width + 35 ,faces[i].height + 35),  Scalar(0, 0, 255), 1, 1, 0);
                        Point center(faces[i].x + faces[i].width * 0.5,faces[i].y + faces[i].height * 0.5);



}


                   imshow("CV_Image",cv_img);
                   //imshow("FaceDetectGrayWindow", grayFrames);
                     waitKey(1);
                     if(waitKey(30)==27){
                                          camera.StopGrabbing();
                                    }
}

                   }


}
}

谢谢

最佳答案

我对此不太确定,但detectMultiScale 函数适用于 cv_8u 类型的图像,而且我看到您使用的是 cv_8uc3,据我所知,cv_8u 是具有 1 个 channel 的 8 位像素,cv_8uc3它也是 8 位,但有 3 个 channel ,您需要将图像转换为灰度,我看到您这样做了,但您发表评论了吗?!!! 查看此链接 opencv_face_detection .

也许这会解决你的问题,有些人建议你安装 opencl

sudo apt-get install ocl-icd-opencl-dev

关于c++ - 如何以及在何处使用带有 QT 的 basler 相机实现 opencv 人脸检测代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45882786/

相关文章:

c++ - 使用指针在 C++ 中显示 char 变量的地址?

C++冒泡排序动态分配数组

c++ - 指针作为类变量 Qt

c++ - OSX 上的 Qt 抗锯齿问题

c++ - 在c++中使用x64系统和x86应用程序分配大内存

ios - 将 imageView 中点的位置定位到更大的图像

c++ - 使用 fscanf 了解循环

c++ - 如何检测 Oracle 断开/停止的连接?

OpenCV 窗口无法加载

python opencv crop使用等高线层次结构