JAVA how to get rgb values from an image

  • Java
  • Thread starter atie
  • Start date
  • Tags
    Image Java
In summary, the banana image is encoded in a black and white format with a maximum length of 42 bytes.
  • #1
atie
3
0
hello,

i'm kinda new in this programming thingy so i hope you guys can help me.

i wanted to code a program that can identify whether a banana is ripe or not. basically, i have two image of banana; one yellow in color and another one is green. so I'm thinking about using AI technique (backpropagation maybe) to identify the banana ripeness.

i search for some article on how to do it and most of them start with loading the image and extract the rgb value. this is what I've done. its not much because i don't know what to do next. can anybody help?

import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class banana
{
public static void main(String [] args)throws Exception
{
//get image
File imageFile= new File("yellowbanana.jpg");
BufferedImage image = ImageIO.read(imageFile);

//if file not exist
if(!(imageFile.exists())){
System.out.println("File NOT exists");
System.exit(0);}

int w = image.getWidth();
int h = image.getHeight();

System.out.println("w=" + w + ", h=" + h);

next i should get the pixel of the image. is it correct?
 
Technology news on Phys.org
  • #2
atie said:
hello,

i'm kinda new in this programming thingy so i hope you guys can help me.

i wanted to code a program that can identify whether a banana is ripe or not. basically, i have two image of banana; one yellow in color and another one is green. so I'm thinking about using AI technique (backpropagation maybe) to identify the banana ripeness.

i search for some article on how to do it and most of them start with loading the image and extract the rgb value. this is what I've done. its not much because i don't know what to do next. can anybody help?

import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class banana
{
public static void main(String [] args)throws Exception
{
//get image
File imageFile= new File("yellowbanana.jpg");
BufferedImage image = ImageIO.read(imageFile);

//if file not exist
if(!(imageFile.exists())){
System.out.println("File NOT exists");
System.exit(0);}

int w = image.getWidth();
int h = image.getHeight();

System.out.println("w=" + w + ", h=" + h);

next i should get the pixel of the image. is it correct?

Unless the image is very small, it probably won't consist of a pixel, but will instead consist of w * h pixels. You also need to know how the image is encoded - its color model, which indicates how many bits are used for red, green, and blue, and if there are alpha bits.
 
  • #3
thnk you for ur reply mark..

the banana image i have is 400pixels height and 400pixels width..

how may i know how the image is encoded? i don't understand that part.

oh, btw, i change my coding. i convert the banana image into binary. is changing the image to binary retain the color values?
 
  • #4
Since you are using the BufferedImage class, you should be looking at the documentation for it (see http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html ).

You can find out how the data is encoded by using the getType method on BufferedImage. This method returns one of the constants that describes the color encoding scheme.
 
Last edited by a moderator:
  • #5
hye.. i managed to convert image into grayscale and into binary... however the binary string i got is too long (like 42 pagesssss!). how can i make it shorter? do i need to resize the picture's pixel or something?
 

Related to JAVA how to get rgb values from an image

What is JAVA?

JAVA is a programming language commonly used for developing web applications, desktop applications, and mobile applications. It is an object-oriented language and is known for its platform independence and robustness.

How do I get RGB values from an image in JAVA?

To get RGB values from an image in JAVA, you can use the getRGB() method from the BufferedImage class. This method returns an integer value that represents the combined RGB values of a specific pixel in the image.

Can I manipulate the RGB values of an image in JAVA?

Yes, you can manipulate the RGB values of an image in JAVA by using the setRGB() method from the BufferedImage class. This method allows you to change the RGB values of a specific pixel in the image.

How can I convert RGB values to other color models in JAVA?

To convert RGB values to other color models in JAVA, you can use the Color class. This class provides various methods for converting between different color models, such as RGB, CMYK, HSL, and HSV.

Is there a library or API available for working with images in JAVA?

Yes, there are several libraries and APIs available for working with images in JAVA, such as the Java Advanced Imaging (JAI) API, Java Image IO API, and ImageJ. These libraries provide various functions for image processing, manipulation, and conversion.

Similar threads

  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top