]> SALOME platform Git repositories - modules/geom.git/blobdiff - src/ShapeRecognition/ShapeRec_FeatureDetector.cxx
Salome HOME
0021672: [CEA 565] Dump Study from script
[modules/geom.git] / src / ShapeRecognition / ShapeRec_FeatureDetector.cxx
index ca6caa10d51538b8ec22d42b22be31ae93754671..b405135e571c4dd0f7707b08c84a0bc840497508 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -18,6 +18,7 @@
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 // File   : ShapeRec_FeatureDetector.cxx
 // Author : Renaud NEDELEC, Open CASCADE S.A.S.
@@ -38,16 +39,30 @@ using namespace cv;
   Constructor
   \param theFilename - image to process
 */
-ShapeRec_FeatureDetector::ShapeRec_FeatureDetector(const QString& theFilename): 
+ShapeRec_FeatureDetector::ShapeRec_FeatureDetector(): 
   corners()
 {
   cornerCount = 2000;
   rect=cvRect(0,0,0,0);
-  imagePath = theFilename.toStdString();
+  imagePath = ""; //theFilename;
   // Store the dimensions of the picture
-  IplImage* bg_img = cvLoadImage (imagePath.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
-  imgHeight = bg_img->height;
-  imgWidth  = bg_img->width; 
+  imgHeight = 0;
+  imgWidth  = 0;
+}
+
+/*!
+  Sets the path of the image file to be processed
+  \param thePath - Location of the image file 
+*/
+void ShapeRec_FeatureDetector::SetPath( const std::string& thePath )
+{
+  imagePath = thePath; 
+  if (imagePath != "")
+  {
+    IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
+    imgHeight = src->height;
+    imgWidth = src->width; 
+  }
 }
 
 /*!
@@ -167,15 +182,37 @@ void ShapeRec_FeatureDetector::SetROI( const QRect& theRect )
   }
 }
 
+/*!
+  Crops the image located at imagePath to the region of interest given by the user via SetROI
+  and stores the result in /tmp
+  \param theRect - Region Of Interest of the image located at imagePath 
+*/
+std::string ShapeRec_FeatureDetector::CroppImage()
+{
+  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";
+}
+
+
 /*!
   Performs contours detection and store them in contours 
-  \param src - src image to find contours of 
+  \param binaryImg - src image to find contours of 
 */
-void ShapeRec_FeatureDetector::_detectAndRetrieveContours( Mat src )
+void ShapeRec_FeatureDetector::_detectAndRetrieveContours( Mat binaryImg )
 {
-  src = src > 1; 
+  binaryImg = binaryImg > 1; 
   int method = CV_CHAIN_APPROX_NONE;
-  findContours( src, contours, hierarchy,CV_RETR_CCOMP, method);
+  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
 }
@@ -198,26 +235,31 @@ Mat ShapeRec_FeatureDetector::_colorFiltering()
   // 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);
+                                       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* test_hue = cvCreateImage(cvGetSize(test_image),8,1);
+  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, test_hue, 0, 0, 0);
+  
+  cvCvtPixToPlane(test_hsv, h_plane, s_plane, 0, 0);
+  IplImage* planes[] = { h_plane, s_plane };
   
   //create hist
-  int size_hist = 10;
-  float hranges[] = {0, 180};
-  float* ranges = hranges;
-  hist = cvCreateHist(1, &size_hist, CV_HIST_ARRAY, &ranges, 1);
+  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` histogram
-  cvCalcHist(&test_hue, hist, 0 ,0);
+  //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);
@@ -243,21 +285,28 @@ Mat ShapeRec_FeatureDetector::_colorFiltering()
 //   cvNamedWindow("hist", 1); cvShowImage("hist",hist_image);
   
   
-  //calculate back projection of hue plane of input 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_hue = cvCreateImage(cvGetSize(find_image),8,1);
+  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_hue, 0, 0, 0);
-  cvCalcBackProject(&find_hue, backproject, hist);
+  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(&test_hue);
+  cvReleaseImage(&h_plane);
+  cvReleaseImage(&s_plane);
+  cvReleaseImage(&find_image);
+  cvReleaseImage(&find_hsv);
+  cvReleaseImage(&find_hplane);
+  cvReleaseImage(&find_splane);
   cvReleaseImage(&backproject);
   
   return Mat(binary_backproject);