tags:

views:

459

answers:

5

Hi,

I am working on an idea wherein I have to identify lines in a JPG or PNG file. The PNG file contains one graph of a single value - one x,y combination. For example, the graph looks like Y = mx+c. I would like to identify the line in the graph - if I can indentify the position of the pixel in the frame, I believe I can get back the values (x,y) which were used to plot the graph. The assumptions here is - I know the scale i.e 1 pixel = what unit of Y ? Can somebody please help me to write a code which will identify the pixels of a specific color in one PNG file?

EDIT

Lets take an example to make it clear. Lets say I have a set of data values X and Y like this -

X = 1, Y = 10
X = 2, Y = 20
X = 3, Y = 30
X = 4, Y = 40
X = 5, Y = 50
X = 6, Y = 60

In this case, if I use jfreechart type of charting tool and make a chart, it tools like a straight line.

So the input is the dataset and output is the .PNG file (using jchart or jfreechart) containing this line graph for Y values.

Question is - if we reverse the flow, can we develop one program which will take the PNG file (that contains the graph) and give me back the dataset.

The context of the problem is -- I want to store the PNG files in my application and not the raw dataset. But given a PNG file, I should be able to get back my dataset.

A: 

Generally, on a computer monitor you don't have a central (0,0) point, as that point is defined to be the top left corner of the screen. So if we are looking at a function f(x)=ax+b then b parameter is defined to be the y value of the function at x=0, meaning the left border of the screen. So it's important to define exactly what you are offsetting from.

To find the slope, just take some central point on the screen you know you have a point of the function there, go x pixels to the left or the right, find the y height delta, and y/x is the function slope, or parameter a in the function aforementioned.

Yuval A
Huh? Of course there is b -- it is the y-coordinate of the intersection of the line with the left border of the image.
Svante
@Harleqin, thanks, fixing my answer.
Yuval A
A: 

To answer the part about getting the "specific color in one PNG file", you should use the Robot class as in this RobotTests Java class:

   public void testOpenPNGFile() throws QTException, AWTException {

       QTFile file = new QTFile("data/pureblue.png");
       FileOpener.openFile(file);
       Robot r = new Robot();
       r.delay(5000);
       Color expected = new Color(0, 0, 255);
       Color actual = r.getPixelColor(50, 100);
       assertEquals(expected, actual);

    }
VonC
I think this is a very convoluted way of reading the pixel colour of an image.
Neil Coffey
Sure is, Neil. Your approach is much straightforward. +1
VonC
A: 

Do I read that correctly: you have an image of a single line on an otherwise blank canvas? If so, you can just look down the leftmost pixel column and find the pixel that is different, then take a column a bit to the right and find the pixel there. Now you have two points, and generating a linear function from that is trivial. As unit, why not use pixels?

Svante
Thanks Harleqin, You got it right. I have never used any Java Imaging code, so was wondering how to code it or right classes for it?
Shamik
+3  A: 

I'm a bit confused as to whether your problem is simply determining the colour of pixels in an image, or if the problem is the mathematics of what you're trying to do.

For the former, do something such as the following:

    BufferedImage bimg = ImageIO.read(new File("whatever.png"));

    // get the colour of the pixel at position (x, y)
    int col = bimg.getRGB(x, y);

    // decode red, green and blue components of colour if necessary
    int r = (col >> 16) & 0xff;
    int g = (col >> 8) & 0xff;
    int b = col & 0xff;

If from the graph you just want to get back the dataset (i.e. not derive an equation from that data), then you essentially loop through each X position and find the Y position where there's a pixel of the colour that the graph plotting program uses. If the graph has axes, antialiasing etc, then the task will be more complex.

The task of deriving an equation from the data is potentially much more complex, but you can start by checking for certain suspected formulae such as y = mx + c as you mention. For example, you can loop through checking the difference between each Y position for the last; if that difference is always the same, then you've got a straight line graph (and at that point, deriving the formula should be trivial).

For testing for other equations, it helps to know a bit of calculus. Then, a starting point is to see if the differences in the differences match the derivative for the equation in question. (Just as an example, if the equation is y = ax^2 + c, then for every increase in X, the increase in Y will itself increase by 2a.)

Neil Coffey
A: 

hi, i am not sure where you reached with your project, but i needed to detect lines in an image as well. Here is what helped me. http://www.music.mcgill.ca/~cmckay/software/musictech/ScoreReader/HorizontalLineDetection.html