#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
//#include "mydrive.h"
#ifndef PROPERTY
#define PROPERTY
#define image_width 320 //图片显示宽度
#define image_height 240 //图片显示高度240
#define image_Format QImage::Format_RGB888 //图片显示格式
#define cameraDevice "/dev/video0" //摄像头设备
#define haarXML "./data/haarcascade_frontalface_alt2.xml" //人脸模型级联分类器文件
#define imgSizeScaleSmall 0.5 //图像放缩比例
#define imgSizeScaleBig 2 //图像放缩比例
#endif //PROPERTY
extern volatile bool listen;
extern volatile unsigned char carStation;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
imgBuf = (unsigned char *)malloc(image_width * image_height/*QWidget::width()*QWidget::height()*/* 3 * sizeof(char));
frame = new QImage(imgBuf,image_width,image_height,image_Format); //根据存储空间开辟QImage图像
m_camera = new VideoDevice(tr(cameraDevice)); //获取摄像头设备
connect(m_camera, SIGNAL(display_error(QString)), this,SLOT(display_error(QString)));
camReturn = m_camera->open_device(); //打开摄像头
if(-1==camReturn)
{
QMessageBox::warning(this,tr("error"),tr("open /dev/dsp error"),QMessageBox::Yes);
m_camera->close_device();
}
camReturn = m_camera->init_device(); //初始化摄像头
if(-1==camReturn)
{
QMessageBox::warning(this,tr("error"),tr("init failed"),QMessageBox::Yes);
m_camera->close_device();
}
camReturn = m_camera->start_capturing(); //摄像头开始捕获图像
if(-1==camReturn)
{
QMessageBox::warning(this,tr("error"),tr("start capture failed"),QMessageBox::Yes);
m_camera->close_device();
}
if(-1==camReturn)
{
QMessageBox::warning(this,tr("error"),tr("get frame failed"),QMessageBox::Yes);
m_camera->stop_capturing();
}
// fd=open("dev/mygpio",O_RDWR);
//设定时间间隔,对图像界面进行刷新
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(update())); //update将调用paintEvent
timer->start(30);
// Storage for the rectangles detected
cascadeFile = haarXML;
cascade = (CvHaarClassifierCascade *) cvLoad(cascadeFile.toUtf8());
m_FaceCount = 0;
storage = cvCreateMemStorage(0);
//..................................................
stationTimer =new QTimer(this);
connect(stationTimer,SIGNAL(timeout()),this,SLOT(stationUpdate()));
stationTimer->start();
}
Widget::~Widget()
{
delete ui;
}
void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void Widget::paintEvent(QPaintEvent *)
{
uchar * pImgBuf;
unsigned int len;
camReturn = m_camera->get_frame((void **)&pImgBuf,&len);
convert_yuv_to_rgb_buffer(pImgBuf,imgBuf,image_width,image_height/*QWidget::width(),QWidget::height()*/);
frame->loadFromData((uchar *)imgBuf,/*len*/image_width * image_height * 3 * sizeof(char));
//ui->m_imgLabel->setPixmap(QPixmap::fromImage(*frame,Qt::AutoColor));//add
//camReturn = m_camera->unget_frame();//add
//QImage转IplImage
IplImage* src = QImageToIplImage(frame);
if (!src)
{
printf("img error!");
return;
}
//压缩图像大小,提升人脸检测的速度
double sizeScale = imgSizeScaleSmall;
CvSize img_cvsize;
img_cvsize.width = src->width * sizeScale;
img_cvsize.height = src->height * sizeScale;
IplImage* dst = cvCreateImage(img_cvsize, src->depth, src->nChannels);
cvResize(src, dst, CV_INTER_LINEAR);
detect_and_draw(dst); //实现人脸检测
//恢复原图像大小,但图像分辨率有所下降,图像较原始图像模糊
//(亦可用原始图像直接显示,但ARM处理资源有限,不建议耗费太多资源,除此之外暂时还没想到更好的处理方式)
sizeScale = imgSizeScaleBig;
img_cvsize.width = dst->width * sizeScale;
img_cvsize.height = dst->height * sizeScale;
IplImage* img = cvCreateImage(img_cvsize, dst->depth, dst->nChannels);
cvResize(dst, img, CV_INTER_LINEAR);
QImage qimage = QImage((uchar *)img->imageData, img->width,img->height, image_Format);
//IplImage为BGR格式,QImage为RGB格式,所以要交换B和R通道显示才正常
//可以用OpenCV的cvConcertImage函数交换,也可以用QImage的rgbSwapped函数交换;
qimage = qimage.rgbSwapped();
ui->m_imgLabel->setPixmap(QPixmap::fromImage(qimage));
camReturn = m_camera->unget_frame();
cvReleaseImage(&img); //释放图片内存
cvReleaseImage(&src);
}
void Widget::display_error(QString err)
{
QMessageBox::warning(this,tr("error"), err,QMessageBox::Yes);
}
void Widget::stationUpdate()
{
if(listen)
{
ui->labelListen->setText("connect");
}
else
{
ui->labelListen->setText("listern");
}
ui->recline->clear();
switch (carStation)
{
case 0x00:
ui->recline->setText("Stop");
break;
case 0x01:
ui->recline->setText("Up");
break;
case 0x02:
ui->recline->setText("Back");
break;
case 0x03:
ui->recline->setText("Left");
break;
case 0x04:
ui->recline->setText("Right");
break;
default:
ui->recline->setText("Error");
break;
}
}
/*void Widget::carStation()
{
if(lastStation==recbuf[1])
{
return ;
}else{
lastStation=recbuf[1];
}
ui->recline->clear();
switch (recbuf[1])
{
case 0x00:
printf("stop\n");
ui->recline->setText("Stop");
stopRun(fd);
break;
case 0x01:
printf("up\n");
ui->recline->setText("Up");
upRun(fd);
break;
case 0x02:
printf("back\n");
ui->recline->setText("Back");
backRun(fd);
break;
case 0x03:
printf("left\n");
ui->recline->setText("Left");
leftRun(fd);
break;
case 0x04:
printf("right\n");
ui->recline->setText("Right");
rightRun(fd);
break;
default:
printf("error\n");
ui->recline->setText("Error");
break;
}
}*/
/*yuv格式转换为rgb格式*/
int Widget::convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height)
{
unsigned int in, out = 0;
unsigned int pixel_16;
unsigned char pixel_24[3];
unsigned int pixel32;
int y0, u, y1, v;
for(in = 0; in < width * height * 2; in += 4) {
pixel_16 =
yuv[in + 3] << 24 |
yuv[in + 2] << 16 |
yuv[in + 1] << 8 |
yuv[in + 0];
y0 = (pixel_16 & 0x000000ff);
u = (pixel_16 & 0x0000ff00) >> 8;
y1 = (pixel_16 & 0x00ff0000) >> 16;
v = (pixel_16 & 0xff000000) >> 24;
pixel32 = convert_yuv_to_rgb_pixel(y0, u, v);
pixel_24[0] = (pixel32 & 0x000000ff);
pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
rgb[out++] = pixel_24[0];
rgb[out++] = pixel_24[1];
rgb[out++] = pixel_24[2];
pixel32 = convert_yuv_to_rgb_pixel(y1, u, v);
pixel_24[0] = (pixel32 & 0x000000ff);
pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
rgb[out++] = pixel_24[0];
rgb[out++] = pixel_24[1];
rgb[out++] = pixel_24[2];
}
return 0;
}
int Widget::convert_yuv_to_rgb_pixel(int y, int u, int v)
{
unsigned int pixel32 = 0;
unsigned char *pixel = (unsigned char
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
car.rar (32个子文件)
car
RobotVision2.png 1KB
moc_widget.cpp 3KB
RobotVision2.pro 1KB
moc_videodevice.cpp 3KB
data
haarcascade_frontalface_alt2.xml 818KB
haarcascade_frontalface_alt.xml 898KB
mydrive.cpp 914B
widget.h 1KB
mythread.h 515B
mythread.o 5KB
widget.ui 3KB
mydrive.h 383B
mygpio.h 237B
moc_videodevice.o 5KB
ui_widget.h 4KB
start.png 4KB
main.cpp 235B
videodevice.h 908B
moc_widget.o 56KB
RobotVision2 79KB
RobotVision2.pro.user 48KB
widget.o 69KB
mydrive.o 2KB
Makefile 10KB
mythread.cpp 2KB
moc_mythread.o 5KB
main.o 51KB
widget.cpp 10KB
videodevice.o 15KB
RobotVision2.pro.user.1.3 13KB
videodevice.cpp 7KB
moc_mythread.cpp 3KB
qtc_packaging
共 32 条
- 1
资源评论
qustJHJ
- 粉丝: 7
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功