void Dialog::createCam()
{
cam = cvCaptureFromCAM(-1);
if(cam==NULL)
qDebug()<<"Error: No Camera Detect.";
timer->start(33);
connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
connect(timer,SIGNAL(timeout()),this,SLOT(prcFrame()));
}
void Dialog::getFrame()
{
frame = cvQueryFrame(cam); //get what camera see
image
= QImage ((const uchar
*)frame
->imageData,frame
->width,frame
->height,
QImage::Format_RGB888).
rgbSwapped();
//rgbSwapped() make color better
qPainter
= new QPainter(&image
);
// all qPainter to draw rectangle on original image according to point get from Filter image qPainter->begin(this);
qPainter
->setPen
(QPen(Qt
::red,
3,Qt
::SolidLine));
qPainter->drawRect(rect.x,rect.y,rect.x+rect.width,rect.y+rect.height);
qPainter->end();
ui
->original
->setPixmap
(QPixmap::fromImage(image
));
}
void Dialog::prcFrame()
{
imgHSV= cvCloneImage(frame);
cvCvtColor(frame,imgHSV,CV_BGR2HSV);
imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
cvInRangeS(imgHSV,cvScalar(ui->hueSlide1->value(),ui->satSlide1->value(),ui->lumSlide1->value(),0),cvScalar(ui->hueSlide2->value(),ui->satSlide2->value(),ui->lumSlide2->value(),0),imgFilter);
QImage imgThresdhed
= QImage ((const uchar
*)imgFilter
->imageData,imgFilter
->width,imgFilter
->height,
QImage::Format_Indexed8).
rgbSwapped();
ui
->filter
->setPixmap
(QPixmap::fromImage(imgThresdhed
));
//convert to QImage and show as Filter image
unsigned char *data_hsv= (unsigned char*)imgHSV->imageData;
int step_hsv = imgHSV->widthStep/sizeof(unsigned char), chanels_hsv=imgHSV->nChannels;
storage=cvCreateMemStorage(0);
cvFindContours(imgFilter, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0)); //FindContours
for(;contour;contour = contour->h_next)
{
area1=fabs(cvContourArea(contour,CV_WHOLE_SEQ,1 ));
if(area1<100 || area1>50000 )
{
cvSeqRemove(contour,0);
continue;
}
total =0;
total_S=0;
total_V=0;
for(l = 0;l<contour->total;++l)
{
pt = (CvPoint *)cvGetSeqElem(contour,l);
qDebug()<<"pt"<<pt;
H = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+0];
qDebug()<<"H"<<H;
S = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+1];
qDebug()<<"S"<<S;
V = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+2];
qDebug()<<"V"<<V;
total = H + total;
total_S= S + total_S;
total_V= V + total_V;
}
avg = total / (contour->total);
avg_S=total_S / (contour->total);
avg_V=total_V / (contour->total);
if((avg>=ui->hueSlide1->value())&&(avg<=ui->hueSlide2->value())&&(avg_S>=ui->satSlide1->value())&&(avg_S<=ui->satSlide2->value())&&(avg_V>=ui->lumSlide1->value())&&(avg_V<=ui->lumSlide2->value())) // compare contours size
{
for(i = N-1; i >= 0; --i)
{
if(area1 > maxArea1[i])
{
maxArea1[i] = area1;
contours1[i] = contour;
for(m = (i-1); m >= 0; --m)
{
if(maxArea1[m] < maxArea1[m+1])
{
tmp_area1 = maxArea1[m+1];
tmp_cont = contours1[m+1];
maxArea1[m+1] = maxArea1[m];
contours1[m+1] = contours1[m];
maxArea1[m] = tmp_area1;
contours1[m] = tmp_cont;
}
}
break;
}
}
}
}
rect = ((CvContour*)contours1[0])->rect; //rect point
cvReleaseMemStorage(&storage);
cvReleaseImage(&imgFilter);
cvReleaseImage(&imgHSV);
}
void Dialog::createCam()
{
timer = new QTimer(this);
cam = cvCaptureFromCAM(-1);
if(cam==NULL)
qDebug()<<"Error: No Camera Detect.";
timer->start(33);
connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
connect(timer,SIGNAL(timeout()),this,SLOT(prcFrame()));
}
void Dialog::getFrame()
{
frame = cvQueryFrame(cam); //get what camera see
image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better
qPainter = new QPainter(&image);// all qPainter to draw rectangle on original image according to point get from Filter image
qPainter->begin(this);
qPainter->setPen(QPen(Qt::red,3,Qt::SolidLine));
qPainter->drawRect(rect.x,rect.y,rect.x+rect.width,rect.y+rect.height);
qPainter->end();
ui->original->setPixmap(QPixmap::fromImage(image));
}
void Dialog::prcFrame()
{
imgHSV= cvCloneImage(frame);
cvCvtColor(frame,imgHSV,CV_BGR2HSV);
imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
cvInRangeS(imgHSV,cvScalar(ui->hueSlide1->value(),ui->satSlide1->value(),ui->lumSlide1->value(),0),cvScalar(ui->hueSlide2->value(),ui->satSlide2->value(),ui->lumSlide2->value(),0),imgFilter);
QImage imgThresdhed = QImage ((const uchar*)imgFilter->imageData,imgFilter->width,imgFilter->height,QImage::Format_Indexed8).rgbSwapped();
ui->filter->setPixmap(QPixmap::fromImage(imgThresdhed)); //convert to QImage and show as Filter image
unsigned char *data_hsv= (unsigned char*)imgHSV->imageData;
int step_hsv = imgHSV->widthStep/sizeof(unsigned char), chanels_hsv=imgHSV->nChannels;
storage=cvCreateMemStorage(0);
cvFindContours(imgFilter, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0)); //FindContours
for(;contour;contour = contour->h_next)
{
area1=fabs(cvContourArea(contour,CV_WHOLE_SEQ,1 ));
if(area1<100 || area1>50000 )
{
cvSeqRemove(contour,0);
continue;
}
total =0;
total_S=0;
total_V=0;
for(l = 0;l<contour->total;++l)
{
pt = (CvPoint *)cvGetSeqElem(contour,l);
qDebug()<<"pt"<<pt;
H = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+0];
qDebug()<<"H"<<H;
S = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+1];
qDebug()<<"S"<<S;
V = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+2];
qDebug()<<"V"<<V;
total = H + total;
total_S= S + total_S;
total_V= V + total_V;
}
avg = total / (contour->total);
avg_S=total_S / (contour->total);
avg_V=total_V / (contour->total);
if((avg>=ui->hueSlide1->value())&&(avg<=ui->hueSlide2->value())&&(avg_S>=ui->satSlide1->value())&&(avg_S<=ui->satSlide2->value())&&(avg_V>=ui->lumSlide1->value())&&(avg_V<=ui->lumSlide2->value())) // compare contours size
{
for(i = N-1; i >= 0; --i)
{
if(area1 > maxArea1[i])
{
maxArea1[i] = area1;
contours1[i] = contour;
for(m = (i-1); m >= 0; --m)
{
if(maxArea1[m] < maxArea1[m+1])
{
tmp_area1 = maxArea1[m+1];
tmp_cont = contours1[m+1];
maxArea1[m+1] = maxArea1[m];
contours1[m+1] = contours1[m];
maxArea1[m] = tmp_area1;
contours1[m] = tmp_cont;
}
}
break;
}
}
}
}
rect = ((CvContour*)contours1[0])->rect; //rect point
cvReleaseMemStorage(&storage);
cvReleaseImage(&imgFilter);
cvReleaseImage(&imgHSV);
}
To copy to clipboard, switch view to plain text mode
Bookmarks