Use Java to identify lines in a graph

I am working on an idea in which I need to identify lines in a JPG or PNG file. The PNG file contains one graph of one value - one combination of x, y. For example, the graph looks like Y = mx + c. I would like to identify the line in the graph - if I can indicate the position of the pixel in the frame, I believe that I can return the values ​​(x, y) that were used to plot the graph. Assumptions here - I know the scale ie 1 pixel = which unit is Y? Can someone help me write code that will identify pixels of a certain color in a single PNG file?

EDIT

Let's look at an example to make it clear. Suppose 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 the jfreechart type of charting tool and create a chart, it works like a straight line.

Thus, input is a data set, and output is a .PNG file (using jchart or jfreechart) containing this line graph for Y values.

Question: if we reverse the flow, we can develop one program that takes a PNG file (containing a graph) and returns a data set to me.

Problem context - I want to store PNG files in my application, not the original dataset. But, given the PNG file, I can return my data set.

+4
source share
4 answers

I'm a little confused as to whether your problem is simply determining the color of the pixels in the image or if the problem is the math of what you are trying to do.

For the first, do 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 on a chart you just want to return a data set (i.e. do not get an equation from this data), then you essentially iterate over each X position and find the Y position, where there is a color pixel that the chart draws the program uses. If the graph has axes, smoothing, etc., then the task will be more difficult.

The task of obtaining an equation from the data is potentially much more complicated, but you can start by checking some suspicious formulas, such as y = mx + c, as you mentioned. For example, you can check the difference between each Y position for the last; if this difference is always the same, then you have a straight line graph (and at this point the derivation of the formula should be trivial).

For testing other equations, this helps to find out a little calculus. Then the starting point is to see if the differences in the differences of the derivative correspond to the equation in question. (As an example, if the equation y = ax ^ 2 + c, then with each increase in X, the increase in Y will itself increase by 2a.)

+5
source

As a rule, on the computer monitor you do not have a central point (0,0) , since this point is defined as the upper left corner of the screen. Therefore, if we look at the function f(x)=ax+b , then the parameter b is defined as the value of y for the function at x=0 , which means the left border of the screen. Therefore, it is important to determine exactly what you are compensating for.

To find the slope, just take the center point on the screen that you know, you have a function point there, go x pixels left or right, find the delta of height y and y/x - this is the slope of the function or parameter a in the above function.

0
source

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

0
source

I'm not sure where you reached your project, but I also needed to detect lines in the image. Here is what helped me. http://www.music.mcgill.ca/~cmckay/software/musictech/ScoreReader/HorizontalLineDetection.html

0
source

All Articles