Created
November 26, 2020 23:24
-
-
Save diegowinter/a40661429a2aacaee16b5b3a1a1139dc to your computer and use it in GitHub Desktop.
Especificação de 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 core; | |
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
public class Specification { | |
public static final String PATH = "your_path"; | |
static int[] calcularHistograma(BufferedImage img){ | |
int[] histograma = new int[256]; | |
for(int i=0; i<img.getWidth(); i++){ | |
for(int j=0; j<img.getHeight(); j++){ | |
Color color = new Color(img.getRGB(i, j)); | |
int red = color.getRed(); | |
int green = color.getGreen(); | |
int blue = color.getBlue(); | |
int gray = (red + green + blue) / 3; | |
histograma[gray] += 1; | |
} | |
} | |
return histograma; | |
} | |
static int[] calcularAcumulado(int[] histograma) { | |
int[] acumulado = new int[256]; | |
acumulado[0] = histograma[0]; | |
for(int i=1; i<histograma.length; i++) { | |
acumulado[i] = histograma[i] + acumulado[i-1]; | |
} | |
return acumulado; | |
} | |
static int procurarMenorValor(int[] histograma) { | |
for(int i=0; i <histograma.length; i++) { | |
if(histograma[i] != 0){ | |
return histograma[i]; | |
} | |
} | |
return 0; | |
} | |
static int[] caclularHistogramaEqualizado(int[] histograma, int[] acumulado, int x, int y) { | |
int[] histogramaEqualizado = new int[256]; | |
int quantidadePixels = x * y; | |
float menorValor = procurarMenorValor(histograma); | |
for(int i=0; i<acumulado.length; i++) { | |
histogramaEqualizado[i] = Math.round(((acumulado[i] - menorValor) / (quantidadePixels - menorValor)) * 255); | |
} | |
return histogramaEqualizado; | |
} | |
static BufferedImage equalizar(BufferedImage img) { | |
int[] histograma = calcularHistograma(img); | |
int[] acumulado = calcularAcumulado(histograma); | |
int[] histogramaEqualizado = caclularHistogramaEqualizado(histograma, acumulado, img.getWidth(), img.getHeight()); | |
BufferedImage imgEqualizada = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB); | |
for (int j=0; j<img.getHeight(); j++) { | |
for (int i=0; i<img.getWidth(); i++) { | |
Color color = new Color(img.getRGB(i, j)); | |
int tom = color.getRed(); | |
int newTom = histogramaEqualizado[tom]; | |
Color newColor = new Color(newTom, newTom, newTom); | |
imgEqualizada.setRGB(i, j, newColor.getRGB()); | |
} | |
} | |
return imgEqualizada; | |
} | |
static int encontrarCombinacaoMaisProxima(int valor, int[] histogramaEqualizadoTarget) { | |
for(int i=0; i<256; i++) { | |
if(histogramaEqualizadoTarget[i] >= valor) { | |
return i; | |
} | |
} | |
return 255; | |
} | |
static int[] calcularHistogramaEspecificado(int[] histogramaEqualizadoEntrada, int[] histogramaEqualizadoTarget) { | |
int[] histogramaEspecificado = new int[256]; | |
for(int i=0; i<256; i++) { | |
int nivel = encontrarCombinacaoMaisProxima(histogramaEqualizadoEntrada[i], histogramaEqualizadoTarget); | |
histogramaEspecificado[i] = nivel; | |
} | |
return histogramaEspecificado; | |
} | |
static BufferedImage especificar(BufferedImage imgEntrada, BufferedImage imgTarget) { | |
int[] histogramaEntrada = calcularHistograma(imgEntrada); | |
int[] acumuladoEntrada = calcularAcumulado(histogramaEntrada); | |
int[] histogramaEqualizadoEntrada = caclularHistogramaEqualizado(histogramaEntrada, acumuladoEntrada, imgEntrada.getWidth(), imgEntrada.getHeight()); | |
int[] histogramaTarget = calcularHistograma(imgTarget); | |
int[] acumuladoTarget = calcularAcumulado(histogramaTarget); | |
int[] histogramaEqualizadoTarget = caclularHistogramaEqualizado(histogramaTarget, acumuladoTarget, imgTarget.getWidth(), imgTarget.getHeight()); | |
int[] histogramaEspecificado = calcularHistogramaEspecificado(histogramaEqualizadoEntrada, histogramaEqualizadoTarget); | |
BufferedImage imgEspecificada = new BufferedImage(imgEntrada.getWidth(), imgEntrada.getHeight(), BufferedImage.TYPE_INT_RGB); | |
for (int j=0; j<imgEntrada.getHeight(); j++) { | |
for (int i=0; i<imgEntrada.getWidth(); i++) { | |
Color color = new Color(imgEntrada.getRGB(i, j)); | |
int tom = color.getRed(); | |
int newTom = histogramaEspecificado[tom]; | |
Color newColor = new Color(newTom, newTom, newTom); | |
imgEspecificada.setRGB(i, j, newColor.getRGB()); | |
} | |
} | |
return imgEspecificada; | |
} | |
public static void main(String[] args) throws IOException { | |
BufferedImage imgEntrada = ImageIO.read(new File(PATH, "img1.jpg")); | |
BufferedImage imgTarget = ImageIO.read(new File(PATH, "img2.jpg")); | |
BufferedImage imagemEspecificada = especificar(imgEntrada, imgTarget); | |
ImageIO.write(imagemEspecificada, "jpg", new File(PATH, "output.jpg")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment