Adjust Image Range using Python

Asked

Viewed 1,033 times

1

I’m a beginner in python, but I have experience with other programming languages. I need to do some college work, but I don’t 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 wonder if there is a function that does this in python or if it does not exist, could you tell me what would be the process to make this adjustment? Because knowing the process I can create the algorithm in the "hand".
NOTE: if possible, avoid responses based only on external links, that is, do not leave only the link to an external page as a response.
OBS2: I have nothing "ready", just reading the image using opencv.

import cv2
imagem = cv2.imread("../Imagens/im01.jpg");
  • Out of curiosity, why not accept links that are not from the OS? And if it is necessary to cite the official documentation of the tool?

  • opa @Andersoncarloswoss, links to documentation reference are very welcome. What I meant by avoiding external links is that some users only put as a response the link to some external site and if that site falls in the future, someone who will have the same problem that I will not be able to get the solution. I have been through this several times, I find a solution that leads to an external link, but the page no longer exists. Understood what I meant?

  • Now yes, so it would be more explicit in the question put "avoid response only based on links", but this is also unnecessary, because this is one of the internal philosophies of the community and if there is an answer like this it will be notified and possibly removed.

  • @Andersoncarloswoss understood, I will edit the question with your suggestion.

  • Hello. You say this is a college job, so I understand that it’s part of your learning to learn how to do the program. Your question is valid, but you essentially say, "Does anyone have the code ready?". Well, in the documentation you find the code ready, have you even looked at it? And a simple search for "gamma Correction opencv" on the Internet returns a lot of cool material, such as: https://www.pyimagesearch.com/2015/10/05/opencv-gamma-correction/ It is not my bad will to answer, but I think it will help you more if you try something before. :)

  • @Luizvieira understood Luiz, I think I ended up expressing myself badly, I’m sorry for that. The intention of my question was to know if there was already any function in the python that already did this and if it did not exist, to know what would be the procedures to do this (in this case, the theoretical steps), because I am totally secular in this subject of image processing. Knowing the process, I would create the algorithm myself without problems. But thanks for answering, I will look at the link you passed and in case I manage to solve the problem, I will leave an answer for future users.

  • No problem. : ) By the way, remember that you are very welcome to always post here! I just think that , more interesting questions (which, by the way, generate more reputation) are those with better described/characterized problems. On understanding the process, a good reading source is this: http://www.cambridgeincolour.com/tutorials/gamma-correction.htm

Show 2 more comments

1 answer

2

The following code resolves for you (ref: https://www.pyimagesearch.com/2015/10/05/opencv-gamma-correction/):

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 figure out how it works. The range setting works as follows: O = I (1 / G)

that is, the output is the image, normalized from a value between 0 and 1, raised to 1/range. That’s what the function adjust_gamma ago.

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

Therefore, what is done is a table that relates all possible values of a pixel [0_255] with its value adjusted with the gamma correction.

Then, just do a very efficient "lookup table" implemented by opencv: cv2.LUT.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.