【问题标题】:normalizing a list of doubles to range -1 to 1 or 0 - 255将双精度列表标准化为 -1 到 1 或 0 - 255
【发布时间】:2012-05-09 17:37:48
【问题描述】:

我有一个在-1.3966551.74707 之间的任何地方的双精度列表,甚至可能更高或更低,无论哪种方式,我都会知道MinMax 值在规范化之前是什么。我的问题是如何在-11 之间规范化这些值,或者更好地将它们从双精度值转换为0255 的char 值

任何帮助将不胜感激。

double range = (double)(max - min);
value = 255 * (value - min)/range

【问题讨论】:

  • 您可能会发现How to normalize a list of int values 很有帮助。
  • 对不起,我是一个菜鸟,我时不时需要帮助 :( 我的第一次反对票...让我觉得很垃圾。有时当你试图解决问题时,你会搜索高低搜索找不到任何东西,或者如果你找到了,你就是无法掌握它,所以我想到了这个伟大的社区,我知道友好的人会尝试帮助/让我理解。

标签: c++ normalization


【解决方案1】:

您需要y = mx + c 形式的mapping,并且需要找到mc。你有两个固定的数据点,即:

 1 = m * max + c
-1 = m * min + c

从那里,它是simple algebra

【讨论】:

    【解决方案2】:

    最简单的方法是首先移动所有值,使 min 为 0,方法是从每个数字中减去 Min。然后乘以 255/(Max-Min),这样移动后的 Max 将映射到 255,其他一切都将线性缩放。所以我相信你的方程应该是这样的:

    newval = (unsigned char) ((oldval - Min)*(255/(Max-Min)))

    在转换为 char 之前,您可能需要更仔细地进行四舍五入。

    【讨论】:

      【解决方案3】:

      有两个改变。

      首先,使用 256 作为限制。

      其次,确保稍微缩小范围以避免达到 256。

          public int GetRangedValue(double value, double min, double max)
          {
              int outputLimit = 256;
      
              double range = (max - min) - double.Epsilon; // Here we shorten the range slightly
      
              // Then we build a range such that value >= 0 and value < 1
              double rangedValue = (value - min) / range;
      
              return min + (int)(outputLimit * rangedValue);
          }
      

      通过这两个更改,您将在输出中获得正确的分布。

      【讨论】:

        【解决方案4】:

        当我开始使用 C++ 做一些卷积工作时,我解决了这个需求。

        希望我的代码能给你一个有用的参考:)

        bool normalize(uint8_t*& dst, double* src, int width, int height) {
            dst = new uint8_t[sizeof(uint8_t)*width*height];
            if (dst == NULL)
                return false;
            memset(dst, 0, sizeof(uint8_t)*width*height);
            double max   = std::numeric_limits<double>::min();
            double min   = std::numeric_limits<double>::max();
            double range = std::numeric_limits<double>::max();
            double norm  = 0.0;
            //find the boundary
            for (int j=0; j<height; j++) {
                for (int i=0; i<width; i++) {
                    if (src[i+j*width] > max) 
                        max = src[i+j*width];
                    else if (src[i+j*width] < min)
                        min = src[i+j*width];
                }
            }
            //normalize double matrix to be an uint8_t matrix
            range = max - min;
            for (int j=0; j<height; j++) {
                for (int i=0; i<width; i++) {
                    norm = src[i+j*width];
                    norm = 255.0*(norm-min)/range;
                    dst[i+j*width] = (uint8_t)norm;
                }
            }
            return true;
        }
        

        基本上输出(calley 通过 'dst' 接收)在 [0, 255] 左右。

        【讨论】:

          猜你喜欢
          • 2020-06-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-25
          • 2018-07-25
          • 1970-01-01
          • 2018-11-30
          相关资源
          最近更新 更多