【问题标题】:how to find 5th and 95th percentile in the histogram如何在直方图中找到第 5 个和第 95 个百分位数
【发布时间】:2012-10-04 08:14:53
【问题描述】:

我正在尝试在 php gd 中实现对比度拉伸功能,我想将图像的 5% 设置为最小值,将 95% 设置为最大值。任何人都知道如何使用 php gd 获取直方图和这些值?

谢谢。

【问题讨论】:

    标签: image-processing computer-vision php-gd contrast


    【解决方案1】:

    恐怕你需要一个一个地计算像素。

    $gd = // Create image of same size, copy original image into $gd
    // Make grayscale
    imageFilter($gd, IMG_FILTER_GRAYSCALE);
    
    $pre = array_fill(0, 256, 0);
    
    for ($y = 0; $y < ImageSY($gd); $y++)
    {
        for ($x = 0; $x < ImageSX($gd); $x++)
        {
            $luma = (imageColorAt($x, $y) & 0xFF); // Grayscale, so R=G=B=luma
            $pre[$luma]++;
        }
    }
    
    // Then you need to build the cumulative histogram:
    
    $max = $pre[0];
    $hist[0] = $pre[0];
    for ($i = 1; $i < 256; $i++)
    {
        $hist[$i] = $hist[$i-1]+$pre[$i];
        if ($max < $pre[$i])
                $max = $pre[$i];
    }
    // Now scale to 100%
    for ($i = 0; $i < 256; $i++)
    {
        $hist[$i] = ($hist[$i]*100.0)/((float)$hist[255]);
        if ($hist[$i] >= 5)
            if ((0 == $i) || ($hist[$i-1] < 5))
                print "Fifth percentile ends at index $i (not included)\n";
        if ($hist[$i] >= 95)
            if ($hist[$i-1] < 95)
                print "Ninety-fifth percentile begins at index $i (included)\n";
    }
    
    // Create graphics, just to check.
    // Frequency is red, cumulative histogram is green
    
    $ck = ImageCreateTrueColor(255, 100);
    $w  = ImageColorAllocate($ck, 255, 255, 255);
    $r  = ImageColorAllocate($ck, 255,   0,   0);
    $g  = ImageColorAllocate($ck,   0, 255,   0);
    ImageFilledRectangle($ck, 0, 0, 255, 100, $w);
    for ($i = 0; $i < 256; $i++)
    {
        ImageLine($ck, $i, 100-$hist[$i], $i, 100, $g);
        ImageLine($ck, $i, 100.0-100.0*((float)$pre[$i]/$max), $i, 100, $r);
    }
    ImagePNG($ck, 'histograms.png');
    

    【讨论】:

    • @Iserni wow..非常感谢,但你所说的第一个百分位数的“不包括”是什么意思?这不是第 5 个百分位数的值吗?
    • 我的意思是,如果算法告诉您第 5 个百分位结束于索引 7 不包括在内,这意味着第 5 个百分位由索引 0、1、2、3、4、5 和 6 组成。这是近似的,因为如果你去掉索引 0-6,你实际上是去掉了,比如说,总数的 4.8%,如果你去掉 0-7,你去掉了 5.2% 左右(取决于图像)跨度>
    【解决方案2】:

    获取所有值的排序列表(升序为第 5 个百分位数,降序为第 95 个百分位数)。然后遍历所有值,直到从开始到该索引的子列表长度 >= 完整列表长度的 5%。当前索引的值是您要查找的百分位数。甚至不涉及直方图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 2023-03-27
      • 2019-12-26
      • 2012-10-31
      • 2011-10-10
      • 1970-01-01
      • 2013-10-28
      • 2021-02-07
      相关资源
      最近更新 更多