Foros del Web » Programación para mayores de 30 ;) » Java »

Filtro de laplace

Estas en el tema de Filtro de laplace en el foro de Java en Foros del Web. Hola tengo problemas con este programa que es aplicar el filtro de laplace a una imagen pero me marca un error y no se como ...
  #1 (permalink)  
Antiguo 13/06/2012, 21:26
 
Fecha de Ingreso: junio-2012
Mensajes: 1
Antigüedad: 11 años, 10 meses
Puntos: 0
Exclamación Filtro de laplace

Hola tengo problemas con este programa que es aplicar el filtro de laplace a una imagen pero me marca un error y no se como arreglarlo espero me puedan ayudar

Son dos clases

Les dejo el main donde me marca el error y despues anexo la otra calse donde estan los constructores:


MAIN:

package laplacefilter2;

/**
*
* @author Alano
*/
/************************************************** ***********************
*
*
*
* Cargar una Imagen y aplicar el filtro de lapace
*
* [ -1 -1 -1 ]
* [ -1 8 -1 ]
* [ -1 -1 -1 ]
*
*
*
*
************************************************** ***********************/

import java.awt.Color;

public class LaplaceFilter2 {

public static void main(String[] args) {
Picture pic1 = new Picture(); // original <---------------AKI ME MARCA EL ERROR
Picture pic2 = new Picture(args[0]); // filtered
int width = pic1.width();
int height = pic1.height();


pic1.show();
pic2.show();

for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
Color c00 = pic1.get(x-1, y-1);
Color c01 = pic1.get(x-1, y );
Color c02 = pic1.get(x-1, y+1);
Color c10 = pic1.get(x , y-1);
Color c11 = pic1.get(x , y );
Color c12 = pic1.get(x , y+1);
Color c20 = pic1.get(x+1, y-1);
Color c21 = pic1.get(x+1, y );
Color c22 = pic1.get(x+1, y+1);
int r = -c00.getRed() - c01.getRed() - c02.getRed() +
-c10.getRed() + 8*c11.getRed() - c12.getRed() +
-c20.getRed() - c21.getRed() - c22.getRed();
int g = -c00.getGreen() - c01.getGreen() - c02.getGreen() +
-c10.getGreen() + 8*c11.getGreen() - c12.getGreen() +
-c20.getGreen() - c21.getGreen() - c22.getGreen();
int b = -c00.getBlue() - c01.getBlue() - c02.getBlue() +
-c10.getBlue() + 8*c11.getBlue() - c12.getBlue() +
-c20.getBlue() - c21.getBlue() - c22.getBlue();
r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));
Color c = new Color(r, g, b);
pic2.set(x, y, c);
}
}
pic2.show();
}



}






CLASE DE CONSTRUCTORES!!




package laplacefilter2;


import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;


public final class Picture implements ActionListener {
private BufferedImage image; // Imagen planificada
private JFrame frame; // En vista de pantalla
private String filename; // Nombre de archivo
private boolean isOriginUpperLeft = true; //Ubicacion de origen
private int width, height; // width and height

/**
* Create a blank w-by-h picture, where each pixel is black.
*/
public Picture(int w, int h) {
width = w;
height = h;
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// set to TYPE_INT_ARGB to support transparency
filename = w + "-por-" + h;
}

/**
* Create a picture by reading in a .png, .gif, or .jpg from
* the given filename or URL name.
*/
public Picture(String filename) {
this.filename = filename;
try {
// try to read from file in working directory
File file = new File(filename);
if (file.isFile()) {
image = ImageIO.read(file);
}

// now try to read from file in same directory as this .class file
else {
URL url = getClass().getResource(filename);
if (url == null) { url = new URL(filename); }
image = ImageIO.read(url);
}
width = image.getWidth(null);
height = image.getHeight(null);
}
catch (IOException e) {
// e.printStackTrace();
throw new RuntimeException("No se pudo abrir el archivo: " + filename);
}
}

/**
* Create a picture by reading in a .png, .gif, or .jpg from a File.
*/
public Picture(File file) {
try { image = ImageIO.read(file); }
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("No se pudo abrir el archivo: " + file);
}
if (image == null) {
throw new RuntimeException("Archivo invalido " + file);
}
}

