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
|
#include <string>
#include <algorithm> // for reverse
#include <stdio.h>
#include <stddef.h>
#include "gr.hh"
#include "extern.hh"
#include "image_ex.hh"
static bool filter_butterworth(double *x, double *xout, int nx, double *a, double *b, int nab);
static bool filter_image(int horl);
void highpass_image();
void lowpass_image();
#define ODD(n) (2 * ((n) / 2) != (n))
// filter grid rows|columns recursively a[0] a[1] ... b[0] b[1] ...
// 0 1 2 3 4
bool
filter_gridCmd()
{
if (_nword <= 3) {
NUMBER_WORDS_ERROR;
return false;
}
if (!word_is(3, "recursively")) {
err("Fourth word must be `recursively'");
return false;
}
bool do_rows = true;
if (word_is(2, "rows")) {
do_rows = true;
} else if (word_is(2, "columns")) {
do_rows = false;
} else {
err("Third word must be `rows' or `columns'");
return false;
}
// Get the coefficients
int nab = _nword - 4; // number (a,b) coefficients
if (nab < 1) {
err("No filter coefficients given.");
return false;
}
if (ODD(nab)) {
err("Must give even number of filter coefficients.");
return false;
}
nab = nab / 2;
vector<double> a((size_t)nab, 0.0);
vector<double> b((size_t)nab, 0.0);
unsigned int i, row, col;
for (i = 0; i < (unsigned int) nab; i++) {
a[i] = atof(_word[4 + i]);
b[i] = atof(_word[4 + i + nab]);
}
// Do the filtering
if (do_rows) {
vector<double> orig((size_t)_num_ymatrix_data, 0.0);
vector<double> copy((size_t)_num_ymatrix_data, 0.0);
for (col = 0; col < _num_xmatrix_data; col++) {
for (row = 0; row < _num_ymatrix_data; row++)
orig[row] = _f_xy(col, row);
filter_butterworth(orig.begin(), copy.begin(), _num_ymatrix_data,
a.begin(), b.begin(), nab);
for (row = 0; row < _num_ymatrix_data; row++)
_f_xy(col, row) = copy[row];
}
} else {
vector<double> orig((size_t)_num_xmatrix_data, 0.0);
vector<double> copy((size_t)_num_xmatrix_data, 0.0);
for (row = 0; row < _num_ymatrix_data; row++) {
for (col = 0; col < _num_xmatrix_data; col++)
orig[col] = _f_xy(col, row);
filter_butterworth(orig.begin(), copy.begin(), _num_xmatrix_data,
a.begin(), b.begin(), nab);
for (col = 0; col < _num_xmatrix_data; col++)
_f_xy(col, row) = copy[col];
}
}
return true;
}
// filter column x|y|z|u|v|r|theta recursively a[0] a[1] ... b[0] b[1] ...
bool
filter_columnCmd()
{
int i;
int nab; // number of a, b coefficients
int num = 0; // length of column
Require(_nword > 3,
NUMBER_WORDS_ERROR);
Require(word_is(3, "recursively"),
err("Fourth word must be `recursively'"));
double *orig; // point to data
if (word_is(2, "x")) {
num = _colX.size();
orig = _colX.begin();
} else if (word_is(2, "y")) {
num = _colY.size();
orig = _colY.begin();
} else if (word_is(2, "z")) {
num = _colZ.size();
orig = _colZ.begin();
} else if (word_is(2, "u")) {
num = _colU.size();
orig = _colU.begin();
} else if (word_is(2, "v")) {
num = _colV.size();
orig = _colV.begin();
} else if (word_is(2, "r")) {
num = _colR.size();
orig = _colR.begin();
} else if (word_is(2, "theta")) {
num = _colTHETA.size();
orig = _colTHETA.begin();
} else {
orig = 0; // prevent compiler warning
err("Unknown item.");
}
Require (num > 0,
err("No data in column named '", _word[2], "'", "\\"));
nab = _nword - 4;
Require(nab > 0,
err("No filter coefficients given."));
Require(!ODD(nab),
err("Must give even number of filter coefficients."));
nab = nab / 2;
vector<double> a((size_t)nab, 0.0);
vector<double> b((size_t)nab, 0.0);
for (i = 0; i < nab; i++) {
a[i] = atof(_word[4 + i]);
b[i] = atof(_word[4 + i + nab]);
}
vector<double> copy((size_t)num, 0.0);
filter_butterworth(orig, copy.begin(), num, a.begin(), b.begin(), nab);
for (i = 0; i < num; i++)
orig[i] = copy[i];
return true;
}
// filter_butterworth() -- do butterworth filtering, forward+back
// Input is x[] (unaltered), output is xout[]; recursive-style coefficients are
// a[]; moving average-style coefficients are b[].
static bool
filter_butterworth(double *x, double *xout, int nx, double *a, double *b, int nab)
{
if (nab >= nx)
return false;
vector<double> z((size_t)nx, 0.0);
register int ix, iab;
// pass 1 -- forward
for (ix = 0; ix < nab; ix++) {
// beginning part -- just copy
z[ix] = x[ix];
}
for (ix = nab; ix < nx; ix++) {
z[ix] = b[0] * x[ix];
for (iab = 1; iab < nab; iab++)
z[ix] += b[iab] * x[ix - iab] - a[iab] * z[ix - iab];
}
// pass 2 -- backward
reverse(z.begin(), z.begin() + nx);
for (ix = 0; ix < nab; ix++)
xout[ix] = z[ix];
for (ix = nab; ix < nx; ix++) {
xout[ix] = b[0] * z[ix];
for (iab = 1; iab < nab; iab++)
xout[ix] += b[iab] * z[ix - iab] - a[iab] * xout[ix - iab];
}
reverse(xout, xout + nx);
return true;
}
bool
filter_imageCmd()
{
if (_nword != 3) {
demonstrate_command_usage();
NUMBER_WORDS_ERROR;
} else if (!strcmp(_word[2], "highpass"))
filter_image(1);
else if (!strcmp(_word[2], "lowpass"))
filter_image(0);
else {
demonstrate_command_usage();
err("Can only do `highpass' or `lowpass' filter");
return false;
}
return true;
}
static bool
filter_image(int horl)
{
unsigned char *imPtr;
register int i, j;
int nx, ny, nx1, ny1, newval, change;
Require(image_exists(), err("no image exists"));
nx = _image.ras_width;
nx1 = nx - 1;
ny = _image.ras_height;
ny1 = ny - 1;
vector<unsigned char> imagenew((size_t)(nx * ny), (unsigned char)0);
for (j = ny - 1; j > -1; j--) {
imagenew[j] = _image.image[j];
imagenew[nx1 * ny + j] = _image.image[nx1 * ny + j];
}
for (i = 0; i < nx; i++) {
imagenew[i * ny] = _image.image[i * ny];
imagenew[i * ny + ny1] = _image.image[i * ny + ny1];
}
for (j = 1; j < ny1; j++) {
for (i = 1; i < nx1; i++) {
imPtr = _image.image + i * ny + j;
change = *(imPtr + 1); // i,j+1
change += *(imPtr - 1); // i,j-1
change += *(imPtr - ny); // i-1,j
change += *(imPtr + ny); // i-1,j
change -= *imPtr * 4;
change = (int) (0.125 * (double) change);
switch (horl) {
case 0: // low pass
newval = *imPtr + change;
break;
case 1: // high pass
default:
newval = *imPtr - change;
break;
}
if (newval < 0)
imagenew[i * ny + j] = (unsigned char) 0;
else if (newval > 255)
imagenew[i * ny + j] = (unsigned char) 255;
else
imagenew[i * ny + j] = (unsigned char) newval;
}
}
nx *= ny;
for (i = 0; i < nx; i++)
_image.image[i] = imagenew[i];
return true;
}
void
highpass_image()
{
filter_image(1);
}
void
lowpass_image()
{
filter_image(0);
}
|