bloblabeler.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "bloblabeler.h"
25 
26 #include <stdio.h>
27 
29 {
30 }
31 
32 void
33 BlobLabeler::labelBlobs(cv::Mat& binaryImage,
34  cv::Mat& outputImage,
35  std::vector<std::vector<cv::Point>>& blobList)
36 {
37 
38  blobList.clear();
39  int width = binaryImage.cols;
40  int heigth = binaryImage.rows;
41  int blobIndex = 1;
42 
43 
44  binaryImage.convertTo(outputImage, CV_16UC1);
45 
46 
47  for (uint16_t& pixel : cv::Mat_<uint16_t>(outputImage))
48  {
49  if (pixel > 0)
50  {
51  pixel = 65535;
52  }
53  }
54 
55 
56  for (int y = 0; y < heigth; y++)
57  {
58  for (int x = 0; x < width; x++)
59  {
60  cv::Point currentPixel(x, y);
61 
62  bool isWhite = (outputImage.at<uint16_t>(currentPixel) == 65535);
63  std::vector<cv::Point> pointlist;
64  if (isWhite)
65  {
66  this->markBlob(outputImage, currentPixel, blobIndex, pointlist);
67  blobList.push_back(pointlist);
68  blobIndex++;
69  if (blobIndex >= 65535)
70  {
71  return;
72  }
73  }
74  }
75  }
76 }
77 
78 void
79 BlobLabeler::markBlob(cv::Mat& image,
80  cv::Point currentPixel,
81  uint16_t color,
82  std::vector<cv::Point>& pointList)
83 {
84 
85  int x = currentPixel.x;
86  int y = currentPixel.y;
87  int width = image.cols;
88  int heigth = image.rows;
89  image.at<uint16_t>(currentPixel) = color;
90 
91  for (int j = y - 1; j <= y + 1; j++)
92  {
93  for (int i = x - 1; i <= x + 1; i++)
94  {
95 
96 
97  bool validPoint = i >= 0 && j >= 0 && i <= width && j <= heigth;
98 
99  if (validPoint)
100  {
101  cv::Point neighbor(i, j);
102  uint16_t* neighborValue = &image.at<uint16_t>(neighbor);
103  bool isWhite = *neighborValue == 65535;
104  if (isWhite)
105  {
106  cv::Point neighbor(i, j);
107  image.at<uint16_t>(neighbor) = color;
108  //std::cout << i << " " << j << " " << image.at<uint16_t>(neighbor) <<"\n";
109  pointList.push_back(neighbor);
110  markBlob(image, neighbor, color, pointList);
111  }
112  }
113  }
114  }
115 }
BlobLabeler::BlobLabeler
BlobLabeler()
Definition: bloblabeler.cpp:28
visionx::Point
Eigen::Vector3f Point
Definition: ObjectShapeClassification.h:70
bloblabeler.h
BlobLabeler::labelBlobs
void labelBlobs(cv::Mat &binaryImage, cv::Mat &outputImage, std::vector< std::vector< cv::Point >> &blobList)
Definition: bloblabeler.cpp:33