Skip to content

Instantly share code, notes, and snippets.

@liuliu
Created March 22, 2011 19:05
Show Gist options
  • Save liuliu/881824 to your computer and use it in GitHub Desktop.
Save liuliu/881824 to your computer and use it in GitHub Desktop.
public static class Map extends
Mapper<ImageHeader, FloatImage, IntWritable, FloatImage> {
public void map(ImageHeader key, FloatImage value, Context context)
throws IOException, InterruptedException {
if (value != null && value.getWidth() > 100 && value.getHeight() > 100) {
float[][] tp = new float[100][32 * 32];
for (int i = 0; i < 10; i++) {
int x = (value.getWidth() - 32) * i / 10;
for (int j = 0; j < 10; j++) {
int y = (value.getHeight() - 32) * j / 10;
FloatImage image = value.crop(x, y, 32, 32);
float[] pels = image.getData();
for (int iy = 0; iy < 32; iy++)
for (int ix = 0; ix < 32; ix++)
tp[i * 10 + j][iy * 32 + ix] = (pels[iy * 32 * 3 + ix * 3] + pels[iy * 32 * 3 + ix * 3 + 1] + pels[iy * 32 * 3 + ix * 3 + 2]) / 3 - 100.0f / 255.0f;
}
}
float[] cov = new float[32 * 32 * 32 * 32];
for (int i = 0; i < 32 * 32; i++)
for (int j = 0; j < 32 * 32; j++) {
cov[i * 32 * 32 + j] = 0;
for (int k = 0; k < 100; k++)
cov[i * 32 * 32 + j] += tp[k][i] * tp[k][j];
}
context.write(new IntWritable(0), new FloatImage(32 * 32, 32 * 32, 1, cov));
}
}
}
public static class Reduce extends
Reducer<IntWritable, FloatImage, IntWritable, FloatImage> {
public void reduce(IntWritable key, Iterable<FloatImage> values,
Context context) throws IOException, InterruptedException {
float[] cov = new float[32 * 32 * 32 * 32];
for (int i = 0; i < 32 * 32 * 32 * 32; i++)
cov[i] = 0;
for (FloatImage val : values) {
float[] image = val.getData();
for (int i = 0; i < 32 * 32 * 32 * 32; i++)
cov[i] += image[i];
}
context.write(key, new FloatImage(32 * 32, 32 * 32, 1, cov));
}
}
public static class Map extends
Mapper<ImageHeader, FloatImage, IntWritable, FloatImage> {
public void map(ImageHeader key, FloatImage value, Context context)
throws IOException, InterruptedException {
if (value != null && value.getWidth() > 100 && value.getHeight() > 100) {
float[] tp = new float[32 * 32];
for (int i = 0; i < 10; i++) {
int x = (value.getWidth() - 32) * i / 10;
for (int j = 0; j < 10; j++) {
int y = (value.getHeight() - 32) * j / 10;
FloatImage image = value.crop(x, y, 32, 32);
float[] pels = image.getData();
for (int iy = 0; iy < 32; iy++)
for (int ix = 0; ix < 32; ix++)
tp[iy * 32 + ix] += (pels[iy * 32 * 3 + ix * 3] + pels[iy * 32 * 3 + ix * 3 + 1] + pels[iy * 32 * 3 + ix * 3 + 2]) / 3;
}
}
for (int i = 0; i < 32; i++)
for (int j = 0; j < 32; j++)
tp[i * 32 + j] /= 100;
context.write(new IntWritable(0), new FloatImage(32, 32, 1, tp));
}
}
}
public static class Reduce extends
Reducer<IntWritable, FloatImage, IntWritable, FloatImage> {
public void reduce(IntWritable key, Iterable<FloatImage> values,
Context context) throws IOException, InterruptedException {
float[] mean = new float[32 * 32];
int total = 0;
for (FloatImage val : values) {
float[] image = val.getData();
for (int i = 0; i < 32 * 32; i++)
mean[i] += image[i];
total++;
}
if (total > 0) {
for (int i = 0; i < 32 * 32; i++)
mean[i] /= total;
context.write(key, new FloatImage(32, 32, 1, mean));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment