v9.1.1
Menu

Contrast Adjustment

easyFREEPoint OperatorsImage ProcessingCV: Image Processing

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 α\alpha, to each pixel value, Iin(x,y)I_{in}(x,y).

Here are the steps to achieve contrast adjustment:

  1. Multiply each pixel value by the factor α\alpha.
  2. Apply a clamping operation to ensure the resulting pixel values fall within the valid range.
Iout(x,y)=clamp(α⋅Iin(x,y),0,255)I_{out}(x,y) = \text{clamp}(\alpha \cdot I_{in}(x,y), 0, 255)

This technique is widely used in digital photography and image editing software.

Example

Input
adjust_contrast([[100, 200]], 1.5)
Output
[[150, 255]]
Reasoning
  • Inputs: image is a grayscale image given as rows of 8-bit intensities, here one row of two pixels, 100100 and 200200 (0 is black, 255 is white). alpha =1.5= 1.5 is the contrast gain; values above 1 stretch intensities apart, values below 1 squeeze them together.
  • Scale: every pixel is treated independently: 1.5×100=1501.5 \times 100 = 150 and 1.5×200=3001.5 \times 200 = 300. The gap between the two pixels grows from 100100 to 150150, which is exactly what "more contrast" means.
  • Round and clamp: both products are already whole numbers. 150150 lies inside [0,255][0, 255] and stays; 300300 is brighter than any 8-bit value can be, so it saturates at 255255.
  • Output: [[150, 255]], a list of lists with the same 1×21 \times 2 shape as the input, holding integers.
  • Notice that clipping loses information: any input above 255/1.5=170255 / 1.5 = 170 would also come out as 255255, so bright pixels become indistinguishable. With alpha =0.5= 0.5 nothing clips and the result is [[50, 100]].

Constraints

  • alpha > 0
  • Clamp output to [0, 255]
  • Round to nearest integer

Learn this in

More from CV: Image Processing

solution.py

Test results

Sign in to run your code against the test cases.