/**
* Return a JLabel containing this Picture, for embedding in a JPanel,
* JFrame or other GUI widget.
*/
public JLabel getJLabel() {
if (image == null) { return null; } // no image available
ImageIcon icon = new ImageIcon(image);
return new JLabel(icon);
}

/**
* Set the origin to be the upper left pixel.
*/
public void setOriginUpperLeft() {
isOriginUpperLeft = true;
}

/**
* Set the origin to be the lower left pixel.
*/
public void setOriginLowerLeft() {
isOriginUpperLeft = false;
}

/**
* Display the picture in a window on the screen.
*/
public void show() {

// create the GUI for viewing the image if needed
if (frame == null) {
frame = new JFrame();

JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem menuItem1 = new JMenuItem(" Save... ");
menuItem1.addActionListener(this);
menuItem1.setAccelerator(KeyStroke.getKeyStroke(Ke yEvent.VK_S,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask ()));
menu.add(menuItem1);
frame.setJMenuBar(menuBar);



frame.setContentPane(getJLabel());
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_C LOSE);
frame.setTitle(filename);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}

// draw
frame.repaint();
}

/**
* Return the height of the picture in pixels.
*/
public int height() {
return height;
}

/**
* Return the width of the picture in pixels.
*/
public int width() {
return width;
}

/**
* Return the color of pixel (i, j).
*/
public Color get(int i, int j) {
if (isOriginUpperLeft) return new Color(image.getRGB(i, j));
else return new Color(image.getRGB(i, height - j - 1));
}

/**
* Set the color of pixel (i, j) to c.
*/
public void set(int i, int j, Color c) {
if (c == null) { throw new RuntimeException("can't set Color to null"); }
if (isOriginUpperLeft) image.setRGB(i, j, c.getRGB());
else image.setRGB(i, height - j - 1, c.getRGB());
}

/**
* Is this Picture equal to obj?
*/
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null) return false;
if (obj.getClass() != this.getClass()) return false;
Picture that = (Picture) obj;
if (this.width() != that.width()) return false;
if (this.height() != that.height()) return false;
for (int x = 0; x < width(); x++)
for (int y = 0; y < height(); y++)
if (!this.get(x, y).equals(that.get(x, y))) return false;
return true;
}


/**
* Save the picture to a file in a standard image format.
* The filetype must be .png or .jpg.
*/
public void save(String name) {
save(new File(name));
}

/**
* Save the picture to a file in a standard image format.
*/
public void save(File file) {
this.filename = file.getName();
if (frame != null) { frame.setTitle(filename); }
String suffix = filename.substring(filename.lastIndexOf('.') + 1);
suffix = suffix.toLowerCase();
if (suffix.equals("jpg") || suffix.equals("png")) {
try { ImageIO.write(image, suffix, file); }
catch (IOException e) { e.printStackTrace(); }
}
else {
System.out.println("Error: filename must end in .jpg or .png");
}
}

/**
* Opens a save dialog box when the user selects "Save As" from the menu.
*/
public void actionPerformed(ActionEvent e) {
FileDialog chooser = new FileDialog(frame,
"Use a .png or .jpg extension", FileDialog.SAVE);
chooser.setVisible(true);
if (chooser.getFile() != null) {
save(chooser.getDirectory() + File.separator + chooser.getFile());
}
}


}






ESPERO ME PUEDAN AYUDAR ESTA HECHO EN NETBEANS!!!!
  #2 (permalink)  
Antiguo 14/06/2012, 02:08
Avatar de Xerelo  
Fecha de Ingreso: mayo-2009
Mensajes: 2.175
Antigüedad: 15 años
Puntos: 306
Respuesta: Filtro de laplace

¿Y el error es? Los mensajes de error están para algo.

De todas formas, no has creado el constructor Picture() sin parámetros.
__________________
Cada vez que solucionas los problemas de alguien que no se esfuerza, piensa en que el día de mañana puede llegar a ser tu compañero de trabajo, o peor, tu jefe.

Etiquetas: programa
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:23.