In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces "how to use OpenCV to achieve image enhancement" related knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
This issue describes how to extract useful information from low-resolution / blur / low-contrast images through image processing.
Let's explore this process together:
First we got an image of the LPG cylinder from the warehouse running on the conveyor belt. Our goal is to find out the batch number of LPG cylinders in order to update the number of tested LPG cylinders.
Step 1: import the necessary libraries
Import cv2import numpy as npimport matplotlib.pyplot as plt
Step 2: load the image and display the sample image.
Img= cv2.imread ('cylinder1.png') img1=cv2.imread (' cylinder.png') images=np.concatenate (img (img,img1), axis=1) cv2.imshow ("Images", images) cv2.waitKey (0) cv2.destroyAllWindows ()
Pictures of LPG cylinders (a) batch-D26 (b) batch C27
The contrast of the image is very poor. We can hardly see the batch number. This is a common problem in warehouses with insufficient lighting conditions. Next we will discuss the adaptive histogram equalization with limited contrast and try to experiment with different algorithms on the data set.
Step 3: convert the image to grayscale image
Gray_img=cv2.cvtColor (img,cv2.COLOR_BGR2GRAY) gray_img1=cv2.cvtColor (img1,cv2.COLOR_BGR2GRAY)
Step 4: after finding the histogram of the grayscale image, look for the distribution of intensity.
Hist=cv2.calcHist (gray_img, [0], None, [0256]) hist1=cv2.calcHist (gray_img1, [0], None, [256], [0256]) plt.subplot (121) plt.title ("Image1") plt.xlabel ('bins') plt.ylabel ("No of pixels") plt.plot (hist) plt.subplot (122) plt.title ("Image2") plt.xlabel (' bins') plt.ylabel ("No of pixels") plt.plot (hist1) plt.show ()
Step 5: now use the cv2.equalizeHist () function to equalize the contrast of a given grayscale image. The cv2.equalizeHist () function standardizes brightness and increases contrast.
Gray_img_eqhist=cv2.equalizeHist (gray_img) gray_img1_eqhist=cv2.equalizeHist (gray_img1) hist=cv2.calcHist (gray_img_eqhist, [0], None, [0256]) hist1=cv2.calcHist (gray_img1_eqhist, [0], None, [256], [0256]) plt.subplot (121) plt.plot (hist) plt.subplot (122) plt.plot (hist1) plt.show ()
Step 6: display the grayscale histogram equalization image
Eqhist_images=np.concatenate (gray_img_eqhist,gray_img1_eqhist), axis=1) cv2.imshow ("Images", eqhist_images) cv2.waitKey (0) cv2.destroyAllWindows ()
Gray histogram equalization
Let's learn more about CLAHE.
Step 7:
Adaptive histogram equalization with limited contrast
The algorithm can be used to improve the contrast of the image. The algorithm works by creating multiple histograms of the image and uses all of these histograms to redistribute the brightness of the image. CLAHE can be applied to grayscale images and color images. There are 2 parameters to adjust.
1. Clipping sets the threshold of the contrast limit. Default value is 40
2. TileGridsize sets the number of headings in rows and columns. When CLAHE is applied, in order to perform the calculation, the image is divided into small blocks called blocks (8 * 8).
Clahe=cv2.createCLAHE (clipLimit=40) gray_img_clahe=clahe.apply (gray_img_eqhist) gray_img1_clahe=clahe.apply (gray_img1_eqhist) images=np.concatenate ((gray_img_clahe,gray_img1_clahe), axis=1) cv2.imshow (Images, images) cv2.waitKey (0) cv2.destroyAllWindows ()
Step 8:
Threshold technology
Threshold processing is a simple but effective method to divide an image into foreground and background. If the pixel intensity is less than a predefined constant (threshold), the simplest thresholding method replaces each pixel in the source image with a black pixel; if the pixel intensity is greater than the threshold, the source pixel is replaced with a white pixel. The different types of thresholds are:
Cv2.THRESH_BINARY
Cv2.THRESH_BINARY_INV
Cv2.THRESH_TRUNC
Cv2.THRESH_TOZERO
Cv2.THRESH_TOZERO_INV
Cv2.THRESH_OTSU
Cv2.THRESH_TRIANGLE
Try changing the threshold and max_val to get different results.
Th=80max_val=255ret, o1 = cv2.threshold (gray_img_clahe, th, max_val, cv2.THRESH_BINARY) cv2.putText (o1, "Thresh_Binary", (40100), cv2.FONT_HERSHEY_SIMPLEX,2, (255255255), 3 cv2.threshold (gray_img_clahe, th, max_val, cv2.THRESH_BINARY_INV) cv2.putText (O2, "Thresh_Binary_inv", (40100), cv2.FONT_HERSHEY_SIMPLEX,2, (255255255), 3 Cv2.LINE_AA) ret, o3 = cv2.threshold (gray_img_clahe, th, max_val, cv2.THRESH_TOZERO) cv2.putText (o3, "Thresh_Tozero", (40100), cv2.FONT_HERSHEY_SIMPLEX,2, (255255255), 3 ret cv2.LINEPAA) ret, o4 = cv2.threshold (gray_img_clahe, th, max_val, cv2.THRESH_TOZERO_INV) cv2.putText (o4, "Thresh_Tozero_inv", (40100), cv2.FONT_HERSHEY_SIMPLEX,2, (255255255), 3 Cv2.LINE_AA) ret, o5 = cv2.threshold (gray_img_clahe, th, max_val, cv2.THRESH_TRUNC) cv2.putText (o5, "Thresh_trunc", (40100), cv2.FONT_HERSHEY_SIMPLEX,2, (255255255), 3 cv2.threshold (gray_img_clahe, th, max_val, cv2.THRESH_OTSU) cv2.putText (o6, "Thresh_OSTU", (40100), cv2.FONT_HERSHEY_SIMPLEX,2, (255255255), 3 Cv2.LINE_AA)
Final=np.concatenate ((o1memo2recovero3), axis=1) final1=np.concatenate ((o4memo5recovero6), axis=1)
Cv2.imwrite ("Image1.jpg", final) cv2.imwrite ("Image2.jpg", final1)
Thresh_Binary_inv,Thresh_Binary_inv,Thresh_Tozero
Thresh_Tozero_inv,Thresh_trunc,Thresh_OSTU
Step 9: adaptive threshold
In the previous section, we used global thresholds to apply cv2.threshold (). As we can see, because the lighting conditions are different in different areas of the image, the results are not very good. In these cases, you can try adaptive thresholding. In OpenCV, adaptive threshold processing is performed by the cv2.adapativeThreshold () function
This feature applies adaptive thresholds to src arrays (8-bit single-channel images). The maxValue parameter sets the value of pixels in the dst image that meet the criteria. The adaptiveMethod parameter sets the adaptive threshold algorithm to use.
Cv2.ADAPTIVE_THRESH_MEAN_C: calculate the average value of the blockSize x blockSize neighborhood of T (XPerry y) as the average value of the C parameter.
Cv2.ADAPTIVE_THRESH_GAUSSIAN_C: calculate the T (x _ ()) threshold as the weighted sum of the blockSize x blockSize neighborhood of (x _ ()) minus the C parameter.
The blockSize parameter sets the size of the neighborhood used to calculate the pixel threshold, which can take values 3, 5, 7, and so on.
The C parameter is just a constant subtracted from the mean or weighted mean (depending on the adaptive method of setting the adaptiveMethod parameter). Typically, this value is positive, but can be zero or negative.
Gray_image = cv2.imread ('cylinder1.png',0) gray_image1 = cv2.imread (' cylinder.png',0) thresh2 = cv2.adaptiveThreshold (gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) thresh3 = cv2.adaptiveThreshold (gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 31, 3) thresh4 = cv2.adaptiveThreshold (gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 13 5) thresh5 = cv2.adaptiveThreshold (gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 4) thresh21 = cv2.adaptiveThreshold (gray_image1, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) thresh31 = cv2.adaptiveThreshold (gray_image1, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 31, 5) thresh41 = cv2.adaptiveThreshold (gray_image1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY 21 thresh51 5) thresh51 = cv2.adaptiveThreshold (gray_image1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31,5)
Final=np.concatenate ((thresh2,thresh3,thresh4,thresh5), axis=1) final1=np.concatenate ((thresh21,thresh31,thresh41,thresh51), axis=1) cv2.imwrite ('rect.jpg',final) cv2.imwrite (' rect1.jpg',final1)
Adaptive threshold
Adaptive threshold
Step 10:OTSU binarization
The binarization algorithm of Otsu is a good method in processing bimodal images. The bimodal image can be characterized by its histogram containing two peaks. Otsu's algorithm automatically calculates the optimal threshold to separate the two peaks by maximizing the variance between the two types of pixels. Similarly, the optimal threshold minimizes the differences within the group. Otsu's binarization algorithm is a statistical method because it relies on statistics derived from the histogram (for example, mean, variance or entropy) gray_image = cv2.imread ('cylinder1.png',0) gray_image1 = cv2.imread (' cylinder.png',0) ret,thresh2 = cv2.threshold (gray_image,0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) ret,thresh3 = cv2.threshold (gray_image1,0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
Cv2.imwrite ('rect.jpeg',np.concatenate ((thresh2,thresh3), axis=1))
OTSU binarization
Now, we have clearly identified the batch number from the low-contrast image.
"how to use OpenCV to achieve image enhancement" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.