#include "navideo.h"
#include "ui_navideo.h"
navideo::navideo(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::navideo)
{
ui->setupUi(this);
MaxMin = false;
isDrawing = false;
VideoOpen = false;
RectDraw = false;
RMax = 256; RMin = 256;
GMax = 256; GMin = 256;
BMax = 256; BMin = 256;
SizeValue = 50;
BrigValue = 50;
AreaValue = 50;
LineColor = Qt::green;
BrushColor = Qt::yellow;
FrameColor = Qt::red;
LabX = ui->lab_video->x();
LabY = ui->lab_video->y();
LineX = ui->lab_line->x();
LineY = ui->lab_line->y();
BrushX = ui->lab_brush->x();
BrushY = ui->lab_brush->y();
FrameX = ui->lab_frame->x();
FrameY = ui->lab_frame->y();
LabWidth = ui->lab_video->width();
LabHeight = ui->lab_video->height();
LineWidth = ui->lab_line->width();
LineHeight = ui->lab_line->height();
BrushWidth = ui->lab_brush->width();
BrushHeight = ui->lab_brush->height();
FrameWidth = ui->lab_frame->width();
FrameHeight = ui->lab_frame->height();
timer = new QTimer(this);
qCamera = new QImage();
connect(timer, SIGNAL(timeout()), SLOT(ReadFrame()));
}
navideo::~navideo()
{
delete ui;
}
void navideo::on_btn_camera_clicked()
{
if(!VideoOpen)
{
ui->btn_camera->setText("Camera CLOSE");
pCapture = cvCreateCameraCapture(0);
timer->start(VIDEORATE);
VideoOpen = true;
}
else
{
timer->stop();
ui->btn_camera->setText("Camera OPEN");
cvReleaseCapture(&pCapture);
cvReleaseImage(&pCameraReshape);
ui->lab_video->clear();
VideoOpen = false;
ui->lab_infoR->setText("null");
ui->lab_infoG->setText("null");
ui->lab_infoB->setText("null");
ui->lab_infoUD->setText("null");
ui->lab_infoLR->setText("null");
ui->lab_infoCE->setText("null");
}
}
void navideo::ReadFrame()
{
pCamera = cvQueryFrame(pCapture);
TarPic.width = pCamera->width * SizeValue/100;
TarPic.height = pCamera->height * SizeValue/100;
int DeltaI = (TarPic.width - LabWidth)/2;
int DeltaJ = (TarPic.height - LabHeight)/2;
int GrayMax = qRound(RMax * 0.299 + GMax * 0.587 + BMax * 0.114);
int GrayMin = qRound(RMin * 0.299 + GMin * 0.587 + BMin * 0.114);
if(SizeValue <= 50)
{
DeltaI = 0;DeltaJ = 0;
int DeltaX = LabWidth - TarPic.width;
int DeltaY = LabHeight - TarPic.height;
LabWidth = TarPic.width;
LabHeight = TarPic.height;
LabX = LabX + DeltaX/2;
LabY = LabY + DeltaY/2;
ui->lab_video->resize(LabWidth, LabHeight);
ui->lab_video->move(LabX, LabY);
}
pStorage = cvCreateMemStorage(0);
pContourImage = cvCreateImage(TarPic, pCamera->depth, 3);
pCameraReshape = cvCreateImage(TarPic, pCamera->depth, 3);
pCameraContour = cvCreateImage(TarPic, pCamera->depth, 1);
cvResize(pCamera, pCameraReshape, CV_INTER_AREA);
cvCvtColor(pCameraReshape, pCameraReshape, CV_BGR2YUV);
LUTMat = cvCreateMatHeader(1, 256, CV_8UC3);//change YUV Channels values
cvSetData(LUTMat, LUTPara, 0);
for(int ki = 0; ki < 256; ki++)
{
BrigPara = ki + 5 * (BrigValue - 50);
if(BrigPara > 255){BrigPara = 255;}
else if(BrigPara < 0){BrigPara = 0;}
LUTPara[ki][0] = (uchar)BrigPara;
LUTPara[ki][1] = (uchar)ki;
LUTPara[ki][2] = (uchar)ki;
}
cvLUT(pCameraReshape, pCameraReshape, LUTMat);
cvCvtColor(pCameraReshape, pCameraReshape, CV_YUV2RGB);
cvCvtColor(pCameraReshape, pCameraContour, CV_RGB2GRAY);
cvThreshold(pCameraContour, pCameraContour, GrayMin, 0, CV_THRESH_TOZERO);
cvThreshold(pCameraContour, pCameraContour, GrayMax, 0, CV_THRESH_TOZERO_INV);
cvMorphologyEx(pCameraContour, pCameraContour, NULL, NULL, CV_MOP_OPEN, 2);
cvFindContours(pCameraContour, pStorage, &FirstContour, sizeof(CvContour), CV_RETR_LIST);
double ThresholdLow = (0.02 * AreaValue - AREAFIXED) * PixArea;
double ThresholdHigh = (0.02 * AreaValue + AREAFIXED) * PixArea;
if(ui->check_single->isChecked())
{
if(PixArea < LabWidth * LabHeight/9)
{
ThresholdHigh = THRESHOLDPARA * ThresholdHigh;
}
else if(PixArea > 4 * LabWidth * LabHeight/9)
{
ThresholdLow = ThresholdLow/THRESHOLDPARA;
}
else
{
ThresholdLow = 2 * ThresholdLow/THRESHOLDPARA;
ThresholdHigh = THRESHOLDPARA * ThresholdHigh/2;
}
}
for (GetContourTag = FirstContour; GetContourTag != NULL; GetContourTag = GetContourTag->h_next)
{
ContourArea = fabs(cvContourArea(GetContourTag, CV_WHOLE_SEQ));
if((ContourArea > ThresholdLow)&(ContourArea < ThresholdHigh))
{
TargetRect = cvBoundingRect(GetContourTag);
LeftBou = TargetRect.x - DeltaI; RightBou = TargetRect.x + TargetRect.width - DeltaI;
UpBou = TargetRect.y - DeltaJ; DownBou = TargetRect.y + TargetRect.height - DeltaJ;
if(UpBou < 1){UpBou = 1;} else if(UpBou > LabHeight - 1){UpBou = LabHeight - 1;}
if(DownBou < 1){DownBou = 1;} else if(DownBou > LabHeight - 1){DownBou = LabHeight - 1;}
if(LeftBou < 1){LeftBou = 1;} else if(LeftBou > LabWidth - 1){LeftBou = LabWidth - 1;}
if(RightBou < 1){RightBou = 1;} else if(RightBou > LabWidth - 1){RightBou = LabWidth - 1;}
CenterX = (LeftBou + RightBou)/2; CenterY = (UpBou + DownBou)/2;
PicRGB = cvGet2D(pCameraReshape, CenterY + DeltaJ, CenterX + DeltaI);
if((PicRGB.val[0] >= RMin)&&(PicRGB.val[0] <= RMax)&&(PicRGB.val[1] >= GMin)&&
(PicRGB.val[1] <= GMax)&&(PicRGB.val[2] >= BMin)&&(PicRGB.val[2] <= BMax))
{
RectDraw = true;
ui->lab_infoUD->setText(tr("%1,%2").arg(UpBou).arg(DownBou));
ui->lab_infoLR->setText(tr("%1,%2").arg(LeftBou).arg(RightBou));
ui->lab_infoCE->setText(tr("%1,%2").arg(CenterX).arg(CenterY));
break;
}
}
if(GetContourTag->h_next == NULL)
{
ui->lab_infoUD->setText("null");
ui->lab_infoLR->setText("null");
ui->lab_infoCE->setText("null");
}
}
for(int i = 0; i < LabWidth; i++)//change RGB Channels values
{
for(int j = 0; j < LabHeight; j++)
{
PicRGB = cvGet2D(pCameraReshape, j + DeltaJ, i + DeltaI);
if(isDrawing)
{
if((i > XStartSave)&&(i < XEndSave)&&(j > YStartSave)&&(j < YEndSave))
{
if(MaxMin)
{
RMaxSave = PicRGB.val[0];
RMinSave = PicRGB.val[0];
GMaxSave = PicRGB.val[1];
GMinSave = PicRGB.val[1];
BMaxSave = PicRGB.val[2];
BMinSave = PicRGB.val[2];
MaxMin = false;
}
else
{
RMaxSave = qMax((int)PicRGB.val[0], RMaxSave);
RMinSave = qMin((int)PicRGB.val[0], RMinSave);
GMaxSave = qMax((int)PicRGB.val[1], GMaxSave);
GMinSave = qMin((int)PicRGB.val[1], GMinSave);
BMaxSave = qMax((int)PicRGB.val[2], BMaxSave);
BMinSave = qMin((int)PicRGB.val[2], BMinSave);
}
}
}
if((PicRGB.val[0] >= RMin)&&(PicRGB.val[0] <= RMax)&&(PicRGB.val[1] >= GMin)&&
(PicRGB.val[1] <= GMax)&&(PicRGB.val[2] >= BMin)&&(PicRGB.val
QT与OpenCV编写摄像头图像处理程序
5星 · 超过95%的资源 需积分: 38 14 浏览量
2014-02-13
16:53:31
上传
评论 12
收藏 233KB ZIP 举报
sy19900209
- 粉丝: 0
- 资源: 1
最新资源
- SDN相关软件包,ryn控制器,python环境等
- 基于PWM方法控制音调,支持高、低两个阶段的音调
- 524springboot + vue 在线动漫信息平台.zip(可运行源码+数据库文件+文档)
- 基于QT+QML+C++实现的显示fps帧率的控件+源码
- Java毕业设计-基于Springboot+Vue水果购物网站源码+数据库+PPT文档+演示视频(高分项目).zip
- AT Disk Imager 一个可以自动烧录树莓派镜像的软件
- 523springboot + vue 点餐平台网站.zip(可运行源码+数据库文件+文档)
- 4-2-2.zip
- 522springboot + vue 租房网站.zip(可运行源码+数据库文件+文档)
- Java毕业设计-基于Springboot+Vue大学生竞赛管理系统-源码+数据库+演示视频(高分项目).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页