Contrast Adjustment
Implement a point operator to adjust the contrast of an image by modifying its pixel values. This task involves applying a simple transformation to each pixel in the image.
The concept of contrast adjustment is fundamental in image processing, as it enables the enhancement or reduction of the difference between various regions in an image, making it more visually appealing or suitable for further analysis. The contrast of an image can be adjusted by applying a multiplication factor, denoted as , to each pixel value, .
Here are the steps to achieve contrast adjustment:
- Multiply each pixel value by the factor .
- Apply a clamping operation to ensure the resulting pixel values fall within the valid range.
This technique is widely used in digital photography and image editing software.
Example
adjust_contrast([[100, 200]], 1.5)
[[150, 255]]
- Inputs:
imageis a grayscale image given as rows of 8-bit intensities, here one row of two pixels, and (0 is black, 255 is white).alphais the contrast gain; values above 1 stretch intensities apart, values below 1 squeeze them together. - Scale: every pixel is treated independently: and . The gap between the two pixels grows from to , which is exactly what "more contrast" means.
- Round and clamp: both products are already whole numbers. lies inside and stays; is brighter than any 8-bit value can be, so it saturates at .
- Output:
[[150, 255]], a list of lists with the same shape as the input, holding integers. - Notice that clipping loses information: any input above would also come out as , so bright pixels become indistinguishable. With
alphanothing clips and the result is[[50, 100]].
Constraints
- alpha > 0
- Clamp output to [0, 255]
- Round to nearest integer