/*!
  Display a rectangle.

  \param rectangle : Rectangle characteristics.
  \param color : Rectangle color.
  \param fill : When set to true fill the rectangle.

  \param thickness : Thickness of the four lines used to display the
  rectangle. This parameter is only useful when \e fill is set to
  false.

*/
void
vpDisplayOpenCV::displayRectangle(const vpRect &rectangle,
                                  const vpColor &color, bool fill,
                                  unsigned int thickness)
{
  if (displayHasBeenInitialized)
  {
    if (fill == false) {
      if (color.id < vpColor::id_unknown) {
	cvRectangle( background,
		     cvPoint( vpMath::round( rectangle.getLeft() ),
			      vpMath::round( rectangle.getBottom() ) ),
		     cvPoint( vpMath::round( rectangle.getRight() ),
			      vpMath::round( rectangle.getTop() ) ),
		     col[color.id], (int)thickness);
      }
      else {
	cvcolor = CV_RGB(color.R, color.G, color.B) ;
	cvRectangle( background,
		     cvPoint( vpMath::round( rectangle.getLeft() ),
			      vpMath::round( rectangle.getBottom() ) ),
		     cvPoint( vpMath::round( rectangle.getRight() ),
			      vpMath::round( rectangle.getTop() ) ),
		     cvcolor, (int)thickness);

      }
    }
    else {
      if (color.id < vpColor::id_unknown) {
	cvRectangle( background,
		     cvPoint( vpMath::round( rectangle.getLeft() ),
			      vpMath::round( rectangle.getBottom() ) ),
		     cvPoint( vpMath::round( rectangle.getRight() ),
			      vpMath::round( rectangle.getTop() ) ),
		     col[color.id], CV_FILLED);
      }
      else {
	cvcolor = CV_RGB(color.R, color.G, color.B) ;
	cvRectangle( background,
		     cvPoint( vpMath::round( rectangle.getLeft() ),
			      vpMath::round( rectangle.getBottom() ) ),
		     cvPoint( vpMath::round( rectangle.getRight() ),
			      vpMath::round( rectangle.getTop() ) ),
		     cvcolor, CV_FILLED);
      }
    }
  }
  else
  {
    vpERROR_TRACE("OpenCV not initialized " ) ;
    throw(vpDisplayException(vpDisplayException::notInitializedError,
                             "OpenCV not initialized")) ;
  }
}
示例#2
0
/*!

  Check if an image point belongs to a rectangle.
  
  \param rect : the rectangle.
  
  \return Returns true if the point belongs to the rectangle.

*/
bool vpImagePoint::inRectangle( const vpRect &rect ) const
{
  return ( this->i <= rect.getBottom() && 
	   this->i >= rect.getTop() &&
	   this->j <= rect.getRight() &&
	   this->j >= rect.getLeft());
}
/*!
  Return the percentage of overlapping for two rectangles.

  \param r1: First rectangle.
  \param r2: Second rectangle.
  \return The percentage of overlapping or -1.0 if they don't overlap.
*/
double vpCascadeClassifier::getPercentageOfOverlap(const vpRect &r1, const vpRect &r2) {
  double left = (std::max)(r1.getLeft(), r2.getLeft());
  double right = (std::min)(r1.getRight(), r2.getRight());
  double bottom = (std::min)(r1.getBottom(), r2.getBottom());
  double top = (std::max)(r1.getTop(), r2.getTop());

  if(left < right && bottom > top) {
    double overlap_area = (right-left)*(bottom-top);
    double r1_area = r1.getWidth()*r1.getHeight();
    double r2_area = r2.getWidth()*r2.getHeight();
    double percentage = overlap_area / (r1_area + r2_area - overlap_area) * 100.0;
    return percentage;
  }

  return -1;
}
/*!
  Return the average intensity in the region.

  \param I: Grayscale image.
  \param rect: Bounding box corresponding to the desired region.
  \return The average intensity in the region or -1.0 if the region is outside of the image.
*/
double vpCascadeClassifier::getMeanIntensity(const vpImage<unsigned char> &I, const vpRect &rect) {
  bool is_inside = (rect.getLeft() >= 0 && rect.getTop() >= 0
      && rect.getRight() < I.getWidth() && rect.getBottom() < I.getHeight());

  unsigned int total = 0;
  if(is_inside) {
    for(unsigned int i = (unsigned int) rect.getTop(); i < (unsigned int) rect.getBottom(); i++) {
      for(unsigned int j = (unsigned int) rect.getLeft(); j < (unsigned int) rect.getRight(); j++) {
        total += I[i][j];
      }
    }

    return total / ((double) rect.getWidth()*rect.getHeight());
  }

  return -1.0;
}
/*!
  Return the median intensitie in the region.

  \param I: Grayscale image.
  \param rect: Bounding box corresponding to the desired region.
  \return The median intensitie in the region or -1.0 if the region is outside of the image.
*/
double vpCascadeClassifier::getMedianIntensity(const vpImage<unsigned char> &I, const vpRect &rect) {
  bool is_inside = (rect.getLeft() >= 0 && rect.getTop() >= 0
      && rect.getRight() < I.getWidth() && rect.getBottom() < I.getHeight());

  if(is_inside) {
    std::vector<double> v((size_t) (rect.getWidth() * rect.getHeight()));
    for(unsigned int i = (unsigned int) rect.getTop(); i < (unsigned int) rect.getBottom(); i++) {
      for(unsigned int j = (unsigned int) rect.getLeft(); j < (unsigned int) rect.getRight(); j++) {
        size_t index = (size_t) ( (i-rect.getTop())*rect.getWidth() + (j-rect.getLeft()) );
        v[index] = I[i][j];
      }
    }

    return getMedian(v);
  }

  return -1.0;
}