/usr/include/gamera/plugins/contour.hpp is in python-gamera-dev 3.3.3-2+deb7u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | /*
*
* Copyright (C) 2001-2005 Ichiro Fujinaga, Michael Droettboom, Karl MacMillan
* 2010 Oliver Christen, Christoph Dalitz
* 2011 Andreas Leuschner, Christoph Dalitz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef mgd10222004_contours
#define mgd10222004_contours
#include "gamera.hpp"
namespace Gamera {
template<class T>
FloatVector* contour_top(const T& m) {
FloatVector* output = new FloatVector(m.ncols());
try {
for (size_t c = 0; c != m.ncols(); ++c) {
size_t r = 0;
for (; r != m.nrows(); ++r)
if (is_black(m.get(Point(c, r))))
break;
double result;
if (r >= m.nrows())
result = std::numeric_limits<double>::infinity();
else
result = (double)r;
(*output)[c] = result;
}
} catch (std::exception e) {
delete output;
throw;
}
return output;
}
template<class T>
FloatVector* contour_bottom(const T& m) {
FloatVector* output = new FloatVector(m.ncols());
try {
for (size_t c = 0; c != m.ncols(); ++c) {
long r = m.nrows() - 1;
for (; r >= 0; --r)
if (is_black(m.get(Point(c, r))))
break;
double result;
if (r < 0)
result = std::numeric_limits<double>::infinity();
else
result = (double)(m.nrows() - r);
(*output)[c] = result;
}
} catch (std::exception e) {
delete output;
throw;
}
return output;
}
template<class T>
FloatVector* contour_left(const T& m) {
FloatVector* output = new FloatVector(m.nrows());
try {
for (size_t r = 0; r != m.nrows(); ++r) {
size_t c = 0;
for (; c != m.ncols(); ++c)
if (is_black(m.get(Point(c, r))))
break;
double result;
if (c >= m.ncols())
result = std::numeric_limits<double>::infinity();
else
result = (double)c;
(*output)[r] = result;
}
} catch (std::exception e) {
delete output;
throw;
}
return output;
}
template<class T>
FloatVector* contour_right(const T& m) {
FloatVector* output = new FloatVector(m.nrows());
try {
for (size_t r = 0; r != m.nrows(); ++r) {
long c = m.ncols() - 1;
for (; c >= 0; --c)
if (is_black(m.get(Point(c, r))))
break;
double result;
if (c < 0)
result = std::numeric_limits<double>::infinity();
else
result = (double)(m.ncols() - c);
(*output)[r] = result;
}
} catch (std::exception e) {
delete output;
throw;
}
return output;
}
// etxraction of sample points from the contour
// author: Oliver Christen
template<class T>
PointVector * contour_samplepoints(const T& cc, int percentage) {
PointVector *output = new PointVector();
PointVector *contour_points = new PointVector();
PointVector::iterator found;
FloatVector *top = contour_top(cc);
FloatVector *right = contour_right(cc);
FloatVector *bottom = contour_bottom(cc);
FloatVector *left = contour_left(cc);
FloatVector::iterator it;
int x, y, i;
float d;
unsigned int top_d = std::numeric_limits<unsigned int>::max() ;
unsigned int top_max_x = 0;
unsigned int top_max_y = 0;
unsigned int right_d = std::numeric_limits<unsigned int>::max();
unsigned int right_max_x = 0;
unsigned int right_max_y = 0;
unsigned int bottom_d = std::numeric_limits<unsigned int>::max();
unsigned int bottom_max_x = 0;
unsigned int bottom_max_y = 0;
unsigned int left_d = std::numeric_limits<unsigned int>::max();
unsigned int left_max_x = 0;
unsigned int left_max_y = 0;
// top
i = 0;for(it = top->begin() ; it != top->end() ; it++, i++) {
if( *it == std::numeric_limits<double>::infinity() ) {
continue;
}
d = *it;
x = cc.offset_x() + i;
y = cc.offset_y() + d;
if( d < top_d) {
top_d = d;
top_max_x = x;
top_max_y = y;
}
found = find(contour_points->begin(), contour_points->end(), Point(x,y));
if(found == contour_points->end()) {
contour_points->push_back( Point(x,y) );
}
}
// right
i = 0;for(it = right->begin() ; it != right->end() ; it++, i++) {
if( *it == std::numeric_limits<double>::infinity() ) {
continue;
}
d = *it;
x = cc.offset_x() + cc.ncols() - d;
y = cc.offset_y() + i;
if( d < right_d) {
right_d = d;
right_max_x = x;
right_max_y = y;
}
found = find(contour_points->begin(), contour_points->end(), Point(x,y));
if(found == contour_points->end()) {
contour_points->push_back( Point(x,y) );
}
}
// bottom
i = 0;for(it = bottom->begin() ; it != bottom->end() ; it++, i++) {
if( *it == std::numeric_limits<double>::infinity() ) {
continue;
}
d = *it;
x = cc.offset_x() + i;
y = cc.offset_y() + cc.nrows() - d;
if( d <= bottom_d) {
bottom_d = d;
bottom_max_x = x;
bottom_max_y = y;
}
found = find(contour_points->begin(), contour_points->end(), Point(x,y));
if(found == contour_points->end()) {
contour_points->push_back( Point(x,y) );
}
}
// left
i = 0;for(it = left->begin() ; it != left->end() ; it++, i++) {
if( *it == std::numeric_limits<double>::infinity() ) {
continue;
}
d = *it;
x = cc.offset_x() + d;
y = cc.offset_y() + i;
if( d <= left_d) {
left_d = d;
left_max_x = x;
left_max_y = y;
}
found = find(contour_points->begin(), contour_points->end(), Point(x,y));
if(found == contour_points->end()) {
contour_points->push_back( Point(x,y) );
}
}
// add only every 100/percentage-th point
double delta = 100.0/percentage;
double step = 0.0;
unsigned int offset = 0; // to avoid overflow and rounding errors
unsigned int ii = 0;
while (ii < contour_points->size()) {
output->push_back( (*contour_points)[ii] );
step += delta;
if (step > 100.0) {
step -= 100.0;
offset += 100;
}
ii = offset + (unsigned int)step;
}
// add the four outer extreme points ...
// ... top
if (top_d != std::numeric_limits<unsigned int>::max()) {
found = find(output->begin(), output->end(), Point(top_max_x, top_max_y));
if(found == output->end()) {
output->push_back( Point(top_max_x, top_max_y) );
}
}
// ... right
if (right_d != std::numeric_limits<unsigned int>::max()) {
found = find(output->begin(), output->end(), Point(right_max_x, right_max_y));
if(found == output->end()) {
output->push_back( Point(right_max_x, right_max_y) );
}
}
// ... bottom
if (bottom_d != std::numeric_limits<unsigned int>::max()) {
found = find(output->begin(), output->end(), Point(bottom_max_x, bottom_max_y));
if(found == output->end()) {
output->push_back( Point(bottom_max_x, bottom_max_y) );
}
}
// ... left
if (left_d != std::numeric_limits<unsigned int>::max()) {
found = find(output->begin(), output->end(), Point(left_max_x, left_max_y));
if(found == output->end()) {
output->push_back( Point(left_max_x, left_max_y) );
}
}
delete top;
delete right;
delete bottom;
delete left;
delete contour_points;
return output;
}
// contour extraction with Pavlidis' algorithm
// author: Andreas Leuschner
template<class T>
PointVector* contour_pavlidis(T &m) {
PointVector* v_contour = new PointVector();
// neighbor mask:
// 5 6 7
// 4 P 0
// 3 2 1
int mask[8][2];
// X Y
mask[0][0] = 1; mask[0][1] = 0;
mask[1][0] = 1; mask[1][1] = -1;
mask[2][0] = 0; mask[2][1] = -1;
mask[3][0] = -1; mask[3][1] = -1;
mask[4][0] = -1; mask[4][1] = 0;
mask[5][0] = -1; mask[5][1] = 1;
mask[6][0] = 0; mask[6][1] = 1;
mask[7][0] = 1; mask[7][1] = 1;
// find startpixel
unsigned int x = 0;
unsigned int y = 0;
while(m.get(Point(x, y)) == 0 && x < m.ncols() && y < m.nrows()){
if (x == m.ncols() - 1){
y++;
x = 0;
}
x++;
}
v_contour->push_back( Point(x, y) );
// extract contour
Point p_Right;
Point p_Middle;
Point p_Left;
unsigned int newX_R;
unsigned int newY_R;
unsigned int newX_M;
unsigned int newY_M;
unsigned int newX_L;
unsigned int newY_L;
bool found = false;
bool first = true;
bool border = true;
int n = 0;
int s = 6;
int third = 0;
int run = 1;
while ( (*v_contour)[n].x() != (*v_contour)[0].x() ||
(*v_contour)[n].y() != (*v_contour)[0].y() ||
first == true
){
found = false;
while(found == false && third < 3){
third++;
newX_R = (*v_contour)[n].x() + mask[ (s-1)%8 ][0];
newY_R = (*v_contour)[n].y() + mask[ (s-1)%8 ][1];
newX_M = (*v_contour)[n].x() + mask[ (s)%8 ][0];
newY_M = (*v_contour)[n].y() + mask[ (s)%8 ][1];
newX_L = (*v_contour)[n].x() + mask[ (s+1)%8 ][0];
newY_L = (*v_contour)[n].y() + mask[ (s+1)%8 ][1];
if(newX_R < m.ncols() && newY_R < m.nrows()){
p_Right.x( newX_R );
p_Right.y( newY_R );
border = false;
}
if(newX_M < m.ncols() && newY_M < m.nrows()){
p_Middle.x( newX_M );
p_Middle.y( newY_M );
border = false;
}
if(newX_L < m.ncols() && newY_L < m.nrows()){
p_Left.x( newX_L );
p_Left.y( newY_L );
border = false;
}
if(border == false){
border = true;
if ( is_black(m.get( p_Right)) && newX_R < m.ncols() && newY_R < m.nrows() )
{
v_contour->push_back(p_Right);
found = true;
n++;
s = s - 2;
}
else{
if(is_black(m.get(p_Middle)) && newX_M < m.ncols() && newY_M < m.nrows()){
v_contour->push_back(p_Middle);
found = true;
n++;
}
else {
if(is_black(m.get(p_Left) ) && newX_L < m.ncols() && newY_L < m.nrows() ){
v_contour->push_back(p_Left);
found = true;
n++;
}
else {
s = s + 2;
}
}
}
first = false;
}
else {
s = s + 2;
}
}
third = 0;
run++;
}
if (v_contour->size() > 1)
v_contour->pop_back(); // start pixel is doublette
return v_contour;
}
}
#endif
|