To see gamma correction in action, just open up a terminal and execute the following command: $ python adjust_gamma.py -image example_01.pngįigure 2: When applying gamma correction with G < 1, the output image is will darker than the original input image. In the case that our gamma value is 1.0, we simply ignore it ( Lines 29 and 30) since gamma=1.0 will not change our input image.įrom there, Lines 33-38 apply gamma correction to our image and display the output result. On Line 27 we start by looping over gamma values in the range (the np.arange function is non-inclusive), incrementing by 0.5 at each step. # apply gamma correction and show the imagesĪdjusted = adjust_gamma(original, gamma=gamma)Ĭv2.putText(adjusted, "g=".format(gamma), (10, 30),Ĭv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)Ĭv2.imshow("Images", np.hstack()) # ignore when gamma is 1 (there will be no change to the image) Let’s explore gamma correction by using a variety of gamma values and inspecting the output image for each: # loop over various values of gamma Line 24 takes the path to our image and loads it. We only need a single switch here, -image, which is the path to where our input image resides on disk. Lines 17-21 handle parsing command line arguments. Let’s continue on with our example: # construct the argument parse and parse the argumentsĪp.add_argument("-i", "-image", required=True, Lastly, all we need to do is apply the cv2.LUT function ( Line 15) to take the input image and the table and find the correct mappings for each pixel value - it’s a simple (and yet very speedy) operation! The pixel value is then scaled to the range followed by being raised to the power of the inverse gamma - this value is then stored in the table. Lines 11 and 12 build this lookup table by looping over all pixel values in the range. The left column is the input pixel value while the right column is the output pixel value after applying the power law transform. OpenCV can then take this table and quickly determine the output value for a given pixel in O(1) time.įor example, here is an example lookup table for gamma=1.2 : 0 => 0 dictionary) that maps the input pixel values to the output gamma corrected values. However, there is an even faster way to perform gamma correction thanks to OpenCV. Overall, the NumPy approach involves a division, raising to a power, followed by a multiplication - this tends to be very fast since all these operations are vectorized. All we need to do is scale the pixel intensities to the range, apply the transform, and then scale back to the range. The first method is to simply leverage the fact that Python + OpenCV represents images as NumPy arrays. There are two (easy) ways to apply gamma correction using OpenCV and Python. In this case, we default gamma=1.0, but you should supply whatever value is necessary to obtain a decent looking corrected image. A second (optional) value is our gamma value. This method requires a single parameter, image, which is the image we want to apply gamma correction to. We define our adjust_gamma function on Line 7. Lines 2-5 simply import our necessary packages, nothing special here. # apply gamma correction using the lookup table Table = np.array().astype("uint8") # build a lookup table mapping the pixel values to ![]() Open up a new file, name it adjust_gamma.py, and we’ll get started: # import the necessary packages Now that we understand what gamma correction is, let’s use OpenCV and Python to implement it. Figure 1: Our original image (left) Gamma correction with G 1 (right), this time the output image is much lighter than the original.
0 Comments
Leave a Reply. |