In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of non-linear transformation of image grayscale in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
one。 Nonlinear transformation of image grayscale
The gray value of the original image is transformed nonlinearly according to the formula of DB=DA × DA/255. The code is as follows:
#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt# reads the original image img = cv2.imread ('luo.png') # Image grayscale conversion grayImage = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # get image height and width height = grayImage.shape [0] width = grayImage.shape [1] # create an image result = np.zeros ((height, width)) Np.uint8) # Image grayscale nonlinear transformation: DB=DA × DA/255for i in range (height): for j in range (width): gray = int (grayImage [iMagnej]) * int (grayImage [iMagnej]) / 255result [iMagnej] = np.uint8 (gray) # display image cv2.imshow ("Gray Image", grayImage) cv2.imshow ("Result", result) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output result of the nonlinear gray transformation of the image is shown in figure 13-1:
two。 Image gray logarithm transformation
The logarithmic transformation of image grayscale is generally expressed as shown in the formula (13-1):
Where c is the scale comparison constant, DA is the gray value of the original image, and DB is the grayscale value of the transformed target. As shown in figure 13-2, it represents the change of the grayscale value under the logarithmic curve, where x represents the grayscale value of the original image and y represents the target grayscale value after the logarithmic transformation.
Because the slope of the logarithmic curve is large in the areas with lower pixel values and smaller in the areas with higher pixel values, the contrast of the darker areas of the image will be improved after logarithmic transformation. This transformation can be used to enhance the dark details of the image, thereby expanding the darker pixels in the compressed high-value image.
Logarithmic transformation achieves the effect of expanding low gray value and compressing high gray value, which is widely used in the display of spectrum images. A typical application is the Fourier spectrum, whose dynamic range may be as wide as 0: 106 to display the spectrum directly, the dynamic range of the image display device often can not meet the requirements, resulting in the loss of a large number of dark details; after using logarithmic transformation, the dynamic range of the image is reasonably nonlinear compressed, so that it can be displayed clearly.
In figure 13-3, the untransformed spectrum is logarithmically transformed to increase the contrast of the low grayscale area, thereby enhancing the details of the dark part.
The following code realizes the logarithmic transformation of image grayscale.
#-*-coding: utf-8-*-# By:Eastmountimport numpy as npimport matplotlib.pyplot as pltimport cv2# draw the curve def log_plot (c): X = np.arange (0256,0.01) y = c * np.log (1 + x) plt.plot (x, y,'r') Linewidth=1) plt.rcParams ['font.sans-serif'] = [' SimHei'] # normal display of Chinese tags plt.title ('logarithmic transformation function') plt.xlabel ('x') plt.ylabel ('y') plt.xlim (0,255), plt.ylim (0,255) plt.show () # logarithmic transformation def log (c Img): output = c * np.log (1.0 + img) output = np.uint8 (output + 0.5) return output# read the original image img = cv2.imread ('dark.png') # draw logarithmic transformation curve log_plot (42) # Image grayscale logarithmic transformation output = log (42, img) # display image cv2.imshow (' Input', img) cv2.imshow ('Output', output) cv2.waitKey (0) cv2.destroyAllWindows ()
Figure 13-4 shows the effect image after logarithmic function processing. Logarithmic transformation is better for images with low overall contrast and low grayscale value.
The corresponding logarithmic function curve is shown in figure 13-5, where x represents the grayscale value of the original image and y represents the target grayscale value after logarithmic transformation.
three。 Gray gamma transform of image
Gamma transformation, also known as exponential transformation or power transformation, is another commonly used grayscale nonlinear transformation. The gamma transformation of the grayscale of an image is generally expressed as shown in the formula (13-2):
When γ > 1, the area with higher gray level in the image will be stretched and the part with lower gray level will be compressed.
When γ
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.