-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectDetector.cpp
More file actions
61 lines (54 loc) · 2.13 KB
/
ObjectDetector.cpp
File metadata and controls
61 lines (54 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* This file is part of the rgbdemo project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Nicolas Burrus <nicolas@burrus.name>, (C) 2010, 2011
*/
#include "ObjectDetector.h"
#include <ntk/ntk.h>
using namespace cv;
using namespace ntk;
void ObjectDetector :: detect(const cv::Mat1f& distance_image,
cv::Mat1b& mask_image,
std::list<cv::Rect>& rects)
{
if (mask_image.size() != distance_image.size())
mask_image = cv::Mat1b(distance_image.size());
for (int r = 0; r < distance_image.rows; ++r)
for (int c = 0; c < distance_image.cols; ++c)
{
if (distance_image(r,c) >= m_min_threshold && distance_image(r,c) <= m_max_threshold)
mask_image(r,c) = 255;
else
mask_image(r,c) = 0;
}
cv::morphologyEx(mask_image, mask_image,
cv::MORPH_OPEN,
getStructuringElement(cv::MORPH_RECT,
cv::Size(3,3)));
cv::morphologyEx(mask_image, mask_image,
cv::MORPH_CLOSE,
getStructuringElement(cv::MORPH_RECT,
cv::Size(3,3)));
std::vector< std::vector<cv::Point> > contours;
cv::Mat1b contour_image = mask_image.clone();
cv::findContours(contour_image, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
for (int i = 0; i < contours.size(); ++i)
{
cv::Rect rect = cv::boundingRect(cv::Mat(contours[i]));
if (rect.area() > 300)
rects.push_back(rect);
}
}