Adjust Image Range using Python

2

I am a beginner in python, but I have experience with other programming languages. I need to do some college work, but I do not know how to proceed. I would like to know how to adjust the range of an image using python, I downloaded some image processing libraries (like opencv). I would like to know if there is any function that does this in python or if it does not exist, could you inform me what the process would be to do this adjustment? Because knowing the process I can create the algorithm in the "hand".
OBS: if possible, avoid responses based only on external links, that is, do not leave as a response only the link to an external page.
OBS2: I have nothing "ready", I'm just reading the image using opencv.

import cv2
imagem = cv2.imread("../Imagens/im01.jpg");
    
asked by anonymous 03.12.2017 / 13:46

1 answer

2

The following code resolves for you (ref: link ):

def adjust_gamma(image, gamma=1.0):
    # build a lookup table mapping the pixel values [0, 255] to
    # their adjusted gamma values
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255
        for i in np.arange(0, 256)]).astype("uint8")

    # apply gamma correction using the lookup table
    return cv2.LUT(image, table)

Let's understand how it works. The range setting works as follows: O = I ^ (1 / G)

ie the output is the normalized image of a value between 0 and 1, raised to 1 / gamma. This is what the adjust_gamma function does.

You could simply divide the value of each pixel of the image by 255 and then raise it to invGamma, but there you would be doing the same account thousands of times, which is inefficient. Remember that a small image may contain tens or hundreds of thousands of pixels.

So, what you do is a table that lists all the possible values of a pixel [0_255] with its value adjusted with gamma correction.

Next, make a very efficient "lookup table" implemented by opencv: cv2.LUT .

    
25.06.2018 / 03:05