2012-10-04 65 views

回答

4

恐怕你需要逐一計算像素。

$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'); 
+0

@Iserni wow..thank了很多,但什麼叫「不包括」爲弗斯個百分點意味着什麼?這個價值不是第5個百分點的價值嗎? – ATZ

+1

我的意思是,如果算法告訴你第五個百分點在索引7處沒有包括,這意味着第五個百分點由索引0,1,2,3,4,5和6組成。這是近似的,因爲如果你刪掉了你實際切出的0-6的指數,比如說總數的4.8%,如果你切出0-7,則切出5.2%左右(取決於圖像) – LSerni

0

獲取所有值的排序列表(第5百分位升序,第95位降序)。然後遍歷所有值,直到從開始到該索引> =整個列表長度的5%的子列表長度。當前索引值是您正在查找的百分位數。甚至不涉及直方圖。

相關問題