从函数图中得到f(x)值? [英] get f(x) value from graph of the function?

查看:54
本文介绍了从函数图中得到f(x)值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发者!

我有一个功能图(bmp pictute)。我需要一种从这个图中获得F(x)值的方法。

问题看起来非常简单:X值的范围是0到1,Y值的范围是0到1,图表是手工绘制的曲线。

有没有办法通过几行代码实时获得F(x)值而不需要发明一个循环?

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?

推荐答案

好的,看起来你好像是必须从图像中提取数据,因此这里的一些元代码将提取每列中最低黑色像素定义的函数值:



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;
}





您可以通过扫描相邻列来改善提取的值,然后通过线性插值进行线性插值实际positionX值偏离整数部分,但由于这是一个手绘函数曲线,我认为按整个像素逼近是可以接受的。



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.


(尽我所能在解决方案中添加它在这里使用布局标签。)



好​​的,我明白了。

在这种情况下,我建议你写一个线性函数曲线部分的近似值(或者你可以合理建模的其他近似值(例如二次曲线))。

该函数看起来有点像这样:

(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;
}



并找到与a1,b1,a2,b2匹配的rages,...

从值中提取值手绘像素图像不是你应该合理考虑的东西。

例如,你的图像的某些列有多个像素,在这种情况下你会指定什么值?


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?


这篇关于从函数图中得到f(x)值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