In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you the example analysis of image quantization processing in Python, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of gains after reading this article, let's go to understand it together!
I. Principle of Image Quantization
Quantization is the process of converting the continuous variation interval of brightness corresponding to image pixels into a single specific value, that is, discretizing the spatial coordinate amplitude value of the original gray image. The more quantization levels, the richer the image level, the higher the gray resolution, the better the image quality; the less quantization levels, the less rich the image level, the lower the gray resolution, the image contour layering phenomenon will occur, reducing the image quality. Figure 8-1 shows the process of converting successive gray values of an image to gray levels from 0 to 255 [1-3].
If the quantization level is 2, two gray levels will be used to represent the pixels of the original picture (0-255), with 0 for gray values less than 128 and 128 for gray values greater than or equal to 128; If the quantization level is 4, the pixels of the original picture will be represented using four gray levels, and the new image will be stratified into four colors, 0 in the interval 0-64, 64 in the interval 64-128, 128 in the interval 128-192, 192 in the interval 192-255, and so on.
Figure 8.2 is a "Lena" plot comparing different quantization levels. where the quantization level of (a) is 256, the quantization level of (b) is 64, the quantization level of (c) is 16, the quantization level of (d) is 8, the quantization level of (e) is 4, and the quantization level of (f) is 2.
II. implementation of image quantization
The process of image quantization is to create a temporary image, then loop through all pixels in the original image, determine the quantization level that each pixel should belong to, and finally display the temporary image. The following code converts a grayscale image into two quantization levels [4].
# -*- coding: utf-8-*-# By: Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt#Read original image img = cv2.imread ('lena-hd.png ')#Get image height and width height = img.shape[0]width = img.shape[1]#Create an image new_img = np.zeros((height, width, 3), np.uint8)#Image quantization operation quantization level is 2 for i in range(height): for j in range(width): for k in range(3): #corresponds to BGR tricomponent if img[i, j][k] < 128: gray = 0 else: gray = 128 new_img[i, j][k] = np.uint8(gray) #Display image cv2.imshow("src", img)cv2.imshow("", new_img)#Wait for cv2.waitKey(0)cv2.destroyAllWindows() to be displayed
The output is shown in Figure 8-3, which divides the gray image into two quantization levels.
III. image quantization level contrast
The following codes compare the quantization effects of quantization levels 2, 4, and 8, respectively [5].
# -*- coding: utf-8 -*-# By: Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt#Read original image img = cv2.imread ('lena-hd.png ')#Get image height and width height = img.shape[0]width = img.shape[1]#Create an image new_img1 = np.zeros ((height, width, 3), np.uint8)new_img2 = np.zeros((height, width, 3), np.uint8)new_img3 = np.zeros((height, width, 3), np.uint8)#Quantization processing for i in range(height): for j in range(width): for k in range(3): #corresponds to BGR tricomponent if img[i, j][k] < 128: gray = 0 else: gray = 128 new_img1[i, j][k] = np.uint8(gray)#Quantization for i in range(height): for j in range(width): for k in range(3): #corresponds to BGR tricomponent if img[i, j][k] < 64: gray = 0 elif img[i, j][k] < 128: gray = 64 elif img[i, j][k] < 192: gray = 128 else: gray = 192 new_img2[i, j][k] = np.uint8(gray)#Quantization for i in range(height): for j in range(width): for k in range(3): #corresponds to BGR tricomponent if img[i, j][k] < 32: gray = 0 elif img[i, j][k] < 64: gray = 32 elif img[i, j][k] < 96: gray = 64 elif img[i, j][k] < 128: gray = 96 elif img[i, j][k] < 160: gray = 128 elif img[i, j][k] < 192: gray = 160 elif img[i, j][k] < 224: gray = 192 else: gray = 224 new_img3[i, j][k] = np.uint8(gray)#Used to display Chinese tags plt.rcParams <$'font.sans-serif']=['SimHei']#Display image titles = ['(a) Original image','(b) Quantization-L2','(c) Quantization-L4','(d) Quantization-L8'] images = [img, new_img1, new_img2, new_img3] for i in range(4): plt.subplot(2,2,i+1), plt.imshow(images[i], 'gray'), plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()
The output is shown in Figure 8-4. The code calls matplotlib.pyplot to draw four images, where (a) represents the original image,(b) represents quantization at level 2,(c) represents quantization at level 4, and (d) represents quantization at level 8.
4.K-Means clustering to achieve quantitative processing
In addition to quantization processing by statistical comparison of pixels, clustering processing may be performed based on similarity between pixels. A quantization process based on K-Means clustering algorithm is added here, which can segment and quantize RGB pixels of color image. In addition, this section only briefly introduces the reader to the method, more knowledge of K-Means clustering will be described in detail in the image segmentation article [6].
# coding: utf-8# By: Eastmountimport cv2import numpy as npimport matplotlib.pyplot as plt#Read original image img = cv2.imread ('luo.png ') #Convert image 2D pixels to 1D data = img.reshape((-1,3))data = np.float32(data)#Define center (type,max_iter,epsilon)criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)#Set tags = cv2.KMEANS_RANDOM_CENTERS#K-Means Cluster into 4 categories compactness, labels, centers = cv2.kmeans (data, 8, None, criteria, 10, flags)#Image converted back to uint8 2D type centers = np.uint8(centers)res = centers[labels.flatten()]dst = res.reshape ((img.shape))#Convert image to RGB display img = cv2.cvtColor (img, cv2.COLOR_BGR2RGB)dst = cv2.cvtColor (dst, cv2.COLOR_BGR2RGB)#Used to display Chinese tags plt.rcParams <$'font.sans-serif']=<$'SimHei']#Display image titles =<$' original image','cluster quantization K= 8'] images = [img, dst] for i in range(2): plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray'), plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()
The output is shown in Figure 8-5.
It aggregates the gray of color image into eight colors by K-Means clustering algorithm. The core code is as follows:
cv2.kmeans(data, 8, None, criteria, 10, flags)
The above is all the content of this article "Example analysis of image quantization processing in Python", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.