Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, developers!
I have a function graph (bmp pictute). I need a way to get F(x) values from this graph.
The problem is looking like very easy one: X value is in range from 0 to 1 and Y value ranges from 0 to 1 and the graph is a curve drawn by hand.
Is there any way to get F(x) value in real time by few lines of code without inventing a cycle?
Posted
Updated 17-Mar-13 8:32am
v2
Comments
Marco Bertschi 17-Mar-13 14:53pm    
If the graph is only available as a BMP - nope. You'd have to do a bit of picture analysis to know how the graph looks like (which is required for calc'ing f(x)).

cheers,
Marco Bertschi
Arseniy-developer 17-Mar-13 15:46pm    
I can make a graph in any appropriate form if the is any way to extract values from image.
Well.. maybe there is a way to get text file from image?.. point =(1,1) - next point = (2,1) ..etc..
Marco Bertschi 17-Mar-13 16:07pm    
So you want to create the graph by using f(x)?
Arseniy-developer 18-Mar-13 6:27am    
Dear Marco, vise versa - I want to get values from image http://img254.imageshack.us/img254/1992/graphsample.png
Marco Bertschi 18-Mar-13 7:18am    
Now I am confused. I am truly sorry but I will not be able to help you since I simply don't get it. Anyways, I wish you best of luck for your project!

cheers,
Marco Bertschi

OK, it seem like you are bound to have to extract the data from the image, so here some metacode that will extract the function value as defined by the lowest black pixel in each column:

C#
// Assumptions:
// x is in the range [0.0, 1.0], both inclusive
// f(x) is in the range [0.0, 1.0], both inclusive
// Every column value is defined by the lowest black pixel

public static double f(double x, Bitmap bmap)
{
  double RetCode = 1.0; // no pixel, assume top most value 1.0
  int positionX = (int)((double)bmap.Width * x + 0.5);  
  for(int positionY = 0; positionY < bmap.Height; positionY++)
  {
    Color pix = bmap.GetPixel( positionX, positionY );
    if( pix.R == 0 )
    {
        RetCode = (double)positionY / (double)bmap.Height;
        break;
    } 
  }
  return RetCode;
}


You can improve on the value extracted by also scanning the adjacent column and then linear interpolating by the fraction that the real positionX value is off from the integer part, but since this is a hand-drawn function curve, I think approximation by whole pixels is acceptable.
 
Share this answer
 
v2
(Adding this in the solution as I can use the layout tags here.)

OK, I see.
In this case I would recommend that you write a function that does linear approximation of parts of the curve (or some other approximation (e.g. quadratic) that you can model reasonably).
The function would look somehow along this line:
C++
double F(double x)
{
  double RetCode;
  if(x < 0.2) {
    RetCode = a1 * x + b1;
  } else if (x < 0.6) {
    RetCode = a2 * x + b2;
  } else if (x < 0.8) {
    RetCode = a3 * x + b3;
  } else {
    RetCode = a4 * x + b4;
  }
  return RetCode;
}

and find matching rages with the a1, b1, a2, b2, ...
Extracting the values form a hand-drawn pixel image is just not something you should ever reasonably consider.
For example, some columns of your image had more than one pixel, what value would you assign in this case?
 
Share this answer
 
v2
Comments
Arseniy-developer 20-Mar-13 1:37am    
Well, it is pretty good unless you need to change the curve form. You need to change half of coefficients carefully to avoid functing breake :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900