Created
July 21, 2015 13:58
-
-
Save hugoabonizio/7773506507d544d53d1b to your computer and use it in GitHub Desktop.
Exercício 6 - Histograma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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