Ejemplo n.º 1
0
void *Gear_FaceTrack::loopThread()
{
  _keepLooping = true;

  while (_keepLooping)
  {
    // Tell the filter that we want a new frame
    pthread_cond_wait(&_inputCond, &_inputMutex);
    if ( !_keepLooping )
      break;
    
    _pCurrentFrame = (unsigned char*) _grayImage.data();
    rgba2grayscale(_pCurrentFrame, (unsigned char*)_image->data(), _image->size());
    
    computeIntegralImages();
    findFaces();
    
    pthread_mutex_lock(&_outputMutex);
    //    mergeFaces();
    _drawingFoundFaces =  _foundFaces;
    pthread_mutex_unlock(&_outputMutex);
  }

  NOTICE("Exiting thread");
//   pthread_mutex_unlock(&_exitMutex);
  pthread_exit(NULL);
  return NULL;
}
Ejemplo n.º 2
0
// ######################################################################
void FaceDetector::evolve()
{
  // check if the current image is updated
  its_Curr_Img_mutex.lock();
  bool newImageFlag = (itsPrevProcImgID < itsCurrImgID);
  its_Curr_Img_mutex.unlock();

  // if so, process
  if(newImageFlag)
    {
      itsTimer.reset();

      its_Curr_Img_mutex.lock();
      itsProcImg = itsCurrImg;
      itsPrevProcImgID = itsCurrImgID;
      its_Curr_Img_mutex.unlock();

      // find faces
      Image<PixRGB<byte> > res = findFaces(itsProcImg);

      // display the image
      itsOfs->writeRGB(res, "display");
      itsOfs->updateNext();
      LINFO("time: %15.3f", itsTimer.get()/1000.0);

      // publish FacesMessage
      BeobotEvents::FacesMessagePtr msg =
        new BeobotEvents::FacesMessage;
      msg->RequestID = itsPrevProcImgID;

      its_Curr_Res_mutex.lock();
      LINFO("Number of faces detected : %" ZU , itsCurrentFacesFound.size());
      for(uint i = 0; i < itsCurrentFacesFound.size(); i++)
        {
          ImageIceMod::RectangleIce rect;
          rect.tl.i = itsCurrentFacesFound[i].top();
          rect.tl.j = itsCurrentFacesFound[i].left();
          rect.br.i = itsCurrentFacesFound[i].bottomO();
          rect.br.j = itsCurrentFacesFound[i].rightO();
          msg->faces.push_back(rect);
          LINFO("[%3d,%3d,%3d,%3d]", rect.tl.i,rect.tl.j,rect.br.i,rect.br.j );
        }
      its_Curr_Res_mutex.unlock();

      // publish the message
      LINFO("[%6d] Publishing Faces Message", itsCurrMessageID++);
      publish("FacesMessageTopic", msg);
    }
}
Ejemplo n.º 3
0
void MainWindow::updateFrame(const cv::Mat &currentFrame)
{
    cv::Mat currentFramePortrait;

    this->currentFrame = currentFrame;

    // Display frame
    QPixmap pixmap;

    int frameWidth;
    int frameHeight;
    if(page == MainPage)
    {
        frameWidth = ui->frameLabel->width();
        frameHeight = ui->frameLabel->height();
    }
    else
    {
        frameWidth = addFrameLabel->width();
        frameHeight = addFrameLabel->height();
    }

    if(recognizeFaces)
    {
        if(faceRecThread == NULL || (faceRecThread != NULL && faceRecThread->isFinished()))
        {
            findFaces();
        }

        if(detectedFaces.size() > 0)
        {
            showDetectedFaces(currentFrame);
        }
    }

    QMatrix rm;

    Qt::ScreenOrientation orientation = screen->orientation();
    switch(orientation)
    {
    case Qt::LandscapeOrientation:
        frame = Utilities::MatToQImage(this->currentFrame);
        pixmap = QPixmap::fromImage(frame).scaled(frameWidth, frameHeight);
        break;
    case Qt::InvertedLandscapeOrientation:
        rm.rotate(180);
        frame = Utilities::MatToQImage(this->currentFrame);
        frame = frame.transformed(rm);
        pixmap = QPixmap::fromImage(frame).scaled(frameWidth, frameHeight);
        break;
    case Qt::PortraitOrientation:
        cv::transpose(this->currentFrame, currentFramePortrait);
        frame = Utilities::MatToQImage(currentFramePortrait);
        pixmap = QPixmap::fromImage(frame).scaled(frameWidth, frameHeight);
        break;
    case Qt::InvertedPortraitOrientation:
        rm.rotate(180);
        cv::transpose(this->currentFrame, currentFramePortrait);
        frame = Utilities::MatToQImage(currentFramePortrait);
        frame = frame.transformed(rm);
        pixmap = QPixmap::fromImage(frame).scaled(frameWidth, frameHeight);
        break;
    }

    if(page == MainPage)
    {
        ui->frameLabel->setPixmap(pixmap);
    }
    else
    {
        addFrameLabel->setPixmap(pixmap);
    }


}
Ejemplo n.º 4
0
char operateImage(Userdata *userdata) {
	if (!userdata) {
		return 0;
	}

	IplImage *image1 = userdata->input[0];
	IplImage *image2 = userdata->input[1];
	IplImage *imageOut = userdata->output[0];
	IplImage *imageOut2 = userdata->output[1];

	static int color_mode = 4;
	static int smooth_mode = 0;
	static int otsu_mode = 0;
	static int close_mode = 0;
	static int canny_mode = 0;
	static int contour_mode = 0;
	static int hsv_mode = 0;
	static int save_img = 0;
	static int history_mode = 0;

	int key = userdata->key;
	switch (key) {
	case 'g':
		color_mode++;
		color_mode %= 5;
		break;
	case 's':
		smooth_mode = !smooth_mode;
		break;
	case 'o':
		otsu_mode = !otsu_mode;
		break;
	case 'e':
		close_mode = !close_mode;
		break;
	case 'c':
		canny_mode = !canny_mode;
		break;
	case 'b':
		contour_mode = !contour_mode;
		break;
	case 'h':
		hsv_mode = !hsv_mode;
		break;
	case 'H':
		history_mode = !history_mode;
		break;
	case 'S':
		save_img = 1;
		break;
	default:
		//cout << key << "\n";
		break;
	}

	int value = userdata->value;
	int kernelSize = userdata->kernelSize;
	kernelSize += 1 - (kernelSize % 2);
	int lowThreshold = userdata->lowThreshold;
	int highThreshold = userdata->highThreshold;
	CvScalar minScalar = cvScalar(userdata->minScalar0, userdata->minScalar1, userdata->minScalar2);
	CvScalar maxScalar = cvScalar(userdata->maxScalar0, userdata->maxScalar1, userdata->maxScalar2);

	static IplImage *tmp1d = cvCreateImage(cvGetSize(image1), IPL_DEPTH_8U, 1);
	static IplImage *tmp3d = cvCreateImage(cvGetSize(image1), IPL_DEPTH_8U, 3);
	cvCopy(image1, tmp3d, NULL);

	filterByHSV(tmp3d, minScalar, maxScalar, tmp3d);
	
	static int tick = 0;
	static CvRect face = cvRect(0,0,1,1);
	if ((tick %= 10) == 0) {
		//userdata->timestep = 100;
		double scale = 4;
		CvRect *faces = NULL;
		double t = 0;
		int facesCount = findFaces(tmp3d, &faces, scale, &t);
		face = (facesCount != 0) ? faces[0] : face;
		free(faces);
		//printf("%d face(s) detected in %g ms :: 1st face at {%d,%d,%d,%d}", facesCount, t, face.x, face.y, face.width, face.height);
		drawFaces(tmp3d, 1, &face);
	}
	tick++;

	//face extraction
	IplImage *subimage = createSubArray(tmp3d, face);
	cvNamedWindow(CONTROL_WINDOW  "face", 0);
	cvResizeWindow(CONTROL_WINDOW "face", subimage->width, subimage->height);
	cvShowImage(CONTROL_WINDOW    "face", subimage);

	//face histogram
	IplImage *subimage2 = cvCloneImage(subimage);
	cvCvtColor(subimage2, subimage2, CV_BGR2HSV);
	size_t binsCount = 0;
	size_t *bins = NULL;
	//printf("%d (%p)", binsCount, &bins);
	size_t ret = calcularHistograma(subimage2, &binsCount, &bins);
	//printf(" ret=%d %d (%p)", ret, binsCount, bins);
	CvScalar maxValues;
	if (bins) {
		for (size_t i = 0; i < binsCount; i++) {
			size_t idx = 0;
			for (size_t j = 0; j < 256; j++) {
				if (bins[i * 256 + j] > bins[idx]) {
					idx = j;
				}
			}
			maxValues.val[i] = idx;
		}
		free(bins);
	}
#if 0
	if (subimage->width > 10 && subimage->height > 10)
	graficarHistograma(subimage, binsCount, bins);
	cvNamedWindow(CONTROL_WINDOW  "42", 0);
	cvResizeWindow(CONTROL_WINDOW "42", subimage->width, subimage->height);
	cvShowImage(CONTROL_WINDOW    "42", subimage);
#endif

#if 0
	int minH = (int)maxValues.val[0] - 20;
	int maxH = (int)maxValues.val[0] + 20;
	int minS = (int)maxValues.val[1] - 20;
	int maxS = (int)maxValues.val[1] + 20;
	int minV = (int)maxValues.val[2] - 20;
	int maxV = (int)maxValues.val[2] + 20;

	minH = minH < 0 ? 180 - minH : minH;
	maxH = maxH > 180 ? maxH - 180 : maxH;

	printf("%d,%d,%d %d,%d,%d", minH, minS, minV, maxH, maxS, maxV);

	filterByHSV(subimage2, cvScalar(minH, minS, minV, 0), cvScalar(maxH, maxS, maxV, 0), subimage2);
	filterByHSV(subimage2, minScalar, maxScalar, subimage2);
	cvCvtColor(subimage2, subimage2, CV_HSV2BGR);

	cvNamedWindow(CONTROL_WINDOW "41", 0);
	cvResizeWindow(CONTROL_WINDOW "41", subimage2->width, subimage2->height);
	cvShowImage(CONTROL_WINDOW "41", subimage2);
#endif

	cvReleaseImage(&subimage);
	cvReleaseImage(&subimage2);
	
	cvCopy(image1, image2, NULL);
	cvCopy(imageOut, imageOut2, NULL);
	cvCopy(tmp3d, imageOut, NULL);

	//cvReleaseImage(&tmp1d);
	//cvReleaseImage(&tmp3d);

	//afterProcess(userdata);

	printf("\r");
	return 0;
}