#include "mainwindow.h"
#include <QPainter>
#include <iostream>
#include <sstream>
#include <math.h>
using namespace std;
using namespace cv;
MainWindow :: MainWindow()
{
mainWindow = new QWidget;
this->setCentralWidget(mainWindow);
lightModeFlag = 0;
enhancedValue = 0;
addFlag = false;
buttonStart = new QPushButton("START");
buttonStart->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
buttonStop = new QPushButton("STOP");
buttonStop->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
pictureGray = new QLabel();
pictureProcess = new QLabel();
labState = new QLabel("No hand...");
enhancedCheck = new QCheckBox("Enhanced Mode");
upCheck = new QCheckBox("Up Enhance");
downCheck = new QCheckBox("Down Enhance");
dilateCheck = new QCheckBox("dilate");
lightMode = new QComboBox();
lightModeLabel = new QLabel("Light Mode :");
lightMode->addItem("Day Mode ");
lightMode->addItem("Night Mode");
pCapture = cvCreateCameraCapture(1);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_WIDTH,320);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_HEIGHT,240);
// pImage = cvQueryFrame(pCapture);
mainLayout = new QGridLayout;
mainLayout->addWidget(buttonStart,0,0,1,1);
mainLayout->addWidget(buttonStop,0,1,1,1);
mainLayout->addWidget(lightModeLabel,1,0,1,1);
mainLayout->addWidget(lightMode,1,1,1,1);
mainLayout->addWidget(enhancedCheck,2,0,1,1);
mainLayout->addWidget(upCheck,2,1,1,1);
mainLayout->addWidget(dilateCheck,2,3,1,1);
mainLayout->addWidget(downCheck,2,2,1,1);
mainLayout->addWidget(labState,0,6,4,3);
mainLayout->addWidget(pictureProcess,4,0,5,5);
mainLayout->addWidget(pictureGray,4,5,5,5);
mainWindow->setLayout(mainLayout);
QObject::connect(buttonStart,SIGNAL(clicked()),this,SLOT(startVideo()));
QObject::connect(buttonStop,SIGNAL(clicked()),this,SLOT(stopVideo()));
QObject::connect(lightMode,SIGNAL(currentIndexChanged(int)),this,SLOT(setLightMode(int)));
QObject::connect(enhancedCheck, SIGNAL(clicked()), this, SLOT(checkChange()));
QObject::connect(dilateCheck,SIGNAL(clicked()),this,SLOT(dilateChange()));
this->setWindowTitle("beaglebone opencv test~");
this->resize(QSize(680,400));
IplImage * pImageJd = cvLoadImage("jd.PNG",1);
IplImage * pImageBu = cvLoadImage("bu.PNG",1);
IplImage * pImageSt = cvLoadImage("st.PNG",1);
imageSt = new QImage((const uchar *)pImageSt->imageDataOrigin,pImageSt->width,pImageSt->height,QImage::Format_RGB888);
imageBu = new QImage((const uchar *)pImageBu->imageDataOrigin,pImageBu->width,pImageBu->height,QImage::Format_RGB888);
imageJd = new QImage((const uchar *)pImageJd->imageDataOrigin,pImageJd->width,pImageJd->height,QImage::Format_RGB888);
dilateCheck->click();
}
MainWindow :: ~MainWindow()
{
cvReleaseCapture(&pCapture);
mainWindow->deleteLater();
mainLayout->deleteLater();
buttonStart->deleteLater();
buttonStop->deleteLater();
pictureGray->deleteLater();
pictureProcess->deleteLater();
labState->deleteLater();
}
int MainWindow::checkDefect(CvConvexityDefect defect)
{
int temX1 = defect.start->x- defect.depth_point->x;
int temY1 = defect.start->y- defect.depth_point->y;
int temX2 = defect.end->x- defect.depth_point->x;
int temY2 = defect.end->y- defect.depth_point->y;
int temX3 = defect.start->x- defect.end->x;
int temY3 = defect.start->y- defect.end->y;
float length1 = temX1*temX1+temY1*temY1;
float length2 = temX2*temX2+temY2*temY2;
float length3 = temX3*temX3+temY3*temY3;
if((length1>400)
&& (length2>400)
&&(length1+length2<length3*20)
)
{
if(length1+length2>length3)
{
return SHARP;
}
else
{
return JERID;
}
}
else
{
return NOBORN;
}
}
void MainWindow::startVideo()
{
m_nTimerId = startTimer(100);
}
void MainWindow::stopVideo()
{
killTimer(m_nTimerId);
}
void MainWindow::paintEvent(QPaintEvent *event)
{
}
void MainWindow::timerEvent(QTimerEvent *event)
{
/*init*/
IplImage * pImage;
IplImage * grayImage;
IplImage * addImage;
pImage = cvQueryFrame(pCapture);
//pImage = cvLoadImage("test.PNG",1);
if(pImage != NULL)
{
float temsum = 0;
int temCnt = 0;
// QImage showImage((const uchar *)pImage->imageData,pImage->width,pImage->height,pImage->widthStep,QImage::Format_RGB888);
// pictureCamera->setPixmap(QPixmap::fromImage(showImage));
//cvQueryFrame似乎不需要释放内存,释放反而会出错,监控似乎没有内存泄漏
//cvReleaseImage(&pImage);
grayImage = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);
addImage = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);
/*color threshold*/
unsigned char * data =(unsigned char *) pImage->imageData;
int step = pImage->widthStep;
int channels = pImage->nChannels;
int step2 = grayImage->widthStep;
for(int i=0;i<pImage->height;i++)
{
for(int j=0;j<pImage->width;j++)
{
unsigned char pixR,pixG,pixB;
pixB = data[i*step+j*channels];
pixG = data[i*step+j*channels+1];
pixR = data[i*step+j*channels+2];
if((pixR>pixG) &&
(pixG+enhancedValue>pixB) &&
(pixR>pixB) &&
(pixR <240) &&
(pixB > 20)
)
{
float min = pixG<pixB?pixG:pixB;
float tem = min /pixR;
temsum += tem;
temCnt++;
float upValue = 0.8;
float downValue = 0.4;
if(lightModeFlag == 1)
{
upValue +=0.1;
downValue +=0.2;
}
if(upCheck->checkState())
upValue += 0.15;
if(downCheck->checkState())
downValue -= 0.15;
if((tem >downValue)
&&(tem <upValue)
)
grayImage->imageData[i*step2+j] = 0xff;
else
grayImage->imageData[i*step2+j]= 0x00;
}
else
{
grayImage->imageData[i*step2+j] = 0x00;
}
}
}
temsum /= temCnt;
for(int i=0;i<grayImage->height;i++)
{
for(int j=0;j<grayImage->width;j++)
{
if((i==0)||(i == pImage->height-1)||(j==0)||(j == pImage->width-1))
{
addImage->imageData[i*step2+j] = 0x00;
}
else if(grayImage->imageData[i*step2+j])
{
addImage->imageData[i*step2+j] = 0xff;
if(addFlag)
{
for(int m =i-1;m<i+2;m++)
{
for(int n=j-1;n<j+2;n++)
{
addImage->imageData[m*step2+n] = 0xff;
}
}
}
}
else
{
addImage->imageData[i*step2+j] = 0x00;
}
}
}
/*
if(addFlag)
{
for(int i=1;i<pImage->height-1;i++)
{
for(int j=1;j<pImage->width-1;j++)
{
if(grayImage->imageData[i*step2+j])
continue;
#if 1
int cnt =0;
for(int m =i-1;m<i+2;m++)
{
for(int n=j-1;n<j+2;n++)
{
if(grayImage->imageData[m*step2+n])