How to recognize colors/hex values with java?

BlueAcolyte

Platinum Member
Nov 19, 2007
2,793
2
0
I just got a project from a friend.

He needs to take pictures of small strips of copper then measure their length. It has to be within a certain range.

I am using ImageJ for the project although if anyone has any alternatives, please suggest them. ImageJ is written in java, which I don't really know about, so I'm just picking it up as I go.

(we are assuming everything is only along the x axis)
I wrote a small macro that makes images black and white (binary colors) so the edges are very well defined.
So now I would want to have the program recognize the furthest left black pixel and then draw a horizontal line across to the furthest right black pixel.

Later when I figure out the inch-to-pixel mapping I would just want to find the furthest left black pixel and draw a few lines of certain lengths to determine that it is the right length.

It's not super-urgent, but advice would be appreciated.
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Well, not familiar with the ImageJ library but you should probably be able to use a class like BufferedImage which has the method getRGB(x,y) which will allow you to do your algorithm for finding the black pixels.

So basically you'd be starting at the lowest x value going across sampling each pixel and determining if it is "black enough" to mark it as the beginning of the edge, then continue across sampling pixels until one becomes "not black enough".

The return value of getRGB is an int which has the data encoded like this: AARRGGBB. The leftmost 8 bits represent alpha channel, then 8 bits for red value. 8 bits for green and finally 8 bits for blue. You can separate them out using bitwise operations like:
int red = pixel >> 16 & 255
to give you just the red value and so on...

 

BlueAcolyte

Platinum Member
Nov 19, 2007
2,793
2
0
So something like:

class BufferedImage {
public static void main(String[] args) {
getRGB(0,0)
}
}

Edit: my example is junk. I'll learn java and come back later.

I also found pixelgrabber, that could be useful.
 
Sep 29, 2004
18,656
68
91
I have a basic question. What file format are you dealing with?

If it is lossless like JPG you might as well give up now since your answers will be less accurate.

Use a lossless format mike PNG.

Once you have a file format selected, you can use the BufferedImage class to read/wrtie PNG files. I'm not sure if Java 1.4 will read PNG files though. I'm fairly certain that 1.5 does though.
 

BlueAcolyte

Platinum Member
Nov 19, 2007
2,793
2
0
Err, it's coming from a camera, so it's JPG by default. I could convert them I guess. ImageJ already has an edge detector built in but I've been mucking around with javap and the source code and I can't find the right class file.

Edit: Ok, so looking around some more, the edge detector uses 3x3 squares to detect changes in color, and the imageprocessor superclass.

If only I could actually understand anything I'm talking about.