Skip to content

Instantly share code, notes, and snippets.

@hugoabonizio
Created July 21, 2015 13:58
Show Gist options
  • Save hugoabonizio/7773506507d544d53d1b to your computer and use it in GitHub Desktop.
Save hugoabonizio/7773506507d544d53d1b to your computer and use it in GitHub Desktop.
Exercício 6 - Histograma
package aulacompgrafica;
import ij.ImagePlus;
import ij.io.Opener;
import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;
import java.awt.Color;
public class AulaCompGrafica {
public static void main(String[] args) {
new AulaCompGrafica().histogram();
}
public void histogram() {
int histogram[] = new int[256];
ImagePlus imp = new Opener().openImage("lena.bmp");
ImageProcessor ip = imp.getProcessor();
int width = imp.getWidth();
int height = imp.getHeight();
System.out.println(width);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
histogram[imp.getPixel(i, j)[0]]++;
}
}
ImageProcessor ip2 = new ColorProcessor(256, 100);
ip2.setColor(Color.GRAY);
ip2.fill();
int max = 0;
for (int i = 0; i < 256; i++)
if (histogram[i] > max)
max = histogram[i];
int factor, result;
for (int i = 0; i < 256; i++) {
if (histogram[i] == 0) {
result = 0;
} else {
factor = ((100 * histogram[i]) / max);
result = (100 * factor) / 100;
}
for (int j = 0; j < result; j++) {
// System.out.println("plotando (" + i + ", " + j + ")");
ip2.putPixel(i, 100 - j, Color.BLACK.getRGB());
}
// if (histogram[i] != 0) {
// System.out.println("plotando: (" + i + ") " + result);
// }
}
ImagePlus imp2 = new ImagePlus("Histogram", ip2);
imp2.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment