Back to index

salome-geom  6.5.0
Public Types | Public Member Functions | Private Member Functions | Private Attributes
ShapeRec_FeatureDetector Class Reference

#include <ShapeRec_FeatureDetector.hxx>

Collaboration diagram for ShapeRec_FeatureDetector:
Collaboration graph
[legend]

List of all members.

Public Types

typedef std::vector< cv::Point > CvContour
typedef std::vector
< std::vector< cv::Point > > 
CvContoursArray

Public Member Functions

 ShapeRec_FeatureDetector ()
 Constructor.
void SetPath (const std::string &)
 Sets the path of the image file to be processed.
void SetROI (const QRect &)
 Stores a region of interest given by user in rect.
CvPoint2D32f * GetCorners ()
CvContoursArray GetContours ()
std::vector< cv::Vec4i > GetLines ()
std::vector< cv::Vec4i > GetContoursHierarchy ()
int GetCornerCount ()
int GetImgHeight ()
int GetImgWidth ()
std::string CroppImage ()
 Crops the image located at imagePath to the region of interest given by the user via SetROI and stores the result in /tmp.
void ComputeCorners ()
 Computes the corners of the image located at imagePath.
bool ComputeLines ()
 Computes the lines in the image located at imagePath.
bool ComputeContours (int method)
 Computes the contours of the image located at imagePath.

Private Member Functions

void _detectAndRetrieveContours (cv::Mat binaryImg)
 Performs contours detection and store them in contours.
cv::Mat _colorFiltering ()
 Performs color filtering from the image sample contained in the ROI rect of the image located at imagePath Thresholds the result in order ot obtain a binary image.

Private Attributes

std::string imagePath
CvPoint2D32f * corners
int cornerCount
CvContoursArray contours
std::vector< cv::Vec4i > hierarchy
std::vector< cv::Vec4i > lines
int imgHeight
int imgWidth
CvRect rect

Detailed Description

Definition at line 42 of file ShapeRec_FeatureDetector.hxx.


Member Typedef Documentation

typedef std::vector<cv::Point> ShapeRec_FeatureDetector::CvContour

Definition at line 46 of file ShapeRec_FeatureDetector.hxx.

typedef std::vector<std::vector<cv::Point> > ShapeRec_FeatureDetector::CvContoursArray

Definition at line 47 of file ShapeRec_FeatureDetector.hxx.


Constructor & Destructor Documentation

Constructor.

Parameters:
theFilename- image to process

Definition at line 42 of file ShapeRec_FeatureDetector.cxx.

                                                  : 
  corners()
{
  cornerCount = 2000;
  rect=cvRect(0,0,0,0);
  imagePath = ""; //theFilename;
  // Store the dimensions of the picture
  imgHeight = 0;
  imgWidth  = 0;
}

Member Function Documentation

Performs color filtering from the image sample contained in the ROI rect of the image located at imagePath Thresholds the result in order ot obtain a binary image.

Returns:
binary image resulting from filtering and thersholding

Definition at line 226 of file ShapeRec_FeatureDetector.cxx.

{  
  IplImage* find_image = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
  // Reduce noise with a kernel 3x3               
  cvSmooth( find_image, find_image, CV_GAUSSIAN, 3, 3 );
  
  if ( !rect.width > 1 )
    return Mat(find_image);
  
  // Crop the image to build an histogram from the selected part
  cvSetImageROI(find_image, rect);
  IplImage* test_image = cvCreateImage(cvGetSize(find_image),
                                       find_image->depth,
                                       find_image->nChannels);
  cvCopy(find_image, test_image, NULL);
  cvResetImageROI(find_image);
  
  IplImage* test_hsv = cvCreateImage(cvGetSize(test_image),8,3);
  IplImage* h_plane = cvCreateImage( cvGetSize(test_image), 8, 1 );
  IplImage* s_plane = cvCreateImage( cvGetSize(test_image), 8, 1 );
  CvHistogram* hist;

  cvCvtColor(test_image, test_hsv, CV_BGR2HSV);
  
  cvCvtPixToPlane(test_hsv, h_plane, s_plane, 0, 0);
  IplImage* planes[] = { h_plane, s_plane };
  
  //create hist
  int hbins = 30, sbins = 32;                        // TODO think to the best values here
  int   hist_size[] = { hbins, sbins };
  float hranges[] = { 0, 180 };
  float sranges[] = { 0, 255 };
  float* ranges[] = { hranges, sranges };
  hist = cvCreateHist(2, hist_size, CV_HIST_ARRAY, ranges, 1);
  
  //calculate hue /saturation histogram
  cvCalcHist(planes, hist, 0 ,0);

//   // TEST print of the histogram for debugging
//   IplImage* hist_image = cvCreateImage(cvSize(320,300),8,3);
//   
//   //draw hist on hist_test image.
//   cvZero(hist_image);
//   float max_value = 0;
//   cvGetMinMaxHistValue(hist, 0 , &max_value, 0, 0);
//   int bin_w = hist_image->width/size_hist;
//   for(int i = 0; i < size_hist; i++ )
//   {
//     //prevent overflow
//     int val = cvRound( cvGetReal1D(hist->bins,i)*hist_image->
//     height/max_value);
//     CvScalar color = CV_RGB(200,0,0);
//     //hsv2rgb(i*180.f/size_hist);
//     cvRectangle( hist_image, cvPoint(i*bin_w,hist_image->height),
//     cvPoint((i+1)*bin_w,hist_image->height - val),
//     color, -1, 8, 0 );
//   }
//  
//    
//   cvNamedWindow("hist", 1); cvShowImage("hist",hist_image);
  
  
  //calculate back projection of hue and saturation planes of input image
  IplImage* backproject = cvCreateImage(cvGetSize(find_image), 8, 1);
  IplImage* binary_backproject = cvCreateImage(cvGetSize(find_image), 8, 1);
  IplImage* find_hsv = cvCreateImage(cvGetSize(find_image),8,3);
  IplImage* find_hplane = cvCreateImage(cvGetSize(find_image),8,1);
  IplImage* find_splane = cvCreateImage(cvGetSize(find_image),8,1);
  
  cvCvtColor(find_image, find_hsv, CV_BGR2HSV);
  cvCvtPixToPlane(find_hsv, find_hplane, find_splane, 0, 0);
  IplImage* find_planes[] = { find_hplane, find_splane };
  cvCalcBackProject(find_planes, backproject, hist);
  
  // Threshold in order to obtain binary image
  cvThreshold(backproject, binary_backproject, 1, 255, CV_THRESH_BINARY);  // NOTE it would be good to think about the best threshold to use (it's 1 for now)
  cvReleaseImage(&test_image);
  cvReleaseImage(&test_hsv);
  cvReleaseImage(&h_plane);
  cvReleaseImage(&s_plane);
  cvReleaseImage(&find_image);
  cvReleaseImage(&find_hsv);
  cvReleaseImage(&find_hplane);
  cvReleaseImage(&find_splane);
  cvReleaseImage(&backproject);
  
  return Mat(binary_backproject);
}

Here is the caller graph for this function:

void ShapeRec_FeatureDetector::_detectAndRetrieveContours ( cv::Mat  binaryImg) [private]

Performs contours detection and store them in contours.

Parameters:
binaryImg- src image to find contours of

Definition at line 211 of file ShapeRec_FeatureDetector.cxx.

{
  binaryImg = binaryImg > 1; 
  int method = CV_CHAIN_APPROX_NONE;
  findContours( binaryImg, contours, hierarchy,CV_RETR_CCOMP, method);
  // Other possible approximations CV_CHAIN_APPROX_TC89_KCOS, CV_CHAIN_APPROX_TC89_L1, CV_CHAIN_APPROX_SIMPLE cf. OpenCV documentation 
  // for precise information
}

Here is the caller graph for this function:

Computes the contours of the image located at imagePath.

Definition at line 112 of file ShapeRec_FeatureDetector.cxx.

                                                                    {
 
  // Initialising images
  Mat src, src_gray;
  Mat detected_edges;
  
  // Read image
  src = imread( imagePath.c_str() );
  if( !src.data )
    return false; 
  
  if ( detection_method == CANNY )   // The problem is that with that filter the detector detects double contours
  {   
    // Thresholds for Canny detector
    int lowThreshold = 100;
    int ratio = 3;
    int kernel_size = 3; // 3,5 or 7
    
    // Convert the image to grayscale
    if (src.channels() == 3)
      cvtColor( src, src_gray, CV_BGR2GRAY );
    else if (src.channels() == 1)
      src_gray = src;
  
    // Reduce noise with a kernel 3x3               
    blur( src_gray, detected_edges, Size(3,3) );
    // Canny detector
    Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size, /*L2gradient =*/true );      
  }
  else if ( detection_method == COLORFILTER )
  {
    if ( !rect.width > 1 )
      return false;
    detected_edges = _colorFiltering();
  }
  else if ( detection_method == RIDGE_DETECTOR )  // Method adapted for engineering drawings (e.g. watershed functionnality could be used here cf.OpenCV documentation and samples)
  {
    // TODO
    return false;
  }
  _detectAndRetrieveContours( detected_edges );
  
  return true;
  
}

Here is the call graph for this function:

Here is the caller graph for this function:

Computes the corners of the image located at imagePath.

