All problems
Contrast Stretch (Percentile)
Problem
Stretch image contrast between two percentiles.
Sort all n pixel values, take lo at index int(low_pct / 100 * (n - 1)) and hi at index int(high_pct / 100 * (n - 1)), then map each pixel with clamp(round((v - lo) / (hi - lo) * 255), 0, 255). If hi equals lo, return a same-shaped grid of zeros.
Hint
The percentile index uses integer truncation, and values outside the band are clamped.
Test cases
case 1
in: [[[0,1,2,3,4,5,6,7,8,9]],10,90]
expected: [[0,32,64,96,128,159,191,223,255,255]]
case 2
in: [[[0,0,0,255]],0,100]
expected: [[0,0,0,255]]
case 3
in: [[[5,5],[5,5]],0,100]
expected: [[0,0],[0,0]]
case 4
in: [[[0,50,100,150,200,250]],20,80]
expected: [[0,0,85,170,255,255]]
Starter codePython
def percentile_stretch(image, low_pct, high_pct):
# Your code here
pass4 test cases · run your code against them in the editor