2015-07-19 135 views
0

我试图实现直方图RGB,但是我的算法不会像在图形程序中那样产生类似于曲面的外观。有关本网站示例图像:计算的直方图看起来不像预期的那样

OpenCV histogram

我的版本是这样的:

RGB Histogram R channel

正如我理解正确的话,RGB直方图只是测量了每个值在特定的频道频率发生。所以我以这种方式实现它:

 public Process(layerManager: dl.LayerManager) { 
      var surface = layerManager.GetCurrent(); 
      var components = new Uint8Array(1024); 
      surface.ForEachPixel((arr: number[], i: number): void => { 
       components[arr[i]] += 1; 
       components[arr[i + 1] + 256] += 1; 
       components[arr[i + 2] + 512] += 1; 
       components[arr[i + 3] + 768] += 1; 
      }); 
      var histogram = layerManager.GetHistogram(); 
      histogram.Clear(); 
      var viewPort = layerManager.GetHistogramViewPort(); 
      viewPort.Clear(); 
      this.DrawColor(histogram, components, 0, new ut.Color(255, 0, 0, 255)); 
      //histogram.SetBlendMode(ds.BlendMode.Overlay); 
      //this.DrawColor(histogram, components, 256, new ut.Color(0, 255, 0, 255)); 
      //this.DrawColor(histogram, components, 512, new ut.Color(0, 0, 255, 255)); 
     } 

     private DrawColor(surface: ds.ICanvas, components: Uint8Array, i: number, fillStyle: ut.Color) { 
      var point = new ut.Point(0, 255); 
      surface.BeginPath(); 
      surface.FillStyle(fillStyle.R, fillStyle.G, fillStyle.B, fillStyle.A); 
      surface.RGBAStrokeStyle(fillStyle.R, fillStyle.G, fillStyle.B, fillStyle.A); 
      surface.LineWidth(1); 
      surface.MoveTo(point); 
      for (var j = i + 256; i < j; ++i) { 
       point = new ut.Point(point.X + 1, 255 - components[i]); 
       surface.ContinueLine(point); 
      } 
      surface.ClosePathAndStroke(); 

      var viewPort = layerManager.GetHistogramViewPort(); 
      viewPort.DrawImage(surface.Self<HTMLElement>(), 0, 0, 255, 255, 0, 0, viewPort.Width(), viewPort.Height()); 
     } 

我是否错过了什么?

回答

2

您有一个Uint8Array数组来保存结果,但最常见的RGB值发生超过255次。这会导致溢出,并且最终会看到模256值的直方图,对于高值,这是有效的随机值。这就是为什么图的左侧和中间部分(其中值小于255)是正确的,但高价值区域遍布整个地方。

使用较大的数据类型存储结果,并在绘制之前将其规格化为输出画布的大小。

+0

你完全正确! – Puchacz

相关问题