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 #include <stdio.h>
27 {
28 
29 
30 }
31 
32 void BlobLabeler::labelBlobs(cv::Mat& binaryImage, cv::Mat& outputImage, std::vector<std::vector<cv::Point> >& blobList)
33 {
34 
35  blobList.clear();
36  int width = binaryImage.cols;
37  int heigth = binaryImage.rows;
38  int blobIndex = 1;
39 
40 
41  binaryImage.convertTo(outputImage, CV_16UC1);
42 
43 
44  for (uint16_t& pixel : cv::Mat_<uint16_t>(outputImage))
45  {
46  if (pixel > 0)
47  {
48  pixel = 65535;
49  }
50  }
51 
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 }
79 
80 void BlobLabeler::markBlob(cv::Mat& image, cv::Point currentPixel, uint16_t color, std::vector<cv::Point>& pointList)
81 {
82 
83  int x = currentPixel.x;
84  int y = currentPixel.y;
85  int width = image.cols;
86  int heigth = image.rows;
87  image.at<uint16_t>(currentPixel) = color;
88 
89  for (int j = y - 1; j <= y + 1; j++)
90  {
91  for (int i = x - 1; i <= x + 1; i++)
92  {
93 
94 
95  bool validPoint = i >= 0 && j >= 0 && i <= width && j <= heigth;
96 
97  if (validPoint)
98  {
99  cv::Point neighbor(i, j);
100  uint16_t* neighborValue = &image.at<uint16_t>(neighbor);
101  bool isWhite = *neighborValue == 65535;
102  if (isWhite)
103  {
104  cv::Point neighbor(i, j);
105  image.at<uint16_t>(neighbor) = color;
106  //std::cout << i << " " << j << " " << image.at<uint16_t>(neighbor) <<"\n";
107  pointList.push_back(neighbor);
108  markBlob(image, neighbor, color, pointList);
109  }
110 
111  }
112 
113  }
114  }
115 }
BlobLabeler::BlobLabeler
BlobLabeler()
Definition: bloblabeler.cpp:26
visionx::Point
Eigen::Vector3f Point
Definition: ObjectShapeClassification.h:69
bloblabeler.h
BlobLabeler::labelBlobs
void labelBlobs(cv::Mat &binaryImage, cv::Mat &outputImage, std::vector< std::vector< cv::Point > > &blobList)
Definition: bloblabeler.cpp:32