Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image converter java

i'm working with image processing, and i have a question.

I want read an image from project, and convert the image to gray. I'm currently trying to do conversion with the function rgb2gray, but still not working.

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class ImageTesting extends Component {
    private static int[] pixel;
    private static BufferedImage b;

    BufferedImage image;

    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }

    public ImageTesting() {
        try {
            image = ImageIO.read(new File("teste.jpg"));
        } catch (IOException e) {
        }
    }

    public Dimension getPreferredSize() {
        if (image == null) {
            return new Dimension(400, 400);
        } else {
            return new Dimension(image.getWidth(null), image.getHeight(null));
        }
    }

    public static BufferedImage rgb2gray(BufferedImage bi) {
        int heightLimit = bi.getHeight();
        int widthLimit = bi.getTileWidth();
        BufferedImage converted = new BufferedImage(widthLimit, heightLimit, BufferedImage.TYPE_BYTE_GRAY);
        for (int height = 0; height < heightLimit; height++) {
            for (int width = 0; width < widthLimit; width++) {
                Color c = new Color(bi.getRGB(width, height) & 0x00fffff);
                int newRed = (int) ((0.2989f * c.getRed()) * 2);// 0.2989f//multiplicr po 2
                int newGreen = (int) ((0.5870f * c.getGreen()) * 2);// 0.5870f
                int newBlue = (int) ((0.1140f * c.getBlue()) * 2);
                int roOffset = newRed + newGreen + newBlue;
                converted.setRGB(width, height, roOffset);
            }
        }
        return converted;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        JFrame f = new JFrame("Load Image Sample");
        JFrame g = new JFrame("Image RGB");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        g.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.add(new ImageTesting());
        f.pack();
        f.setVisible(true);
        g.add(new ImageTesting());
        rgb2gray(b);
    }
}

When I run the program,these are the errors that appear.

If anyone could help me, i apreciate. Thanks

Edit: I managed to solve this problem,but now another question came up. To continue my work, i want to find the most 10 brilhants points in the resultant image, and return another image with black color in the index's that have the value 0, and white color in the index's that have value 1,but at this point i don't understand the best way to work out the steps.

like image 927
FCoelho Avatar asked Sep 20 '26 08:09

FCoelho


1 Answers

It seems like there's something wrong with the main() method, isn't it? You create two completely identical JFrame instances, then add Imagetesting components that display the original image. And when running rgb2gray at the end, the result is sent nowhere.

like image 184
Dmitriy Merkushov Avatar answered Sep 22 '26 23:09

Dmitriy Merkushov