Bom dia, pessoal estou desenvolvendo um programa de reconhecimento de imagens detectadas por webcam
mas como sou iniciante em java já consegui comparar duas imagens e mostrar o resultado com subtração de imagem
e filtro passa alta, agora não sei como posso fazer para identificar se naquela imagem existe pixels brancos, se tem como verificar a porcentagem acima de 10% da imagem.
Não da pra percorrer com FOR e ir checando pixel por pixel ?
amigo ainda sou iniciante em java você poderia me ajudar com o código?
Leia:
O colega acima está certo. Percorra a imagem chamando o getPixel para ler a cor do pixel.
Boa tarde, pessoal já estou a duas semanas tentando fazer vocês não poderiam me ajudar com o código?
preciso entregar um projeto semana que vem, e não consigo de jeito nenhum
Primeiro de tudo, a imagem é binária (só preto puro e branco puro), em tons de cinza ou colorida?
Se for colorida, como você define “branco”?
E se for colorida, você está trabalhando com qual padrão de cores?
É uma imagem preto e branco, apliquei o filtro passa alta.
O que eu quero fazer é o seguinte meu sistema compara duas imagens e mostra se houve movimento em passa alta, se as duas imagens comparadas forem iguais a imagem resultado fica totalmente em preto, se tiver alguma diferença de objetos, ou pessoas sai em passa alta (branco) então o sistema emitiria um som e um alerta.
O som e o alerta já estão prontos também a única coisa é que preciso definir que só tocará o som e aparecerá o alerta, quando houver uma quantidade pixels brancos.
se puder me ajudar ficarei grato.
Poxa, o código é extremamente trivial:
public static double percentualBrancos(BufferedImage img)
{
double count = 0;
for (int y = 0; y < img.getHeight(); y++)
for (int x = 0; x < img.getWidth(); x++)
{
Color pixel = new Color(img.getRGB(x, y));
if (pixel.getRed() == 255)
count++;
}
return count / (img.getWidth() * img.getHeight());
}
estou tentando pegar a imagem e usar esse método para mostrar a porcentagem dai se a porcentagem for acima de um valor
emite um alerta, mas não consigo, a imagem não está passando.
Estou usando imageprocessor o que posso fazer?
Posta seu código. Minha bola de cristal está na manutenção.
if (btn_resultado.isEnabled() == true) {
try {
int tipo = BufferedImage.TYPE_BYTE_GRAY;
BufferedImage imagemsub = new BufferedImage(w, height, tipo);
int k, z;
Raster raster1 = image1.getRaster();
Raster raster2 = image2.getRaster();
WritableRaster raster = imagemsub.getRaster();
for (k = 0; k < image1.getHeight(); k++) {
for (z = 0; z < image1.getWidth(); z++) {
if (raster1.getSample(z, k, 0) - raster2.getSample(z, k, 0) < 0) {
raster.setSample(z, k, 0, 0);
} else {
raster.setSample(z, k, 0, raster1.getSample(z, k, 0) - raster2.getSample(z, k, 0));
}
}
}
int altura = imagemsub.getHeight();
int largura = imagemsub.getWidth();
BufferedImage imagemout = new BufferedImage(largura, altura, tipo);
WritableRaster rasterout = imagemout.getRaster();
int pixelout = 0;
int pixelout2 = 0;
int pixelfinal = 0;
int alt = altura - 1;
int larg = largura - 1;
// template 1
int a, b, c, d, e, f, g, h, i, div;
a = -1;
b = -1;
c = -1;
d = 0;
e = 0;
f = 0;
g = 1;
h = 1;
i = 1;
div = 1;
int j, l, m, n, o, p, q, r, s, div2;
j = -1;
l = 0;
m = 1;
n = -1;
o = 0;
p = 1;
q = -1;
r = 0;
s = 1;
div2 = 1;
for (int linha = 0; linha < altura; linha++) {
for (int col = 0; col < largura; col++) {
if (linha == 0) {
if (col == 0) {
pixelout = (raster.getSample(larg, alt, 0) * a + raster.getSample(col, alt, 0) * b + raster.getSample(col + 1, alt, 0) * c
+ raster.getSample(larg, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(col + 1, linha, 0) * f
+ raster.getSample(larg, linha + 1, 0) * g + raster.getSample(col, linha + 1, 0) * h + raster.getSample(col + 1, linha + i, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(larg, alt, 0) * j + raster.getSample(col, alt, 0) * l + raster.getSample(col + 1, alt, 0) * m
+ raster.getSample(larg, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(col + 1, linha, 0) * p
+ raster.getSample(larg, linha + 1, 0) * q + raster.getSample(col, linha + 1, 0) * r + raster.getSample(col + 1, linha + i, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
}//fim do col =0
else if (col == larg) {
pixelout = (raster.getSample(col - 1, alt, 0) * a + raster.getSample(col, alt, 0) * b + raster.getSample(0, alt, 0) * c
+ raster.getSample(col - 1, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(0, linha, 0) * f
+ raster.getSample(col - 1, linha + 1, 0) * g + raster.getSample(col, linha + 1, 0) * h + raster.getSample(0, linha + 1, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(col - 1, alt, 0) * j + raster.getSample(col, alt, 0) * l + raster.getSample(0, alt, 0) * m
+ raster.getSample(col - 1, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(0, linha, 0) * p
+ raster.getSample(col - 1, linha + 1, 0) * q + raster.getSample(col, linha + 1, 0) * r + raster.getSample(0, linha + 1, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
}// fim do col = larg
else { // linha=0 col =qualquer
pixelout = (raster.getSample(col - 1, alt, 0) * a + raster.getSample(col, alt, 0) * b + raster.getSample(col + 1, alt, 0) * c
+ raster.getSample(col - 1, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(col + 1, linha, 0) * f
+ raster.getSample(col - 1, linha + 1, 0) * g + raster.getSample(col, linha + 1, 0) * h + raster.getSample(col + 1, linha + 1, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(col - 1, alt, 0) * j + raster.getSample(col, alt, 0) * l + raster.getSample(col + 1, alt, 0) * m
+ raster.getSample(col - 1, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(col + 1, linha, 0) * p
+ raster.getSample(col - 1, linha + 1, 0) * q + raster.getSample(col, linha + 1, 0) * r + raster.getSample(col + 1, linha + 1, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
} //fim do else col!= larg
}// fim do linha =0
else if (linha == alt) {
if (col == 0) {
pixelout = (raster.getSample(larg, linha - 1, 0) * a + raster.getSample(col, linha - 1, 0) * b + raster.getSample(col + 1, linha - 1, 0) * c
+ raster.getSample(larg, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(col + 1, linha, 0) * f
+ raster.getSample(larg, 0, 0) * g + raster.getSample(col, 0, 0) * h + raster.getSample(col + 1, 0, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(larg, linha - 1, 0) * j + raster.getSample(col, linha - 1, 0) * l + raster.getSample(col + 1, linha - 1, 0) * m
+ raster.getSample(larg, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(col + 1, linha, 0) * p
+ raster.getSample(larg, 0, 0) * q + raster.getSample(col, 0, 0) * r + raster.getSample(col + 1, 0, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
}// fim do col=0
else if (col == larg) {
pixelout = (raster.getSample(col - 1, linha - 1, 0) * a + raster.getSample(col, linha - 1, 0) * b + raster.getSample(0, linha - 1, 0) * c
+ raster.getSample(col - 1, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(0, linha, 0) * f
+ raster.getSample(col - 1, 0, 0) * g + raster.getSample(col, 0, 0) * h + raster.getSample(0, 0, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(col - 1, linha - 1, 0) * j + raster.getSample(col, linha - 1, 0) * l + raster.getSample(0, linha - 1, 0) * m
+ raster.getSample(col - 1, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(0, linha, 0) * p
+ raster.getSample(col - 1, 0, 0) * q + raster.getSample(col, 0, 0) * r + raster.getSample(0, 0, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
} // fim do col=larg
else { // linha=ultima col =qualquer
pixelout = (raster.getSample(col - 1, linha - 1, 0) * a + raster.getSample(col, linha - 1, 0) * b + raster.getSample(col + 1, linha - 1, 0) * c
+ raster.getSample(col - 1, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(col + 1, linha, 0) * f
+ raster.getSample(col - 1, 0, 0) * g + raster.getSample(col, 0, 0) * h + raster.getSample(col + 1, 0, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(col - 1, linha - 1, 0) * j + raster.getSample(col, linha - 1, 0) * l + raster.getSample(col + 1, linha - 1, 0) * m
+ raster.getSample(col - 1, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(col + 1, linha, 0) * p
+ raster.getSample(col - 1, 0, 0) * q + raster.getSample(col, 0, 0) * r + raster.getSample(col + 1, 0, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
}//fim do else col!= larg
}// fim do linha = alt
else if (col == 0) { // linha=qualquer col=0
pixelout = (raster.getSample(larg, linha - 1, 0) * a + raster.getSample(col, linha - 1, 0) * b + raster.getSample(col + 1, linha - 1, 0) * c
+ raster.getSample(larg, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(col + 1, linha, 0) * f
+ raster.getSample(larg, linha + 1, 0) * g + raster.getSample(col, linha + 1, 0) * h + raster.getSample(col + 1, linha + 1, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(larg, linha - 1, 0) * j + raster.getSample(col, linha - 1, 0) * l + raster.getSample(col + 1, linha - 1, 0) * m
+ raster.getSample(larg, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(col + 1, linha, 0) * p
+ raster.getSample(larg, linha + 1, 0) * q + raster.getSample(col, linha + 1, 0) * r + raster.getSample(col + 1, linha + 1, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
} // fim do col =0
else if (col == larg) {// linha qualquer col= ult
pixelout = (raster.getSample(col - 1, linha - 1, 0) * a + raster.getSample(col, linha - 1, 0) * b + raster.getSample(0, linha - 1, 0) * c
+ raster.getSample(col - 1, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(0, linha, 0) * f
+ raster.getSample(col - 1, linha + 1, 0) * g + raster.getSample(col, linha + 1, 0) * h + raster.getSample(0, linha + 1, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(col - 1, linha - 1, 0) * j + raster.getSample(col, linha - 1, 0) * l + raster.getSample(0, linha - 1, 0) * m
+ raster.getSample(col - 1, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(0, linha, 0) * p
+ raster.getSample(col - 1, linha + 1, 0) * q + raster.getSample(col, linha + 1, 0) * r + raster.getSample(0, linha + 1, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
}// fim do col==larg
else { // linha=ultima col =qualquer
pixelout = (raster.getSample(col - 1, linha - 1, 0) * a + raster.getSample(col, linha - 1, 0) * b + raster.getSample(col + 1, linha - 1, 0) * c
+ raster.getSample(col - 1, linha, 0) * d + raster.getSample(col, linha, 0) * e + raster.getSample(col + 1, linha, 0) * f
+ raster.getSample(col - 1, linha + 1, 0) * g + raster.getSample(col, linha + 1, 0) * h + raster.getSample(col + 1, linha + 1, 0) * i) / div;
if (pixelout < 0) {
pixelout = 0;
}
pixelout2 = (raster.getSample(col - 1, linha - 1, 0) * j + raster.getSample(col, linha - 1, 0) * l + raster.getSample(col + 1, linha - 1, 0) * m
+ raster.getSample(col - 1, linha, 0) * n + raster.getSample(col, linha, 0) * o + raster.getSample(col + 1, linha, 0) * p
+ raster.getSample(col - 1, linha + 1, 0) * q + raster.getSample(col, linha + 1, 0) * r + raster.getSample(col + 1, linha + 1, 0) * s) / div2;
if (pixelout2 < 0) {
pixelout2 = 0;
}
pixelfinal = pixelout + pixelout2;
rasterout.setSample(col, linha, 0, pixelfinal);
}
}
}
//Abre a imagem selecionada e coloca no buffer de memória
ImageIcon icon = new ImageIcon(imagemout);
label_resultado.setIcon(icon);
try {
ImageIO.write((RenderedImage) ((ImageIcon) label_resultado.getIcon()).getImage(), "jpg", new File("imagem.jpg"));
} catch (IOException ex) {
Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
}
//Primeira Suavização
try {
ImagePlus iplus = ij.IJ.openImage("imagem.jpg");
if (iplus != null) {
img = iplus.getProcessor();
exibeImagemProcessada(img, label_resultado);
}
imgSaida = img.duplicate();
float[] Template = {
-1, -1, -1,
-1, 4, -1,
0, -1, 0,};
imgSaida = converter(imgSaida, 8);
convolucao(imgSaida, Template);
//Suavizacao
imgSaida.smooth();
exibeImagemProcessada(imgSaida, label_resultado);
ImageIO.write((RenderedImage) ((ImageIcon) label_resultado.getIcon()).getImage(), "jpg", new File("imagem2.jpg"));
btn_salvar.setEnabled(true);
} catch (Exception ex) {
Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
}
// //Emissão de Alerta Caso tenha detectado movimento ou intrusão no ambiente
try {
AudioClip clip = Applet.newAudioClip(new File("alerta.wav").toURL());
clip.play();
//clip.loop();
} catch (MalformedURLException ex) {
//JOptionPane.showMessageDialog(rootPane, "Erro. Verifique o diretorio de sons");
ex.printStackTrace();
}
} catch (Exception ex) {
Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
JOptionPane.showMessageDialog(rootPane, "Carregue o cenário e a imagem com o movimento!");
}
//Janela de Alerta de Detecção de Movimento
Icon figura = new ImageIcon(getToolkit().createImage(getClass().getResource("imgalerta.gif")));
JOptionPane.showMessageDialog(null, "Detecção de Movimento encontrada no Ambiente!!!", "Alerta", JOptionPane.WARNING_MESSAGE, figura);
Não estou vendo onde você chama o percentualBrancos nesse código.
Seria só chamar:
double percentual = percentualBrancos(imagemSub);
Rapaz, deixa eu te explicar uma coisa: Filtros passa alta e passa baixa são utilizados para aumentar o diminuir regiões de alta frequência em uma imagem, ou seja para atenuar ou aumentar a nitidez de uma imagem.
Se você quer implementar um mecanismo de detecção de movimento você vai usar aritmética e para ser mais específico o detector de movimento mais simples é o que usa a subtração de imagens. O problema é que a subtração não impõe uma maneira de você setar a sensibilidade da detecção. Saber se tudo é preto ou branco é o limite do intervalo entende? Numa situação real não se encontra imagens iguais pela iluminação ser uma variável e não uma constante. Não haverá imagens completamente pretas ou brancas.
A análise por histograma deve ser a melhor opção.
Dá uma olhada nesse artigo
Fiz um código para analizar em cada pixel a quantidade de RGB e fazer uma comparação se o pixel em questao tem as 3 cores iguais, para determinar que a cor é o cinza, mas sempre, em qualquer pixel que ele analisa sempre ta dando igual as cores, ou seja, ta me dizendo que tudo é cinza em qualquer imagem que eu selecione x( alguem ja passou por isso?
[code]JFileChooser Destino = new JFileChooser();
Destino.setDialogTitle(“Selecione a imagem para análise…”);
Destino.showOpenDialog(this);
int Cont = 0;
ImageIcon Imagem = new ImageIcon(Destino.getSelectedFile().getAbsolutePath());
IMG = new BufferedImage(Imagem.getIconWidth(), Imagem.getIconHeight(), 1);
for(int i = 0; i < IMG.getWidth(); i++){
for(int j = 0; j < IMG.getHeight(); j++){
Pixel = new Color(IMG.getRGB(i, j));
if((Pixel.getRed() == Pixel.getGreen()) && (Pixel.getRed() == Pixel.getBlue())){
Cont++;
}
}
}
this.Percentual = Cont;
JOptionPane.showMessageDialog(null, this.Percentual + " é a quantidade de tons de cinza que tem na imagem");[/code]
up