#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QVector>
#include <cstring>
#include "opencv2/core/core.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
const char* cascade_name = "/mnt/haarcascade_frontalface_alt2.xml";
CascadeClassifier face_cascade;
IplImage *pImage=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
IplImage *p=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
timer = new QTimer;
connect(timer, SIGNAL(timeout()), this, SLOT(slot_timer_positionImage()));
//this->init();
}
Widget::~Widget()
{
delete ui;
timer->stop();
cvReleaseCapture(&pCapture);
cvReleaseImage(&pImage);
}
bool Widget::init()
{
pCapture = NULL;
qDebug()<<"before cvCaptureFromCAM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
if((pCapture = cvCaptureFromCAM(0)) == NULL)
{
return false;
}
qDebug()<<"after cvCaptureFromCAM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
return true;
}
void Widget::slot_timer_positionImage()
{
// qDebug()<<"read camera data!!!!!!!!!!!!!!!!!!!!!!!!!";
if((pImage = cvQueryFrame(pCapture)) != NULL)
{
cvCvtColor(pImage, p, CV_BGR2RGB);
image = QImage((uchar *)p->imageData, p->width, p->height, p->widthStep, QImage::Format_RGB888);
detect_and_draw(pImage);
update();
}
}
void Widget::detect_and_draw(IplImage* img )
{
double scale=8; //1.2
static CvScalar colors[] = {
{{0,0,255}},{{0,128,255}},{{0,255,255}},{{0,255,0}},
{{255,128,0}},{{255,255,0}},{{255,0,0}},{{255,0,255}}
};//Just some pretty colors to draw with
//Image Preparation
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
if( !cascade )
{
fprintf( stderr, "ERROR: Failed to load classifier cascade\n" );
return ;
}
storage = cvCreateMemStorage(0);
IplImage* gray = cvCreateImage(cvSize(img->width,img->height),8,1);
IplImage* small_img=cvCreateImage(cvSize(cvRound(img->width/scale),cvRound(img->height/scale)),8,1);
cvCvtColor(img,gray, CV_BGR2GRAY);
cvResize(gray, small_img, CV_INTER_LINEAR);
cvEqualizeHist(small_img,small_img); //直方图均衡
//Detect objects if any
//
cvClearMemStorage(storage);
double t = (double)cvGetTickCount();
CvSeq* objects = cvHaarDetectObjects(small_img,
cascade,
storage,
1.1,
2,
0/*CV_HAAR_DO_CANNY_PRUNING*/,
cvSize(30,30));
t = (double)cvGetTickCount() - t;
printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
//Loop through found objects and draw boxes around them
for(int i=0;i<(objects? objects->total:0);++i)
{
CvRect* r=(CvRect*)cvGetSeqElem(objects,i);
cvRectangle(img, cvPoint(r->x*scale,r->y*scale), cvPoint((r->x+r->width)*scale,(r->y+r->height)*scale), colors[i%8]);
}
for( int i = 0; i < (objects? objects->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( objects, 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( img, center, radius, colors[i%8], 3, 8, 0 );
}
cvCvtColor(img,img, CV_BGR2RGB);
im = QImage((uchar *)img->imageData, img->width, img->height, img->widthStep, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(im,Qt::AutoColor));
cvReleaseImage(&gray);
cvReleaseImage(&small_img);
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(QRect(0,0, 640, 480), image);
}
bool Widget::ShowImage()
{
timer->start(1000 / 30); // / 24
return true;
}