JeVoisBase  1.10
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
BlobDetector.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2018 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
19 
22 #include <opencv2/imgproc/imgproc.hpp>
23 
24 // ##############################################################################################################
26 { }
27 
28 // ##############################################################################################################
29 std::vector<std::vector<cv::Point> > BlobDetector::detect(cv::Mat const & imghsv)
30 {
31  std::vector<std::vector<cv::Point> > retcontours;
32 
33  // Threshold the HSV image to only keep pixels within the desired HSV range:
34  cv::Mat imgth;
35  cv::inRange(imghsv, cv::Scalar(hrange::get().min(), srange::get().min(), vrange::get().min()),
36  cv::Scalar(hrange::get().max(), srange::get().max(), vrange::get().max()), imgth);
37 
38  // Apply morphological operations to cleanup the image noise:
39  cv::Mat erodeElement = getStructuringElement(cv::MORPH_RECT, cv::Size(erodesize::get(), erodesize::get()));
40  cv::erode(imgth, imgth, erodeElement);
41 
42  cv::Mat dilateElement = getStructuringElement(cv::MORPH_RECT, cv::Size(dilatesize::get(), dilatesize::get()));
43  cv::dilate(imgth, imgth, dilateElement);
44 
45  // Detect objects by finding contours:
46  std::vector<std::vector<cv::Point> > contours; std::vector<cv::Vec4i> hierarchy;
47  cv::findContours(imgth, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
48 
49  // Identify the "good" objects:
50  if (hierarchy.size() > 0 && hierarchy.size() <= maxnumobj::get())
51  for (int index = 0; index >= 0; index = hierarchy[index][0])
52  {
53  // Let's examine this contour:
54  std::vector<cv::Point> const & c = contours[index];
55 
56  cv::Moments moment = cv::moments(c);
57  double const area = moment.m00;
58 
59  // If this contour fits the bill, we will return it:
60  if (objectarea::get().contains(int(area + 0.4999))) retcontours.push_back(c);
61  }
62 
63  return retcontours;
64 }
virtual ~BlobDetector()
Virtual destructor for safe inheritance.
Definition: BlobDetector.C:25
double area(const std::vector< Point2D< T > > &polygon, const bool getsigned=false)
What is the area of a polygon?
Definition: Point2D.H:422
std::vector< std::vector< cv::Point > > detect(cv::Mat const &imghsv)
Detect blobs, each is represented as a contour (vector of (x,y) coordinates)
Definition: BlobDetector.C:29