]> SALOME platform Git repositories - modules/geom.git/blob - src/ShapeRecognition/ShapeRec_FeatureDetector.cxx
Salome HOME
b61f4bc905df5426ecab7c578b9b7840a99ca210
[modules/geom.git] / src / ShapeRecognition / ShapeRec_FeatureDetector.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : ShapeRec_FeatureDetector.cxx
24 // Author : Renaud NEDELEC, Open CASCADE S.A.S.
25
26 #include "ShapeRec_FeatureDetector.hxx"
27 #include <stdio.h>
28 #include "utilities.h"
29
30 // TODO : All the following methods but ComputeContours use the C API of OpenCV while ComputContours
31 // uses the C++ API of the library.
32 // This should be homogenized and preferably by using the C++ API (which is more recent for all the methods
33
34 // The code has to be "cleaned up" too
35
36 /*!
37   Constructor
38   \param theFilename - image to process
39 */
40 ShapeRec_FeatureDetector::ShapeRec_FeatureDetector(): 
41   corners()
42 {
43   cornerCount = 2000;
44   rect=cvRect(0,0,0,0);
45   imagePath = ""; //theFilename;
46   // Store the dimensions of the picture
47   imgHeight = 0;
48   imgWidth  = 0;
49 }
50
51 /*!
52   Sets the path of the image file to be processed
53   \param thePath - Location of the image file 
54 */
55 void ShapeRec_FeatureDetector::SetPath( const std::string& thePath )
56 {
57   imagePath = thePath; 
58   if (imagePath != "")
59   {
60     IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
61     imgHeight = src->height;
62     imgWidth = src->width; 
63   }
64 }
65
66 /*!
67   Computes the corners of the image located at imagePath
68 */
69 void ShapeRec_FeatureDetector::ComputeCorners( bool useROI, ShapeRec_Parameters* parameters )
70 {
71   ShapeRec_CornersParameters* aCornersParameters = dynamic_cast<ShapeRec_CornersParameters*>( parameters );
72   if ( !aCornersParameters ) aCornersParameters = new  ShapeRec_CornersParameters();
73
74   // Images to be used for detection
75   IplImage *eig_img, *temp_img, *src_img_gray;
76   
77   // Load image
78   src_img_gray = cvLoadImage (imagePath.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
79   
80   if ( useROI )
81   {
82     // If a ROI as been set use it for detection
83     cvSetImageROI( src_img_gray, rect );
84   }
85   
86   eig_img = cvCreateImage (cvGetSize (src_img_gray), IPL_DEPTH_32F, 1);
87   temp_img = cvCreateImage (cvGetSize (src_img_gray), IPL_DEPTH_32F, 1);
88   corners = (CvPoint2D32f *) cvAlloc (cornerCount * sizeof (CvPoint2D32f));
89   
90   // image height and width
91   imgHeight = src_img_gray->height;
92   imgWidth  = src_img_gray->width;
93
94   // Corner detection using cvCornerMinEigenVal 
95   // (one of the methods available inOpenCV, there is also a cvConerHarris method that can be used by setting a flag in cvGoodFeaturesToTrack)
96   cvGoodFeaturesToTrack (src_img_gray, eig_img, temp_img, corners, &cornerCount, aCornersParameters->qualityLevel, aCornersParameters->minDistance);
97   cvFindCornerSubPix (src_img_gray, corners, cornerCount, cvSize (aCornersParameters->kernelSize, aCornersParameters->kernelSize), cvSize (-1, -1),
98                       cvTermCriteria (aCornersParameters->typeCriteria, aCornersParameters->maxIter, aCornersParameters->epsilon));
99
100   cvReleaseImage (&eig_img);
101   cvReleaseImage (&temp_img);
102   cvReleaseImage (&src_img_gray);
103
104 }
105
106 /*!
107   Computes the contours of the image located at imagePath
108 */
109 bool ShapeRec_FeatureDetector::ComputeContours( bool useROI, ShapeRec_Parameters* parameters )
110
111   // Initialising images
112   cv::Mat src, src_gray;
113   cv::Mat detected_edges;
114   
115   // Read image
116   src = cv::imread( imagePath.c_str() );
117   if( !src.data )
118     return false; 
119   
120   if ( !useROI )   // CANNY: The problem is that with that filter the detector detects double contours
121   {   
122     // Convert the image to grayscale
123     if (src.channels() == 3)
124       cv::cvtColor( src, src_gray, CV_BGR2GRAY );
125     else if (src.channels() == 1)
126       src_gray = src;
127
128     ShapeRec_CannyParameters* aCannyParameters = dynamic_cast<ShapeRec_CannyParameters*>( parameters );
129     if ( !aCannyParameters ) aCannyParameters = new ShapeRec_CannyParameters();
130
131     // Reduce noise              
132     blur( src_gray, detected_edges, cv::Size( aCannyParameters->kernelSize, aCannyParameters->kernelSize ) );
133     // Canny detector
134     Canny( detected_edges, detected_edges, aCannyParameters->lowThreshold, aCannyParameters->lowThreshold * aCannyParameters->ratio,
135            aCannyParameters->kernelSize, aCannyParameters->L2gradient );
136   }
137   else //COLORFILTER
138   {
139     // Load the input image where we want to detect contours
140     IplImage* input_image = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
141
142     ShapeRec_ColorFilterParameters* aColorFilterParameters = dynamic_cast<ShapeRec_ColorFilterParameters*>( parameters );
143     if ( !aColorFilterParameters ) aColorFilterParameters = new ShapeRec_ColorFilterParameters();
144
145     // Reduce noise
146     cvSmooth( input_image, input_image, CV_GAUSSIAN, aColorFilterParameters->smoothSize, aColorFilterParameters->smoothSize );
147   
148     // Crop the image to the selected part only (sample_image)
149     cvSetImageROI(input_image, rect);
150     IplImage* sample_image = cvCreateImage(cvGetSize(input_image),
151                                            input_image->depth,
152                                            input_image->nChannels);
153     cvCopy(input_image, sample_image, NULL);
154     cvResetImageROI(input_image);
155   
156     IplImage* sample_hsv = cvCreateImage( cvGetSize(sample_image),8,3 );
157     IplImage* sample_h_plane  = cvCreateImage( cvGetSize(sample_image), 8, 1 );
158     IplImage* sample_s_plane = cvCreateImage( cvGetSize(sample_image), 8, 1 );
159     CvHistogram* sample_hist;
160
161     cvCvtColor(sample_image, sample_hsv, CV_BGR2HSV);
162   
163     cvCvtPixToPlane(sample_hsv, sample_h_plane, sample_s_plane, 0, 0);
164     IplImage* sample_planes[] = { sample_h_plane, sample_s_plane };
165   
166     // Create the hue / saturation histogram of the SAMPLE image.
167     // This histogramm will be representative of what is the zone
168     // we want to find the frontier of. Indeed, the sample image is meant to 
169     // be representative of this zone
170     float hranges[] = { 0, 180 };
171     float sranges[] = { 0, 256 };
172     float* ranges[] = { hranges, sranges };
173     sample_hist = cvCreateHist( 2, aColorFilterParameters->histSize, aColorFilterParameters->histType, ranges );
174   
175     //calculate hue /saturation histogram
176     cvCalcHist(sample_planes, sample_hist, 0 ,0);
177
178 //   // TEST print of the histogram for debugging
179 //   IplImage* hist_image = cvCreateImage(cvSize(320,300),8,3);
180 //   
181 //   //draw hist on hist_test image.
182 //   cvZero(hist_image);
183 //   float max_value = 0;
184 //   cvGetMinMaxHistValue(hist, 0 , &max_value, 0, 0);
185 //   int bin_w = hist_image->width/size_hist;
186 //   for(int i = 0; i < size_hist; i++ )
187 //   {
188 //     //prevent overflow
189 //     int val = cvRound( cvGetReal1D(hist->bins,i)*hist_image->
190 //     height/max_value);
191 //     CvScalar color = CV_RGB(200,0,0);
192 //     //hsv2rgb(i*180.f/size_hist);
193 //     cvRectangle( hist_image, cvPoint(i*bin_w,hist_image->height),
194 //     cvPoint((i+1)*bin_w,hist_image->height - val),
195 //     color, -1, 8, 0 );
196 //   }
197 //  
198 //    
199 //   cvNamedWindow("hist", 1); cvShowImage("hist",hist_image);
200   
201   
202     // Calculate the back projection of hue and saturation planes of the INPUT image
203     // by mean of the histogram of the SAMPLE image.
204     //
205     // The pixels which (h,s) coordinates correspond to high values in the histogram
206     // will have high values in the grey image result. It means that a pixel of the INPUT image 
207     // which is more probably in the zone represented by the SAMPLE image, will be whiter 
208     // in the back projection.
209     IplImage* backproject = cvCreateImage(cvGetSize(input_image), 8, 1);
210     IplImage* binary_backproject = cvCreateImage(cvGetSize(input_image), 8, 1);
211     IplImage* input_hsv = cvCreateImage(cvGetSize(input_image),8,3);
212     IplImage* input_hplane = cvCreateImage(cvGetSize(input_image),8,1);
213     IplImage* input_splane = cvCreateImage(cvGetSize(input_image),8,1);
214   
215     // Get hue and saturation planes of the INPUT image
216     cvCvtColor(input_image, input_hsv, CV_BGR2HSV);
217     cvCvtPixToPlane(input_hsv, input_hplane, input_splane, 0, 0);
218     IplImage* input_planes[] = { input_hplane, input_splane };
219     
220     // Compute the back projection
221     cvCalcBackProject(input_planes, backproject, sample_hist);
222   
223     // Threshold in order to obtain a binary image
224     cvThreshold(backproject, binary_backproject, aColorFilterParameters->threshold, aColorFilterParameters->maxThreshold, CV_THRESH_BINARY);
225     cvReleaseImage(&sample_image);
226     cvReleaseImage(&sample_hsv);
227     cvReleaseImage(&sample_h_plane);
228     cvReleaseImage(&sample_s_plane);
229     cvReleaseImage(&input_image);
230     cvReleaseImage(&input_image);
231     cvReleaseImage(&input_hsv);
232     cvReleaseImage(&input_hplane);
233     cvReleaseImage(&input_splane);
234     cvReleaseImage(&backproject);
235   
236     detected_edges = cv::Mat(binary_backproject);
237   }
238   // else if ( detection_method == RIDGE_DETECTOR )  // Method adapted for engineering drawings (e.g. watershed functionnality could be used here cf.OpenCV documentation and samples)
239   // {
240   //   // TODO
241   //   return false;
242   // }
243
244   //  _detectAndRetrieveContours( detected_edges, parameters->findContoursMethod );
245   detected_edges = detected_edges > 1;
246   findContours( detected_edges, contours, hierarchy, CV_RETR_CCOMP, parameters->findContoursMethod);
247
248   return true;
249   
250 }
251
252 /*!
253   Computes the lines in the image located at imagePath
254 */
255 bool ShapeRec_FeatureDetector::ComputeLines(){
256   MESSAGE("ShapeRec_FeatureDetector::ComputeLines()")
257   // Initialising images
258   cv::Mat src, src_gray, detected_edges, dst;
259   
260   src=cv::imread(imagePath.c_str(), 0);
261   
262   Canny( src, dst, 50, 200, 3 );
263   HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
264   return true;
265   
266 }
267
268 /*!
269   Stores a region of interest given by user in rect
270   \param theRect - Region Of Interest of the image located at imagePath 
271 */
272 void ShapeRec_FeatureDetector::SetROI( const QRect& theRect )
273 {
274   if (!theRect.isEmpty()){
275     rect = cvRect(theRect.x(),theRect.y(),theRect.width(),theRect.height());
276   }
277 }
278
279 /*!
280   Crops the image located at imagePath to the region of interest given by the user via SetROI
281   and stores the result in /tmp
282   \param theRect - Region Of Interest of the image located at imagePath 
283 */
284 std::string ShapeRec_FeatureDetector::CroppImage()
285 {
286   IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
287  
288   cvSetImageROI(src, rect);
289   IplImage* cropped_image = cvCreateImage(cvGetSize(src),
290                                           src->depth,
291                                           src->nChannels);
292   cvCopy(src, cropped_image, NULL);
293   cvResetImageROI(src);
294   
295   cvSaveImage ("/tmp/cropped_image.bmp", cropped_image);
296   
297   return "/tmp/cropped_image.bmp";
298 }
299
300 /*!
301   \class ShapeRec_CornersParameters
302   \brief Parameters for the corners detection 
303 */
304 ShapeRec_CornersParameters::ShapeRec_CornersParameters()
305 {
306   qualityLevel = 0.2;
307   minDistance = 1;
308   typeCriteria = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
309   maxIter = 20;
310   epsilon = 0.03;
311 }
312 ShapeRec_CornersParameters::~ShapeRec_CornersParameters()
313 {
314 }
315
316 /*!
317   \class ShapeRec_Parameters
318   \brief Parameters for the contour/corners detection 
319 */
320 ShapeRec_Parameters::ShapeRec_Parameters()
321 {
322   kernelSize = 3;
323   findContoursMethod = CV_CHAIN_APPROX_NONE;
324 }
325 ShapeRec_Parameters::~ShapeRec_Parameters()
326 {
327 }
328
329 /*!
330   \class ShapeRec_CannyParameters
331   \brief Parameters for the contour detection 
332 */
333 ShapeRec_CannyParameters::ShapeRec_CannyParameters()
334 {
335   lowThreshold = 100; // is used for edge linking.
336   ratio = 3;          // lowThreshold*ratio is used to find initial segments of strong edges
337   L2gradient = true;  // norm L2 or L1
338 }
339
340 ShapeRec_CannyParameters::~ShapeRec_CannyParameters()
341 {
342 }
343
344 /*!
345   \class ShapeRec_ColorFilterParameters
346   \brief Parameters for the contour detection 
347 */
348 ShapeRec_ColorFilterParameters::ShapeRec_ColorFilterParameters()
349 {
350   smoothSize = 3;           // The parameter of the smoothing operation, the aperture width. Must be a positive odd number
351   histSize = new int[2];    // array of the histogram dimension sizes
352   histSize[0] = 30;         // hbins
353   histSize[1] = 32;         // sbins
354   histType = CV_HIST_ARRAY; // histogram representation format
355   threshold = 128;          // threshold value
356   maxThreshold = 255;       // maximum value to use with the THRESH_BINARY thresholding types
357 }
358
359 ShapeRec_ColorFilterParameters::~ShapeRec_ColorFilterParameters()
360 {
361 }