kjyszylkj-darkksider 2015-09-01 13:05 采纳率: 0%
浏览 4002

opencv初学者 为什么调用摄像头 怎么出现捕获源什么框框的。。。

图片说明
用的opencv 4.8 和vs 2010

这是调用的那个cpp代码
/*打开摄像头,通过摄像头进行实时人数统计*/

#include "peoplecounting.h"

#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"
#include "cxcore.h"

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void peoplecounting::SelectTheCamera()
{
//初始化内存及分类器
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;

//加载分类器,名称为"xml.xml"
const char* cascade_name="C:/Users/kjyszylkj/Desktop/460518377Count/Count/Count/xml.xml";
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

if( !cascade )
{
    QMessageBox::information(
        0,
        "错误",
        "不能加载分类器",
        QMessageBox::Ok);
}

//定义某一帧的图片、灰度图及haar图
IplImage* pFrame = NULL;
IplImage* pHaarImg = NULL;
IplImage* pGrayImg = NULL;  

int nFrmNum = 0;

cvNamedWindow("camera",1);

cvMoveWindow("camera",30,0);

//打开自带摄像头
CvCapture* pCapture = cvCreateCameraCapture(-1);

if(!(pCapture))
{
    QMessageBox::information(
        0,
        "错误",
        "不能打开摄像头",
        QMessageBox::Ok);
}
else
{
    while(pFrame = cvQueryFrame(pCapture))                      //如果能够捕获到视频
    {
        nFrmNum++;

        //如果这是第一帧视频,需要申请内存并初始化
        if(nFrmNum == 1)
        {
            pGrayImg = cvCreateImage(cvSize(pFrame->width,pFrame->height),8,1);
            pHaarImg = cvCreateImage(cvSize(cvRound(pFrame->width/1.1),cvRound(pFrame->height/1.1)),8,1);

            //转化为灰度图像再处理
            cvCvtColor(pFrame, pGrayImg,CV_BGR2GRAY);

            //缩放灰度图像
            cvResize(pGrayImg,pHaarImg,CV_INTER_LINEAR);

        }
        else
        {
            cvGrabFrame(pCapture);                                  //抓取一帧

            pFrame = cvRetrieveFrame(pCapture);                     //将图像从缓存中转换为IplImage存在pFrame中

            //转化为灰度图像
            cvCvtColor(pFrame,pGrayImg,CV_BGR2GRAY);

            //缩放灰度图像
            cvResize(pGrayImg, pHaarImg,CV_INTER_LINEAR);

            //直方图均衡化
            cvEqualizeHist(pHaarImg,pHaarImg);

            //分配内存空间
            storage = cvCreateMemStorage(0);

            //人数统计
            if(cascade)
            {
                //检测窗口每次放大1.1倍
                double scale = 1.1;
                int i;

                //使用分类器进行人脸检测
                CvSeq* PeopleCounting = cvHaarDetectObjects( pHaarImg, cascade, storage,
                                            scale, 2, 0/*"CV_HAAR_DO_CANNY_PRUNING"*/,
                                            cvSize(30,30) );

                //在检测到的人脸上画出红色圆圈标记
                for(i=0;i<(PeopleCounting?PeopleCounting->total:0);i++)
                {
                    CvRect* r = (CvRect*)cvGetSeqElem(PeopleCounting,i);                    //返回矩形的信息
                    CvPoint center;                                     
                    int radius;
                    center.x = cvRound((r->x + r->width*0.5)*scale);                        //圆心横坐标
                    center.y = cvRound((r->y + r->height*0.5)*scale);                       //圆心纵坐标
                    radius = cvRound((r->width + r->height)*0.25*scale);                    //半径
                    cvCircle(pFrame, center, radius, CV_RGB(255,0,0), 3, 8, 0 );            //(图像,圆心,半径,颜色,粗细,线条类型,小数点位数)
                }

                //将人数的个数显示到主窗口
                int number;
                number = PeopleCounting->total;
                QString tmp;
                tmp = QString("副窗口画面中一共有%1个人").arg(number);           //文字信息
                ui->textEdit->setText(QString(tmp));                            //设置文字信息
                ui->textEdit->setAlignment( Qt::AlignCenter );                  //设置文字居中

            }

            //显示每一帧的图像
            cvShowImage("camera",pFrame);

            char key = cvWaitKey(1000);

        }
    }

    //释放图像
    cvReleaseImage(&pFrame);
    cvReleaseImage(&pHaarImg);
    cvReleaseImage(&pGrayImg);

    cvWaitKey(0);

    //销毁窗口
    cvDestroyWindow("camera");

    //释放摄像头
    cvReleaseCapture(&pCapture);
}

}

各位大侠求救!!!

  • 写回答

4条回答 默认 最新

  • devmiao 2015-09-01 14:54
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样