1

I have an image with four squares red, green, blue and yellow. I need to get the rgb values of each squares. I'm able to get the rgb of the whole image, but i want it for a specific section.

  • The image which i am going to get will be from the camera and stored onto the SDCard
mn0102
  • 839
  • 1
  • 12
  • 25
  • Have you tried cropping the specific area and detecting color? – Karthik Balakrishnan May 20 '13 at 05:24
  • you need to show areas in the image and show their rgb values, am i right? – İsmet Alkan May 20 '13 at 05:25
  • @IsmetAlkan i just want to get all the four sections rgb specifically and not* going to display it anywhere – mn0102 May 20 '13 at 05:31
  • @Torcellite Not cropped but i tried providing images containing only R, only G and only B... and i got the right rgbs for them.. Basically i'm getting the average rgb color of the image. – mn0102 May 20 '13 at 05:33
  • @aditya Are the squares always going to be in the same positions? I mean the colors of the squares can change but their respective positions does not alter, if so, your best bet would be to crop and detect. – Karthik Balakrishnan May 20 '13 at 06:52
  • @Torcellite actually cropping and detecting would ask me to use the OpenCV library... I'm looking for a way if this task can be accomplish using simple android APIs like Bitmap and BitmapFactory – mn0102 May 20 '13 at 07:15
  • @aditya Do check out the answer. – Karthik Balakrishnan May 20 '13 at 07:19
  • I'm very new to Opencv & trying to find the red circular color present in an image.How do i achieve it – Pranesh Sahu Sep 15 '18 at 10:24

3 Answers3

2

I don't know if I understand you exactly, but here it comes.

You need to create BufferedImage object to get RGB value:

File f = new File(yourFilePath);
BufferedImage img = ImageIO.read(f);

You can get RGB Color values from the image from then. You have 4 squares; to check their RGB values, you can check the corner pixels' RGB values:

Color leftTop = new Color(img.getRGB(0, 0));
Color rightTop = new Color(img.getRGB(img.getWidth - 1, 0));
Color leftBottom = new Color(img.getRGB(0, img.getHeight - 1));
Color rightBottom = new Color(img.getRGB(img.getWidth - 1, img.getHeight - 1));

After that it's easy to get red, green and blue values individually:

int red = leftTop.getRed();
int green = leftTop.getGreen();
int blue = leftTop.getBlue();

EDIT: I'm really sorry, I didn't see it's for Android. As you said, Android doesn't have ImageIO class. To accomplish the task in Android, first initialize the image:

Bitmap img = BitmapFactory.decodeFile(yourFilePath);

From then the operation is pretty much the same:

int leftTop = img.getPixel(0, 0);
...

int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);
İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64
  • Thanks for the support... i am trying this out.. i'll just get back to you then.. – mn0102 May 20 '13 at 05:51
  • BufferedImage & ImageIO is not supported in Android SDK... Now i am looking for if Bitmap class could do this for me. – mn0102 May 20 '13 at 06:38
  • you changed my approach completely... I am getting the top and bottom corners pixels rgb... Lets see if things will workout with this way.. M not accepting the answer for now but surely a +1 for you.. Thanks for the help mate.. – mn0102 May 20 '13 at 09:32
1

Use this to crop your image.

Now to detect the color of the image take a pixel from the square and detect it's color with this.

After finding the RGB value use a simple conditional statement to see if the square is red blue or green.

Community
  • 1
  • 1
Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
0

I got it this way

int topLeftIndex = squareImage.getPixel(0, 0);
int R1 = (topLeftIndex >> 16) & 0xff;
int G1 = (topLeftIndex >> 8) & 0xff;
int B1  = topLeftIndex  & 0xff;

and same way with

int bottomLeftIndex=squareImage.getPixel(0, picHeight - 1);
int topRightIndex=squareImage.getPixel(picWidth -1 , 0);
int bottomRightIndex=squareImage.getPixel(picWidth -1, picHieght - 1);
mn0102
  • 839
  • 1
  • 12
  • 25