Note that there are some explanatory texts on larger screens.

plurals
  1. POImage converter java
    primarykey
    data
    text
    <p>i'm working with image processing, and i have a question.</p> <p>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.</p> <pre><code>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 &lt; heightLimit; height++) { for (int width = 0; width &lt; widthLimit; width++) { Color c = new Color(bi.getRGB(width, height) &amp; 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); } } </code></pre> <p>When I run the program,these are the errors that appear.</p> <p>If anyone could help me, i apreciate. Thanks</p> <p><strong>Edit:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload