Last active
May 8, 2017 08:24
-
-
Save juandjara/4e85044c22a2369c39f9721b39244427 to your computer and use it in GitHub Desktop.
Archivo para la clase de Teledeteccion
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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package javatdt; | |
import java.awt.Color; | |
/** | |
* | |
* @author Kaito | |
*/ | |
// <editor-fold defaultstate="collapsed" desc="Métodos"> | |
public class Metodos { | |
public static Imagen ExpansionLineal(Imagen imagen, int ndCorteMin, int ndCorteMax){ | |
float numerador; | |
float denominador; | |
float res; | |
for(int i=0; i<imagen.anchura;i++){ | |
for(int j=0; j<imagen.altura;j++){ | |
if(imagen.pixeles[i][j]<ndCorteMin) imagen.pixeles[i][j] = 0; | |
else{ | |
if(imagen.pixeles[i][j]>ndCorteMax) imagen.pixeles[i][j] = 255; | |
else{ | |
numerador = imagen.pixeles[i][j]-ndCorteMin; | |
denominador = (ndCorteMax-ndCorteMin); | |
res = 255*(numerador/denominador); | |
imagen.pixeles[i][j] = (short) res; | |
} | |
} | |
} | |
} | |
return imagen; | |
} | |
public static int CalculaPercentilMin(Histograma histograma, int numpixeles, int porcentaje){ | |
Integer acum = 0; | |
int res=0; | |
int perc = (int)((porcentaje*1./100.)*numpixeles*1.); | |
for(int i=0;i <= histograma.values.length;i++){ | |
acum += histograma.values[i]; | |
if(acum >= perc){ | |
res = i; | |
break; | |
} | |
} | |
return res; | |
} | |
public static int CalculaPercentilMax(Histograma histograma, int numpixeles, int porcentaje){ | |
Integer acum = 0; | |
int res=0; | |
int perc = (int)((porcentaje*1./100.)*numpixeles*1.); | |
acum = numpixeles; | |
for(int i=255; i > 0;i--){ | |
acum -= histograma.values[i]; | |
if(acum <= numpixeles-perc){ | |
res = i; | |
break; | |
} | |
} | |
return res; | |
} | |
public static Color[][] ActualizarFalsoColor(Imagen rojo, Imagen verde, Imagen azul){ | |
throw new UnsupportedOperationException(); | |
} | |
public static float FiltroEnPixel(float[] matrizPaso, Imagen imagen, int fila, int col){ | |
float result = 0; | |
if(fila <= 0 || fila >= (imagen.altura - 1) || col <= 0 || col >= (imagen.anchura - 1)) { | |
return result; | |
} | |
float[] entorno = { | |
imagen.pixeles[col-1][fila-1], | |
imagen.pixeles[col][fila-1], | |
imagen.pixeles[col+1][fila-1], | |
imagen.pixeles[col-1][fila], | |
imagen.pixeles[col][fila], | |
imagen.pixeles[col+1][fila], | |
imagen.pixeles[col-1][fila+1], | |
imagen.pixeles[col][fila+1], | |
imagen.pixeles[col+1][fila+1] | |
}; | |
for(int i = 0; i < matrizPaso.length; i++){ | |
float pixelFiltro = matrizPaso[i]; | |
float pixelEntorno = entorno[i]; | |
result += pixelFiltro * pixelEntorno; | |
} | |
float sumaCoef = SumarCoeficientes(matrizPaso); | |
result = result / sumaCoef; | |
if(result < 0) { | |
result = 0; | |
} | |
if(result > 255) { | |
result = 255; | |
} | |
return result; | |
} | |
public static Imagen Filtro(float[] matrizPaso, Imagen imagen){ | |
for(int i=0; i<imagen.anchura;i++){ | |
for(int j=0; j<imagen.altura;j++){ | |
imagen.pixeles[i][j] = (short) FiltroEnPixel(matrizPaso, imagen, j, i); | |
} | |
} | |
return imagen; | |
} | |
public static Imagen FiltroSobel(float[] matrizSobelH, float[] matrizSobelV, Imagen imagen){ | |
Imagen F = Filtro(matrizSobelH, imagen); | |
Imagen C = Filtro(matrizSobelV, imagen); | |
for(int i=0; i<imagen.anchura;i++){ | |
for(int j=0; j<imagen.altura;j++){ | |
short pixelF = F.pixeles[i][j]; | |
short pixelC = C.pixeles[i][j]; | |
short result = (short) Math.sqrt((pixelC * pixelC)+(pixelF * pixelF)); | |
if(result < 0) { | |
result = 0; | |
} | |
if(result > 255) { | |
result = 255; | |
} | |
imagen.pixeles[i][j] = result; | |
} | |
} | |
return imagen; | |
} | |
public static Imagen Ecualizacion(Imagen imagen, Histograma histograma){ | |
int[] frAcum = new int[256]; | |
frAcum[0] = histograma.values[0]; | |
for(int i=1;i<=255;i++){ | |
frAcum[i] = frAcum[i-1] + histograma.values[i]; | |
} | |
int numPixeles = frAcum[255]; | |
double factorEscala = (255*1.000)/(numPixeles*1.000); | |
double[] frAes = new double[256]; | |
int[] ndNuevo = new int[256]; | |
for(int i = 0;i<=255;i++){ | |
frAes[i] = frAcum[i]*factorEscala; | |
ndNuevo[i] = (int)Math.round((frAcum[i]*factorEscala)); | |
} | |
for(int i=0;i<imagen.anchura;i++){ | |
for(int e=0;e<imagen.altura;e++){ | |
imagen.pixeles[i][e] = (short) ndNuevo[imagen.pixeles[i][e]]; | |
} | |
} | |
return imagen; | |
} | |
public static float SumarCoeficientes(float[] matrizPaso){ | |
float result = 0; | |
for(int i = 0; i < matrizPaso.length; i++){ | |
result += matrizPaso[i]; | |
} | |
if(result > 0) { | |
return result; | |
} else { | |
return 1; | |
} | |
} | |
public static Imagen RestarImagenes(Imagen imagen1, Imagen imagen2){ | |
throw new UnsupportedOperationException(); | |
} | |
public static Imagen SumarImagenes(Imagen imagen1, Imagen imagen2){ | |
throw new UnsupportedOperationException(); | |
} | |
//</editor-fold> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment