Qt Code:
  1. void Dialog::createCam()
  2. {
  3. timer = new QTimer(this);
  4. cam = cvCaptureFromCAM(-1);
  5. if(cam==NULL)
  6. qDebug()<<"Error: No Camera Detect.";
  7.  
  8. timer->start(33);
  9. connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
  10. connect(timer,SIGNAL(timeout()),this,SLOT(prcFrame()));
  11. }
  12.  
  13. void Dialog::getFrame()
  14. {
  15. frame = cvQueryFrame(cam); //get what camera see
  16. image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better
  17.  
  18. qPainter = new QPainter(&image);// all qPainter to draw rectangle on original image according to point get from Filter image
  19. qPainter->begin(this);
  20. qPainter->setPen(QPen(Qt::red,3,Qt::SolidLine));
  21. qPainter->drawRect(rect.x,rect.y,rect.x+rect.width,rect.y+rect.height);
  22. qPainter->end();
  23. ui->original->setPixmap(QPixmap::fromImage(image));
  24.  
  25. }
  26.  
  27. void Dialog::prcFrame()
  28. {
  29. imgHSV= cvCloneImage(frame);
  30. cvCvtColor(frame,imgHSV,CV_BGR2HSV);
  31. imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
  32. 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);
  33. QImage imgThresdhed = QImage ((const uchar*)imgFilter->imageData,imgFilter->width,imgFilter->height,QImage::Format_Indexed8).rgbSwapped();
  34. ui->filter->setPixmap(QPixmap::fromImage(imgThresdhed)); //convert to QImage and show as Filter image
  35.  
  36. unsigned char *data_hsv= (unsigned char*)imgHSV->imageData;
  37. int step_hsv = imgHSV->widthStep/sizeof(unsigned char), chanels_hsv=imgHSV->nChannels;
  38. storage=cvCreateMemStorage(0);
  39.  
  40. cvFindContours(imgFilter, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0)); //FindContours
  41.  
  42. for(;contour;contour = contour->h_next)
  43. {
  44. area1=fabs(cvContourArea(contour,CV_WHOLE_SEQ,1 ));
  45.  
  46. if(area1<100 || area1>50000 )
  47. {
  48. cvSeqRemove(contour,0);
  49. continue;
  50. }
  51.  
  52. total =0;
  53. total_S=0;
  54. total_V=0;
  55.  
  56. for(l = 0;l<contour->total;++l)
  57. {
  58. pt = (CvPoint *)cvGetSeqElem(contour,l);
  59. qDebug()<<"pt"<<pt;
  60. H = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+0];
  61. qDebug()<<"H"<<H;
  62. S = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+1];
  63. qDebug()<<"S"<<S;
  64. V = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+2];
  65. qDebug()<<"V"<<V;
  66. total = H + total;
  67. total_S= S + total_S;
  68. total_V= V + total_V;
  69. }
  70.  
  71. avg = total / (contour->total);
  72. avg_S=total_S / (contour->total);
  73. avg_V=total_V / (contour->total);
  74.  
  75. 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
  76. {
  77. for(i = N-1; i >= 0; --i)
  78. {
  79. if(area1 > maxArea1[i])
  80. {
  81. maxArea1[i] = area1;
  82. contours1[i] = contour;
  83. for(m = (i-1); m >= 0; --m)
  84. {
  85. if(maxArea1[m] < maxArea1[m+1])
  86. {
  87. tmp_area1 = maxArea1[m+1];
  88. tmp_cont = contours1[m+1];
  89. maxArea1[m+1] = maxArea1[m];
  90. contours1[m+1] = contours1[m];
  91. maxArea1[m] = tmp_area1;
  92. contours1[m] = tmp_cont;
  93. }
  94. }
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. rect = ((CvContour*)contours1[0])->rect; //rect point
  101.  
  102.  
  103. cvReleaseMemStorage(&storage);
  104. cvReleaseImage(&imgFilter);
  105. cvReleaseImage(&imgHSV);
  106. }
To copy to clipboard, switch view to plain text mode