Definition at line 71 of file ShapeRec_FeatureDetector.cxx.

                                             {
  
  // Parameters for the corner detection
  double qualityLevel = 0.2;
  double minDistance = 1;
 
  // Images to be used for detection
  IplImage *eig_img, *temp_img, *src_img_gray;
  
  // Load image
  src_img_gray = cvLoadImage (imagePath.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
  
  if ( rect.width > 1 )
  {
    // If a ROI as been set use it for detection
    cvSetImageROI( src_img_gray, rect );
  }
  
  eig_img = cvCreateImage (cvGetSize (src_img_gray), IPL_DEPTH_32F, 1);
  temp_img = cvCreateImage (cvGetSize (src_img_gray), IPL_DEPTH_32F, 1);
  corners = (CvPoint2D32f *) cvAlloc (cornerCount * sizeof (CvPoint2D32f));
  
  // image height and width
  imgHeight = src_img_gray->height;
  imgWidth  = src_img_gray->width;

  // Corner detection using cvCornerMinEigenVal 
  // (one of the methods available inOpenCV, there is also a cvConerHarris method that can be used by setting a flag in cvGoodFeaturesToTrack)
  cvGoodFeaturesToTrack (src_img_gray, eig_img, temp_img, corners, &cornerCount, /*quality-level=*/qualityLevel, /*min-distance=*/minDistance);
  cvFindCornerSubPix (src_img_gray, corners, cornerCount,
                    cvSize (3, 3), cvSize (-1, -1), cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03));

  cvReleaseImage (&eig_img);
  cvReleaseImage (&temp_img);
  cvReleaseImage (&src_img_gray);

}

Here is the caller graph for this function:

Computes the lines in the image located at imagePath.

Definition at line 161 of file ShapeRec_FeatureDetector.cxx.

                                           {
  MESSAGE("ShapeRec_FeatureDetector::ComputeLines()")
  // Initialising images
  Mat src, src_gray, detected_edges, dst;
  
  src=imread(imagePath.c_str(), 0);
  
  Canny( src, dst, 50, 200, 3 );
  HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
  return true;
  
}

Crops the image located at imagePath to the region of interest given by the user via SetROI and stores the result in /tmp.

Parameters:
theRect- Region Of Interest of the image located at imagePath

Definition at line 190 of file ShapeRec_FeatureDetector.cxx.

{
  IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
 
  cvSetImageROI(src, rect);
  IplImage* cropped_image = cvCreateImage(cvGetSize(src),
                                          src->depth,
                                          src->nChannels);
  cvCopy(src, cropped_image, NULL);
  cvResetImageROI(src);
  
  cvSaveImage ("/tmp/cropped_image.bmp", cropped_image);
  
  return "/tmp/cropped_image.bmp";
}

Here is the caller graph for this function:

Definition at line 54 of file ShapeRec_FeatureDetector.hxx.

{ return contours;    };

Here is the caller graph for this function:

std::vector<cv::Vec4i> ShapeRec_FeatureDetector::GetContoursHierarchy ( ) [inline]

Definition at line 56 of file ShapeRec_FeatureDetector.hxx.

{ return hierarchy;   };

Here is the caller graph for this function:

Definition at line 57 of file ShapeRec_FeatureDetector.hxx.

{ return cornerCount; };

Here is the caller graph for this function:

CvPoint2D32f* ShapeRec_FeatureDetector::GetCorners ( ) [inline]

Definition at line 53 of file ShapeRec_FeatureDetector.hxx.

{ return corners;     };

Here is the caller graph for this function:

Definition at line 58 of file ShapeRec_FeatureDetector.hxx.

{ return imgHeight;   };

Here is the caller graph for this function:

Definition at line 59 of file ShapeRec_FeatureDetector.hxx.

{ return imgWidth;    };

Here is the caller graph for this function:

std::vector<cv::Vec4i> ShapeRec_FeatureDetector::GetLines ( ) [inline]

Definition at line 55 of file ShapeRec_FeatureDetector.hxx.

{ return lines;       };
void ShapeRec_FeatureDetector::SetPath ( const std::string &  thePath)

Sets the path of the image file to be processed.

Parameters:
thePath- Location of the image file

Definition at line 57 of file ShapeRec_FeatureDetector.cxx.

{
  imagePath = thePath; 
  if (imagePath != "")
  {
    IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
    imgHeight = src->height;
    imgWidth = src->width; 
  }
}

Here is the caller graph for this function:

void ShapeRec_FeatureDetector::SetROI ( const QRect &  theRect)

Stores a region of interest given by user in rect.

Parameters:
theRect- Region Of Interest of the image located at imagePath

Definition at line 178 of file ShapeRec_FeatureDetector.cxx.

{
  if (!theRect.isEmpty()){
    rect = cvRect(theRect.x(),theRect.y(),theRect.width(),theRect.height());
  }
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 73 of file ShapeRec_FeatureDetector.hxx.

Definition at line 71 of file ShapeRec_FeatureDetector.hxx.

CvPoint2D32f* ShapeRec_FeatureDetector::corners [private]

Definition at line 70 of file ShapeRec_FeatureDetector.hxx.

std::vector<cv::Vec4i> ShapeRec_FeatureDetector::hierarchy [private]

Definition at line 74 of file ShapeRec_FeatureDetector.hxx.

std::string ShapeRec_FeatureDetector::imagePath [private]

Definition at line 68 of file ShapeRec_FeatureDetector.hxx.

Definition at line 76 of file ShapeRec_FeatureDetector.hxx.

Definition at line 77 of file ShapeRec_FeatureDetector.hxx.

std::vector<cv::Vec4i> ShapeRec_FeatureDetector::lines [private]

Definition at line 75 of file ShapeRec_FeatureDetector.hxx.

Definition at line 78 of file ShapeRec_FeatureDetector.hxx.


The documentation for this class was generated from the following files: