Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Image sampling and processing in Python

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about the sample analysis of image sampling processing in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

one。 Principle of image sampling and processing

Image sampling (Image Sampling) processing is to divide a continuous image into M × N grids in space, each of which is represented by a luminance value or grayscale value, as shown in figure 9-1.

The larger the interval of image sampling, the less the number of pixels, the lower the spatial resolution, the worse the image quality, and even the mosaic effect; on the contrary, the smaller the interval of image sampling, the more pixels and the higher the spatial resolution of the image. the better the image quality, but the amount of data will increase accordingly. Figure 9-2 shows "Lena" pictures with different sampling intervals, in which figure (a) is the original image, figure (b) is 128x128image sampling effect, figure ©is 64 × 64 image sampling effect, figure (d) is 32 × 32 image sampling effect, figure (e) is 16 × 16 image sampling effect, figure (f) is 8 × 8 image sampling effect [1-3].

two。 Implementation of image sampling

The following describes the Python image sampling processing related code operations. The core process is to establish a temporary picture, set the size of the area to be sampled (such as 16 × 16), and then cycle through all the pixels in the original image, and the pixels in the sampling area are assigned the same value (such as the gray value of the pixels in the upper left corner). Finally, the image sampling processing is realized.

#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt# reads the original image img = cv2.imread ('lena-hd.png') # gets the image height and width height = img.shape [0] width = img.shape [1] # sampling converts to 16 regions numHeight = int (height/16) numWidth = int (width/16) # create an image new_img = np.zeros ((height, width, 3) Np.uint8) # Image cycle sampling 16x16 region for i in range (16): # get Y coordinate y = i*numHeight for j in range (16): # get X coordinate x = j*numWidth # get the pixel in the upper left corner of the fill color b = img [y, x] [0] g = img [y, x] [1] r = img [y X] [2] # Cycling set small area sampling for n in range (numHeight): for m in range (numWidth): new_img [yardn, xampm] [0] = np.uint8 (b) new_img [yambin, xampm] [1] = np.uint8 (g) new_ IMG [y + n Xampm] [2] = np.uint8 (r) # display image cv2.imshow ("src", img) cv2.imshow ("Sampling", new_img) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()

The output is shown in figure 9-3, which samples the grayscale image into 16 × 16 regions.

Similarly, the color image can be sampled and processed, and the following code samples the image of "Xiao Luo" into an 8 × 8 mosaic region.

#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt# reads the original image img = cv2.imread ('luo.png') # gets the image height and width height = img.shape [0] width = img.shape [1] # sampling converts to 8 × 8 area numHeight = int (height/8) numwidth = int (width/8) # create an image new_img = np.zeros ((height, width, 3) Np.uint8) # Image cycle sampling 8x8 region for i in range (8): # get Y coordinate y = i*numHeight for j in range (8): # get X coordinate x = j*numwidth # get the pixel in the upper left corner of the fill color b = img [y, x] [0] g = img [y, x] [1] r = img [y X] [2] # Cycling set small area sampling for n in range (numHeight): for m in range (numwidth): new_img [yardn, xampm] [0] = np.uint8 (b) new_img [yambin, xampm] [1] = np.uint8 (g) new_ IMG [y + n Xampm] [2] = np.uint8 (r) # display image cv2.imshow ("src", img) cv2.imshow ("Sampling", new_img) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()

The output is shown in figure 9-4, which samples the color image into 8 × 8 regions.

However, there is a problem in the above code that when the length and width of the image are not divisible by the sampling region, the rightmost and lowest regions of the output image are not sampled. Here, it is recommended that readers do a congruence operation to sample the areas that cannot be divided into parts.

three。 Image local sampling processing

The code described earlier is to sample the whole image, so how to mosaic the local area of the image? The following code implements this function. When the mouse is pressed, it can mosaic the area where the mouse is dragged and press the "s" key to save the image locally.

#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt# reads the original image im = cv2.imread ('luo.png', 1) # set the left mouse button to open en = False# mouse event def draw (event, x, y, flags Param): global en # press the left mouse button to open the en value if event==cv2.EVENT_LBUTTONDOWN: en = True # press and move the left mouse button elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_LBUTTONDOWN: # call function mosaic if en: drawMask (y X) # left mouse button pop-up end operation elif event==cv2.EVENT_LBUTTONUP: en = False # Image local sampling operation def drawMask (x, y, size=10): # size*size sampling processing m = int (x / size*size) n = int (y / size*size) print (m N) # 10: 10 locale is set to the same pixel value for i in range (size): for j in range (size): im [m] [n] # Open dialog box cv2.namedWindow ('image') # call draw function to set mouse operation cv2.setMouseCallback (' image', draw) # Loop processing while (1): cv2.imshow ('image' Im) # Press ESC to exit if cv2.waitKey (10) & 0xFF==27: break # Press the s key to save the picture elif cv2.waitKey (10) & 0xFF==115: cv2.imwrite ('sava.png', im) # exit window cv2.destroyAllWindows ()

The output is shown in figure 9-5, which mosaics the face of the character.

Thank you for reading! This is the end of this article on "sample analysis of picture sampling and processing in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report