Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET
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 17-Mar-13 8:23am
Edited 17-Mar-13 8:32am
v2
Comments
Marco Bertschi at 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 at 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 at 17-Mar-13 16:07pm
   
So you want to create the graph by using f(x)?
Arseniy-developer at 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 at 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
ali_crash at 17-Mar-13 19:55pm
   
can you show us one of that graphs for example?
Arseniy-developer at 18-Mar-13 4:51am
   
Here is example graph:
http://img254.imageshack.us/img254/1992/graphsample.png[^]

I need to get valus F(x) for example F(0.5) will be 0.32
Sergey Alexandrovich Kryukov at 17-Mar-13 20:43pm
   
It would be the one of the greatest abuse I ever heard of. Why would anyone need it?
—SA
Arseniy-developer at 18-Mar-13 4:51am
   
The goal is to simulate car wheel angle turning (Y axis is angle from 0 to 1 (60 grad in scaling) ) depending on left and right arrow pressing time (X axis is time from 0 to 1 sec)
For every car model and tuning there is individual wheel reaction function.

It looks like Need for speed have such kind of graps or they wrote some complex functions which is hard for my understanding.
Sergey Alexandrovich Kryukov at 18-Mar-13 10:08am
   
I was asking why getting data from a picture, while it should be the opposite?
—SA
Arseniy-developer at 19-Mar-13 4:43am
   
Sergey I dont know a function formula, but I am able to draw it. And I need to change it in future easy just by changing image quickly (not working with coefficients and formula polinoms)
Sergey Alexandrovich Kryukov at 19-Mar-13 9:26am
   
No. If you know how to draw, you know a function. Not a formula of a function, but a function, data. When you draw, you loose data. But you should not loose data, keep it. After all, use your brain.
—SA
Steve44 at 18-Mar-13 19:04pm
   
Hi Arseniy,
I am with Sergey here, the question is why do you provide a hand drawn bitmap to define a F(x) function? Where do these bitmaps come from and are there other ways to have a user enter the coordinate pairs (x, F(x))? Are they visualization of some measurement?
(I can give you guidance how to extract your information from the bitmap, but first I want to know, if there are better ways to solve the whole problem.)
Arseniy-developer at 19-Mar-13 4:37am
   
I just have an idea how mechanical wheel should react on player key pressing and I can express it in Image in paint.exe program or any other graphic or function editor by hand. The function is neither linear nor the power of x, it is maybe some kind of 3rd or 4th power of x with a lot of coefficients which I cannot predict.
Steve44 at 19-Mar-13 4:52am
   
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:
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 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
 
// 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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

(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:
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?
  Permalink  
v2
Comments
Arseniy-developer at 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)

  Print Answers RSS
0 OriginalGriff 474
1 Gihan Liyanage 338
2 ChauhanAjay 180
3 Sergey Alexandrovich Kryukov 163
4 Vinay Mistry 160
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100