c++ - 在对比拉伸(stretch)方面需要帮助

标签 c++

我在网站上找到了这个公式及其描述

l(x,y)=(l(x,y)-min)(no of intensity levels/(max-min)) + initial intensity level

其中,I( x,y ) 代表图像,在左侧它代表输出图像,而在右侧它代表输入图像中第 y 列的第 x 个像素。在这个等式中,“min”和“max”是当前图像中的最小强度值和最小强度值。这里的“强度级别数”显示了可以分配给一个像素的强度值的总数。例如,通常在灰度图像中,可能的最低强度为0,最高强度值为255。因此“强度级别数”等于255。

我已经把它转换成代码了,我是否正确转换了它?

for(int y = 0; y < bmp.bmHeight; y++)   
  {      
      for(int x = 0; x < bmp.bmWidth; x++)     
      {         
          COLORREF rgb = dc.GetPixel(x, y);     
          BYTE r = GetRValue(rgb);       
          BYTE g = GetGValue(rgb);     
          BYTE b = GetBValue(rgb);     
          BYTE p,p1;
          if(r<g)
          {
              p=r;
              p1=g;
          }
          else
          {
              p=g;
              p1=r;
          }
          if(b<p)
              p=b;
          if(b>p1)
              p1=b;
          BYTE bw = (RGB(r, g, b)-p);
          bw=bw*(255/(p1-p));
          bw+=p1;
          dc.SetPixel(x, y, RGB(bw, bw, bw));      
      }   

最佳答案

这是我对公式进行编码的尝试。基于 Fisher et al 中的公式.请注意,除非您使用 Fisher 文章中描述的截止分数,否则您仍然会遇到看起来很假的图像,但那是您自己的作业 ;-)

我没有用真实图像测试这个,调试它也是你的功课。

引用: 有关强度的计算,请参阅 HSL and HSV on wikipedia .

typedef unsigned char byte;

const byte OUT_MIN  = 0;   // The desired min output luminosity 0   to stretch to entire spectrum
const byte OUT_MAX  = 255; // The desired max output luminosity 255 to stretch to entire spectrum

byte min = 255, max = 0;

// get the minimum and maximum values.
for( int y = 0; y < bmp.bmHeight; y++ )
{      
   for( int x = 0; x < bmp.bmWidth; x++ )     
   {  
       COLORREF rgb       = dc.GetPixel(x, y);   
       byte     intensity = ( rValue(rgb) + gValue(rgb) + bValue(rgb) ) / 3;

       min = min < intensity ? min : intensity;
       max = max > intensity ? max : intensity;
   }
}     

// set the new pixel values
for(int y = 0; y < bmp.bmHeight; y++)   
{      
   for(int x = 0; x < bmp.bmWidth; x++)     
   {  
      COLORREF rgb = dc.GetPixel(x, y);

     // Creates color image
     // Calculate new color using the formula. Has to be int, not byte, because you might go out of range.
     // We correct the range manually on the next two lines. 

     int r = ( rValue( rgb ) - min ) * ( OUT_MAX / (max - min) ) + OUT_MIN;  
     r = r < 0   ? 0   : r;  
     r = r > 255 ? 255 : r;

     int g = ( gValue( rgb ) - min ) * ( OUT_MAX / (max - min) ) + OUT_MIN;  
     g = g < 0   ? 0   : g;  
     g = g > 255 ? 255 : g;

     int b = ( bValue( rgb ) - min ) * ( OUT_MAX / (max - min) ) + OUT_MIN;  
     b = b < 0   ? 0   : b;  
     b = b > 255 ? 255 : b;

     dc.SetPixel( x, y, RGB( r, g, b ) );    
   }   
}

关于c++ - 在对比拉伸(stretch)方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3123196/

相关文章:

c++ - 为什么析构函数挂了

c++ - 使用 alloca 时发生访问冲突

c++ - 在 vector 结果记录器中记录元组

c++ - 重新定义类构造函数错误

c++ - 无法推断出 T[] 的模板参数

c++ - 当我们抛出一个对象/变量来捕获时会发生什么?

c++ - ASCII数据导入: how can I match Fortran's bulk read performance in C++?

c++ - 在循环双向链表的第一个节点之前/之后插入的算法是什么?

c++ - 段错误背包问题

使用 lambda 的 c++11 排序列表