/usr/include/gamera/plugins/projections.hpp is in python-gamera-dev 3.4.2+svn1437-2.
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 | /*
*
* Copyright (C) 2001-2005
* Ichiro Fujinaga, Michael Droettboom, and Karl MacMillan
*
* 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 kwm02212003_projections
#define kwm02212003_projections
#include "gamera.hpp"
namespace Gamera {
#ifndef round
template<class T>
inline T round(T p){return T(floor(p + (T)0.5));}
#endif
/*
Generic projection routine - x and y projections
are acheived by passing in either row or col
iterators.
*/
template<class T>
inline IntVector* projection(T i, const T end) {
IntVector* proj = new IntVector(end - i, 0);
try {
typename T::iterator j;
typename IntVector::iterator p = proj->begin();
for (; i != end; ++i, ++p) {
for (j = i.begin(); j != i.end(); ++j) {
if (is_black(*j))
*p += 1;
}
}
} catch (std::exception e) {
delete proj;
throw;
}
return proj;
}
/*
Projection along the y axis (rows) of an image.
*/
template<class T>
IntVector* projection_rows(const T& image) {
return projection(image.row_begin(), image.row_end());
}
/*
Projection along the y axis (rows) of a portion
on an image.
NOTE: 'rect' must be absolute with respect to the underlying image data,
*not* relative to the offset of the view 'image'
*/
template<class T>
IntVector* projection_rows(const T& image, const Rect& rect) {
T proj_image(image, rect);
return projection_rows(proj_image);
}
/*
Projection along the x axis (rows) of an image.
MGD: Should be faster now because it accesses the image data in
row-major order.
*/
template<class T>
IntVector* projection_cols(const T& image) {
IntVector* proj = new IntVector(image.ncols(), 0);
try {
for (size_t r = 0; r != image.nrows(); ++r) {
for (size_t c = 0; c != image.ncols(); ++c) {
if (is_black(image.get(Point(c, r)))) {
(*proj)[c] += 1;
}
}
}
} catch (std::exception e) {
delete proj;
throw;
}
return proj;
}
/*
Projection along the y axis (rows) of a portion
on an image.
NOTE: 'rect' must be absolute with respect to the underlying image data,
*not* relative to the offset of the view 'image'
*/
template<class T>
IntVector* projection_cols(const T& image, const Rect& rect) {
T proj_image(image, rect);
return projection_cols(proj_image);
}
/*
Projections of strips of a image -
the coordinates are relative to the view.
*/
template<class T>
IntVector* yproj_vertical_strip(T& image, size_t offset_x,
size_t width) {
Rect r(Point(image.offset_x() + offset_x, image.offset_y()),
Dim(width, image.nrows()));
return projection_rows(image, r);
}
template<class T>
IntVector* yproj_horizontal_strip(T& image, size_t offset_y,
size_t height) {
Rect r(Point(image.offset_x(), image.offset_y() + offset_y),
Dim(image.ncols(), height));
return projection_rows(image, r);
}
template<class T>
IntVector* xproj_vertical_strip(T& image, size_t offset_x,
size_t width) {
Rect r(Point(image.offset_x() + offset_x, image.offset_y()),
Dim(width, image.nrows()));
return projection_cols(image, r);
}
template<class T>
IntVector* xproj_horizontal_strip(T& image, size_t offset_y,
size_t height) {
Rect r(Point(image.offset_x(), image.offset_y() + offset_y),
Dim(image.ncols(), height));
return projection_cols(image, r);
}
/*
returns y-projections of a rotated image
*/
template<class T>
void projection_skewed_cols(const T& image, FloatVector* angles, std::vector<IntVector*>& proj) {
int x;
size_t i;
size_t n = angles->size();
FloatVector sina(n);
FloatVector cosa(n);
for (i = 0; i < n; i++) {
sina[i] = sin((*angles)[i] * M_PI / 180.0);
cosa[i] = cos((*angles)[i] * M_PI / 180.0);
}
for (i = 0; i < n; i++)
proj[i] = new IntVector(image.ncols(), 0);
// compute skewed projections simultanously
for (size_t r = 0; r < image.nrows(); ++r) {
for (size_t c = 0; c < image.ncols(); ++c) {
if (is_black(image.get(Point(c, r)))) {
for (i = 0; i < n; i++) {
x = (int) round(c*cosa[i] - r*sina[i]);
if ((x > 0) && (x < (int)image.ncols()))
++(*(proj[i]))[x];
}
}
}
}
}
// The Python part
template<class T>
PyObject* projection_skewed_cols(const T& image, FloatVector* angles) {
size_t n = angles->size();
std::vector<IntVector*> proj(n);
projection_skewed_cols(image, angles, proj);
PyObject* projlist = PyList_New(n);
// move projections to return list
for (size_t i = 0; i < n; i++) {
PyList_SET_ITEM(projlist, i, IntVector_to_python(proj[i]));
delete proj[i];
}
return projlist;
}
/*
returns x-projections of a rotated image
*/
template<class T>
void projection_skewed_rows(const T& image, FloatVector* angles,
std::vector<IntVector*>& proj) {
int y;
size_t i;
size_t n = angles->size();
FloatVector sina(n);
FloatVector cosa(n);
for (i = 0; i < n; i++) {
sina[i] = sin((*angles)[i] * M_PI / 180.0);
cosa[i] = cos((*angles)[i] * M_PI / 180.0);
}
for (i = 0; i < n; i++)
proj[i] = new IntVector(image.nrows(), 0);
// compute skewed projections simultanously
for (size_t r = 0; r < image.nrows(); ++r) {
for (size_t c = 0; c < image.ncols(); ++c) {
if (is_black(image.get(Point(c, r)))) {
for (i = 0; i < n; i++) {
y = (int) round(c*sina[i] + r*cosa[i]);
if ((y > 0) && (y < (int)image.nrows()))
++(*(proj[i]))[y];
}
}
}
}
}
// The Python part
template<class T>
PyObject* projection_skewed_rows(const T& image, FloatVector* angles) {
size_t n = angles->size();
std::vector<IntVector*> proj(n);
projection_skewed_rows(image, angles, proj);
PyObject* projlist = PyList_New(n);
// move projections to return list
for (size_t i = 0; i < n; i++) {
PyList_SET_ITEM(projlist, i, IntVector_to_python(proj[i]));
delete proj[i];
}
return projlist;
}
}
#endif
|