Apollo 11.0
自动驾驶开放平台
segment_image.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17/*
18Copyright (C) 2006 Pedro Felzenszwalb
19This program is free software; you can redistribute it and/or modify
20it under the terms of the GNU General Public License as published by
21the Free Software Foundation; either version 2 of the License, or
22(at your option) any later version.
23This program is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26GNU General Public License for more details.
27You should have received a copy of the GNU General Public License
28along with this program; if not, write to the Free Software
29Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30*/
31
32#pragma once
33
34#include <cstdlib>
35
40
41namespace apollo {
42namespace perception {
43namespace lidar {
44
55inline float diff(Image<float> *I, int x1, int y1, int x2, int y2) {
56 return std::fabs(imRef(I, x1, y1) - imRef(I, x2, y2));
57}
58
69Image<int> *segment_image(Image<float> *im, float sigma, float c, int min_size, int *num_ccs) {
70 int width = im->width();
71 int height = im->height();
72 // smooth each color channel
73 Image<float> *smooth_r = smooth(im, sigma);
74 // build graph
75 edge *edges = new edge[width * height * 4];
76 int num = 0;
77 for (int y = 0; y < height; y++) {
78 for (int x = 0; x < width; x++) {
79 if (x < width - 1) {
80 edges[num].a = y * width + x;
81 edges[num].b = y * width + (x + 1);
82 edges[num].w = diff(smooth_r, x, y, x + 1, y);
83 num++;
84 }
85 if (y < height - 1) {
86 edges[num].a = y * width + x;
87 edges[num].b = (y + 1) * width + x;
88 edges[num].w = diff(smooth_r, x, y, x, y + 1);
89 num++;
90 }
91 if ((x < width - 1) && (y < height - 1)) {
92 edges[num].a = y * width + x;
93 edges[num].b = (y + 1) * width + (x + 1);
94 edges[num].w = diff(smooth_r, x, y, x + 1, y + 1);
95 num++;
96 }
97 if ((x < width - 1) && (y > 0)) {
98 edges[num].a = y * width + x;
99 edges[num].b = (y - 1) * width + (x + 1);
100 edges[num].w = diff(smooth_r, x, y, x + 1, y - 1);
101 num++;
102 }
103 }
104 }
105 delete smooth_r;
106 // segment
107 Universe *u = segment_graph(width * height, num, edges, c);
108 // post process small components
109 for (int i = 0; i < num; i++) {
110 int a = u->find(edges[i].a);
111 int b = u->find(edges[i].b);
112 if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
113 u->join(a, b);
114 }
115 delete[] edges;
116 *num_ccs = u->num_sets();
117 Image<int> *output = new Image<int>(width, height);
118 for (int y = 0; y < height; y++) {
119 for (int x = 0; x < width; x++) {
120 int comp = u->find(y * width + x);
121 imRef(output, x, y) = comp;
122 }
123 }
124 delete u;
125 return output;
126}
127} // namespace lidar
128} // namespace perception
129} // namespace apollo
int height() const
Get the height of an image
Definition image.h:73
int width() const
Get the width of an image
Definition image.h:64
int find(int x)
Find parent of x
int num_sets() const
Get set number
void join(int x, int y)
Join set x and set y
int size(int x) const
Get size of set x
#define imRef(im, x, y)
Definition image.h:88
Universe * segment_graph(int num_vertices, int num_edges, edge *edges, float c)
Segment a graph
Image< float > * smooth(Image< float > *src, float sigma)
Convolve image with gaussian filter
Definition filter.h:86
Image< int > * segment_image(Image< float > *im, float sigma, float c, int min_size, int *num_ccs)
Segment an image
float diff(Image< float > *I, int x1, int y1, int x2, int y2)
Dissimilarity measure between pixels
class register implement
Definition arena_queue.h:37