1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : ShapeRec_FeatureDetector.cxx
24 // Author : Renaud NEDELEC, Open CASCADE S.A.S.
26 #include "ShapeRec_FeatureDetector.hxx"
28 #include "utilities.h"
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
34 // The code has to be "cleaned up" too
38 \param theFilename - image to process
40 ShapeRec_FeatureDetector::ShapeRec_FeatureDetector():
45 imagePath = ""; //theFilename;
46 // Store the dimensions of the picture
52 Sets the path of the image file to be processed
53 \param thePath - Location of the image file
55 void ShapeRec_FeatureDetector::SetPath( const std::string& thePath )
60 IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
61 imgHeight = src->height;
62 imgWidth = src->width;
67 Computes the corners of the image located at imagePath
69 void ShapeRec_FeatureDetector::ComputeCorners( bool useROI, ShapeRec_Parameters* parameters )
71 ShapeRec_CornersParameters* aCornersParameters = dynamic_cast<ShapeRec_CornersParameters*>( parameters );
72 if ( !aCornersParameters ) aCornersParameters = new ShapeRec_CornersParameters();
74 // Images to be used for detection
75 IplImage *eig_img, *temp_img, *src_img_gray;
78 src_img_gray = cvLoadImage (imagePath.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
82 // If a ROI as been set use it for detection
83 cvSetImageROI( src_img_gray, rect );
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));
90 // image height and width
91 imgHeight = src_img_gray->height;
92 imgWidth = src_img_gray->width;
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));
100 cvReleaseImage (&eig_img);
101 cvReleaseImage (&temp_img);
102 cvReleaseImage (&src_img_gray);
107 Computes the contours of the image located at imagePath
109 bool ShapeRec_FeatureDetector::ComputeContours( bool useROI, ShapeRec_Parameters* parameters )
111 // Initialising images
112 cv::Mat src, src_gray;
113 cv::Mat detected_edges;
116 src = cv::imread( imagePath.c_str() );
120 if ( !useROI ) // CANNY: The problem is that with that filter the detector detects double contours
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)
128 ShapeRec_CannyParameters* aCannyParameters = dynamic_cast<ShapeRec_CannyParameters*>( parameters );
129 if ( !aCannyParameters ) aCannyParameters = new ShapeRec_CannyParameters();
132 blur( src_gray, detected_edges, cv::Size( aCannyParameters->kernelSize, aCannyParameters->kernelSize ) );
134 Canny( detected_edges, detected_edges, aCannyParameters->lowThreshold, aCannyParameters->lowThreshold * aCannyParameters->ratio,
135 aCannyParameters->kernelSize, aCannyParameters->L2gradient );
139 // Load the input image where we want to detect contours
140 IplImage* input_image = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
142 ShapeRec_ColorFilterParameters* aColorFilterParameters = dynamic_cast<ShapeRec_ColorFilterParameters*>( parameters );
143 if ( !aColorFilterParameters ) aColorFilterParameters = new ShapeRec_ColorFilterParameters();
146 cvSmooth( input_image, input_image, CV_GAUSSIAN, aColorFilterParameters->smoothSize, aColorFilterParameters->smoothSize );
148 // Crop the image to the selected part only (sample_image)
149 cvSetImageROI(input_image, rect);
150 IplImage* sample_image = cvCreateImage(cvGetSize(input_image),
152 input_image->nChannels);
153 cvCopy(input_image, sample_image, NULL);
154 cvResetImageROI(input_image);
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;
161 cvCvtColor(sample_image, sample_hsv, CV_BGR2HSV);
163 cvCvtPixToPlane(sample_hsv, sample_h_plane, sample_s_plane, 0, 0);
164 IplImage* sample_planes[] = { sample_h_plane, sample_s_plane };
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 );
175 //calculate hue /saturation histogram
176 cvCalcHist(sample_planes, sample_hist, 0 ,0);
178 // // TEST print of the histogram for debugging
179 // IplImage* hist_image = cvCreateImage(cvSize(320,300),8,3);
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++ )
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 );
199 // cvNamedWindow("hist", 1); cvShowImage("hist",hist_image);
202 // Calculate the back projection of hue and saturation planes of the INPUT image
203 // by mean of the histogram of the SAMPLE image.
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);
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 };
220 // Compute the back projection
221 cvCalcBackProject(input_planes, backproject, sample_hist);
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);
236 detected_edges = cv::Mat(binary_backproject);
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)
244 // _detectAndRetrieveContours( detected_edges, parameters->findContoursMethod );
245 detected_edges = detected_edges > 1;
246 findContours( detected_edges, contours, hierarchy, CV_RETR_CCOMP, parameters->findContoursMethod);
253 Computes the lines in the image located at imagePath
255 bool ShapeRec_FeatureDetector::ComputeLines(){
256 MESSAGE("ShapeRec_FeatureDetector::ComputeLines()")
257 // Initialising images
258 cv::Mat src, src_gray, detected_edges, dst;
260 src=cv::imread(imagePath.c_str(), 0);
262 Canny( src, dst, 50, 200, 3 );
263 HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
269 Stores a region of interest given by user in rect
270 \param theRect - Region Of Interest of the image located at imagePath
272 void ShapeRec_FeatureDetector::SetROI( const QRect& theRect )
274 if (!theRect.isEmpty()){
275 rect = cvRect(theRect.x(),theRect.y(),theRect.width(),theRect.height());
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
284 std::string ShapeRec_FeatureDetector::CroppImage()
286 IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR);
288 cvSetImageROI(src, rect);
289 IplImage* cropped_image = cvCreateImage(cvGetSize(src),
292 cvCopy(src, cropped_image, NULL);
293 cvResetImageROI(src);
295 cvSaveImage ("/tmp/cropped_image.bmp", cropped_image);
297 return "/tmp/cropped_image.bmp";
301 \class ShapeRec_CornersParameters
302 \brief Parameters for the corners detection
304 ShapeRec_CornersParameters::ShapeRec_CornersParameters()
308 typeCriteria = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
312 ShapeRec_CornersParameters::~ShapeRec_CornersParameters()
317 \class ShapeRec_Parameters
318 \brief Parameters for the contour/corners detection
320 ShapeRec_Parameters::ShapeRec_Parameters()
323 findContoursMethod = CV_CHAIN_APPROX_NONE;
325 ShapeRec_Parameters::~ShapeRec_Parameters()
330 \class ShapeRec_CannyParameters
331 \brief Parameters for the contour detection
333 ShapeRec_CannyParameters::ShapeRec_CannyParameters()
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
340 ShapeRec_CannyParameters::~ShapeRec_CannyParameters()
345 \class ShapeRec_ColorFilterParameters
346 \brief Parameters for the contour detection
348 ShapeRec_ColorFilterParameters::ShapeRec_ColorFilterParameters()
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
359 ShapeRec_ColorFilterParameters::~ShapeRec_ColorFilterParameters()