use of mpi.fruitfly.math.datastructures.FloatArray2D in project TrakEM2 by trakem2.
the class ImageFilter method create_gaussian_kernel_2D_offset.
/*
** create a normalized gaussian impulse with appropriate size and offset center
*/
public static FloatArray2D create_gaussian_kernel_2D_offset(final float sigma, final float offset_x, final float offset_y, final boolean normalize) {
int size = 3;
FloatArray2D gaussian_kernel;
if (sigma == 0) {
gaussian_kernel = new FloatArray2D(3, 3);
gaussian_kernel.data[4] = 1;
} else {
size = Math.max(3, (int) (2 * Math.round(3 * sigma) + 1));
final float two_sq_sigma = 2 * sigma * sigma;
// float normalization_factor = 1.0/(float)M_PI/two_sq_sigma;
gaussian_kernel = new FloatArray2D(size, size);
for (int x = size - 1; x >= 0; --x) {
final float fx = (float) (x - size / 2);
for (int y = size - 1; y >= 0; --y) {
final float fy = (float) (y - size / 2);
final float val = (float) (Math.exp(-(Math.pow(fx - offset_x, 2) + Math.pow(fy - offset_y, 2)) / two_sq_sigma));
gaussian_kernel.set(val, x, y);
}
}
}
if (normalize) {
float sum = 0;
for (final float value : gaussian_kernel.data) sum += value;
for (int i = 0; i < gaussian_kernel.data.length; i++) gaussian_kernel.data[i] /= sum;
}
return gaussian_kernel;
}
use of mpi.fruitfly.math.datastructures.FloatArray2D in project TrakEM2 by trakem2.
the class ImageFilter method computeLaPlaceFilter3.
public static FloatArray2D computeLaPlaceFilter3(final FloatArray2D input) {
final FloatArray2D output = new FloatArray2D(input.width, input.height);
float derivX, derivY;
float x1, x2, x3;
float y1, y2, y3;
for (int y = 1; y < input.height - 1; y++) for (int x = 1; x < input.width - 1; x++) {
x1 = input.get(x - 1, y);
x2 = input.get(x, y);
x3 = input.get(x + 1, y);
derivX = x1 - 2 * x2 + x3;
y1 = input.get(x, y - 1);
y2 = input.get(x, y);
y3 = input.get(x, y + 1);
derivY = y1 - 2 * y2 + y3;
output.set((float) Math.sqrt(Math.pow(derivX, 2) + Math.pow(derivY, 2)), x, y);
}
return output;
}
use of mpi.fruitfly.math.datastructures.FloatArray2D in project TrakEM2 by trakem2.
the class ImageFilter method computeGaussian.
public static FloatArray2D computeGaussian(final FloatArray2D input, final float sigma) {
final FloatArray2D output = new FloatArray2D(input.width, input.height);
float avg, kernelsum;
final FloatArray2D kernel = createGaussianKernel2D(sigma, true);
final int filterSize = kernel.width;
for (int x = 0; x < input.width; x++) {
for (int y = 0; y < input.height; y++) {
avg = 0;
kernelsum = 0;
for (int fx = -filterSize / 2; fx <= filterSize / 2; fx++) for (int fy = -filterSize / 2; fy <= filterSize / 2; fy++) {
try {
avg += input.get(x + fx, y + fy) * kernel.get(fx + filterSize / 2, fy + filterSize / 2);
kernelsum += kernel.get(fx + filterSize / 2, fy + filterSize / 2);
} catch (final Exception e) {
}
;
}
output.set(avg / kernelsum, x, y);
}
}
return output;
}
use of mpi.fruitfly.math.datastructures.FloatArray2D in project TrakEM2 by trakem2.
the class ImageFilter method createGradients.
public static FloatArray2D[] createGradients(final FloatArray2D array) {
final FloatArray2D[] gradients = new FloatArray2D[2];
gradients[0] = new FloatArray2D(array.width, array.height);
gradients[1] = new FloatArray2D(array.width, array.height);
for (int y = 0; y < array.height; ++y) {
final int[] ro = new int[3];
ro[0] = array.width * Math.max(0, y - 1);
ro[1] = array.width * y;
ro[2] = array.width * Math.min(y + 1, array.height - 1);
for (int x = 0; x < array.width; ++x) {
// L(x+1, y) - L(x-1, y)
final float der_x = (array.data[ro[1] + Math.min(x + 1, array.width - 1)] - array.data[ro[1] + Math.max(0, x - 1)]) / 2;
// L(x, y+1) - L(x, y-1)
final float der_y = (array.data[ro[2] + x] - array.data[ro[0] + x]) / 2;
// ! amplitude
gradients[0].data[ro[1] + x] = (float) Math.sqrt(Math.pow(der_x, 2) + Math.pow(der_y, 2));
// ! orientation
gradients[1].data[ro[1] + x] = (float) Math.atan2(der_y, der_x);
}
}
// ImageArrayConverter.FloatArrayToImagePlus( gradients[ 1 ], "gradients", 0, 0 ).show();
return gradients;
}
use of mpi.fruitfly.math.datastructures.FloatArray2D in project TrakEM2 by trakem2.
the class ImageFilter method convolveSeparable.
/**
* convolve an image with a horizontal and a vertical kernel
* simple straightforward, not optimized---replace this with a trusted better version soon
*
* @param input the input image
* @param h horizontal kernel
* @param v vertical kernel
*
* @return convolved image
*/
public static FloatArray2D convolveSeparable(final FloatArray2D input, final float[] h, final float[] v) {
final FloatArray2D output = new FloatArray2D(input.width, input.height);
final FloatArray2D temp = new FloatArray2D(input.width, input.height);
final int hl = h.length / 2;
final int vl = v.length / 2;
int xl = input.width - h.length + 1;
int yl = input.height - v.length + 1;
// create lookup tables for coordinates outside the image range
final int[] xb = new int[h.length + hl - 1];
final int[] xa = new int[h.length + hl - 1];
for (int i = 0; i < xb.length; ++i) {
xb[i] = flipInRange(i - hl, input.width);
xa[i] = flipInRange(i + xl, input.width);
}
final int[] yb = new int[v.length + vl - 1];
final int[] ya = new int[v.length + vl - 1];
for (int i = 0; i < yb.length; ++i) {
yb[i] = input.width * flipInRange(i - vl, input.height);
ya[i] = input.width * flipInRange(i + yl, input.height);
}
// String xa_str = "xa: ";
// String xb_str = "xb: ";
// String ya_str = "ya: ";
// String yb_str = "yb: ";
// for ( int i = 0; i < xa.length; ++i )
// {
// xa_str = xa_str + xa[ i ] + ", ";
// xb_str = xb_str + xb[ i ] + ", ";
// ya_str = ya_str + ( ya[ i ] / input.width ) + ", ";
// yb_str = yb_str + ( yb[ i ] / input.width ) + ", ";
// }
//
// System.out.println( xb_str );
// System.out.println( xa_str );
// System.out.println( yb_str );
// System.out.println( ya_str );
xl += hl;
yl += vl;
// horizontal convolution per row
int rl = input.height * input.width;
for (int r = 0; r < rl; r += input.width) {
for (int x = hl; x < xl; ++x) {
final int c = x - hl;
float val = 0;
for (int xk = 0; xk < h.length; ++xk) {
val += h[xk] * input.data[r + c + xk];
}
temp.data[r + x] = val;
}
for (int x = 0; x < hl; ++x) {
float valb = 0;
float vala = 0;
for (int xk = 0; xk < h.length; ++xk) {
valb += h[xk] * input.data[r + xb[x + xk]];
vala += h[xk] * input.data[r + xa[x + xk]];
}
temp.data[r + x] = valb;
temp.data[r + x + xl] = vala;
}
}
// vertical convolution per column
rl = yl * temp.width;
final int vlc = vl * temp.width;
for (int x = 0; x < temp.width; ++x) {
for (int r = vlc; r < rl; r += temp.width) {
float val = 0;
final int c = r - vlc;
int rk = 0;
for (int yk = 0; yk < v.length; ++yk) {
val += v[yk] * temp.data[c + rk + x];
rk += temp.width;
}
output.data[r + x] = val;
}
for (int y = 0; y < vl; ++y) {
final int r = y * temp.width;
float valb = 0;
float vala = 0;
for (int yk = 0; yk < v.length; ++yk) {
valb += h[yk] * temp.data[yb[y + yk] + x];
vala += h[yk] * temp.data[ya[y + yk] + x];
}
output.data[r + x] = valb;
output.data[r + rl + x] = vala;
}
}
return output;
}
Aggregations