Jump to content

JAVA + backpropagation how to get rgb value from image

- - - - -

  • Please log in to reply
2 replies to this topic

#1
atie

atie

    Newbie

  • Members
  • Pip
  • 1 posts
hye

i'm new in programming. i wanted to code a program that can identify which banana is ripe. basically i have 2 image of banana, one yellow in color and another one is green.

first, i want to get the image. then i need to get the rgb values from the image. next, i have to get the pixel but i stuck there. can anyone help? i have here some code i wrote but its not much because i dunno how to proceed. please 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);

Edited by Roger, 16 December 2011 - 08:29 AM.
moved to correct forum + added code tags


#2
bleedy3

bleedy3

    Newbie

  • Members
  • PipPip
  • 26 posts
In order to get the individual RGB color values from a pixel in a BufferedImage, you will have to use the getRGB(int x, int y) method where x & y are the location of the pixel within the Image. However, since this method return the RGB values as a 32-bit integer, you will have to convert them to integer using binary arithmetic.

Here is the code:

//Arrays to store the red, green, and blue values in
int [] redValue = new int[image.getWidth() * image.getHeight()];
int [] greenValue = new int[image.getWidth() * image.getHeight()];
int [] blueValue = new int[image.getWidth() * image.getHeight()];
int index = 0;


for(int x = 0; x < image.getWidth(); x++)
{
for(int y = 0; y < image.getHeight(); y++)
{
//Returns the rgb value of the specified pixel as a 32-bit binary
int rgb = buttonImage.getRGB(x, y);

//Convert rgb from binary to integer in order to get pixel's individual red, green, and blue values
//then assign them an int value
int red = (rgb >> 16) & 0x000000FF;
int green = (rgb >> 8 ) & 0x000000FF;
int blue = (rgb) & 0x000000FF;

//Assign the each of the arrays to the red, green, and blue values of the image respectively
redValue[index] = red;
greenValue[index] = green;
blueValue[index] = blue;
//Increment the index by 1
index++;
}
}

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think you can put the rgb-int into a Color:
[B]int rgb = buttonImage.getRGB(x,y);
Color color = new Color(rgb);
color.getRed();
color.getGreen();
color.getBlue();[/B]





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users