In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Python and OpenCV for image processing". In daily operation, I believe many people have doubts about how to use Python and OpenCV for image processing. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use Python and OpenCV for image processing". Next, please follow the editor to study!
1. Read Ima
OpenCV provides the cv2.imread () function for reading images. The basic format of this function is:
Retval = cv2.imread (filename [, flags])
Where:
Retval is the return value, whose value is the read image.
Filename is the full file name of the image to be read.
Flags is a read tag that controls the type of file to be read. Some of the commonly used tag values are shown in Table 3-1, where the values of the first column are consistent with the numerical values of the third column.
▲ Table 3-1 commonly used flags tag values
Example 3-1 uses the cv2.imread () function to read an image
The code is as follows:
Import cv2 as cv image = cv2.imread ("F:/picture/lena.png") # read lena image print (image)
Running the code will get the pixel values of the lena image, as shown in figure 3-3.
▲ figure 3-3 partial pixel values of lena images
two。 Display image
OpenCV provides several functions related to image display, and here are a few that are commonly used.
The namedWindow () function is used to create the specified window. The general format is as follows:
None = cv2.namedWindow (window)
Where window is the name of the window. For example:
Cv2.namedWindow ("image")
This program creates a new window named image.
The imshow () function is used to display images in the following general format:
None = cv2.imshow (window, image)
Where:
Window is the name of the window.
Image is the image to be displayed.
The waitKey () function is used to wait for a key to be pressed, and the statement is executed when a key is pressed. The general format is as follows:
Retval= cv2.waitKey ([delay])
Where:
Retval is the return value.
Delay represents the time to wait for the keyboard to trigger, in ms. When the value is negative or 0, it means infinite waiting, and the default value is 0.
The destroyAllWindows function is used to release all windows, and its general format is:
None = cv2. DestroyAllWindows ()
Example 3-2 shows the read image
The code is as follows:
Import cv2 as cv # Import from cv2 module image = cv.imread ("F:/picture/lena.png") # read lena image cv.namedWindow ("image") # create an image window cv.imshow ("image", image) # display image cv.waitKey () # default is 0, infinite wait for cv.destroyAllWindows () # to release all windows
The running result of the program is shown in figure 3-4.
▲ figure 3-4 cases 3-2 running results
3. Save Ima
The cv2.imwrite () function is provided in OpenCV to save the image, and its general format is:
Retval= cv2.imwrite (filename, img [, params])
Where:
Retval is the return value.
Filename is the full pathname of the image to be saved, including the file extension.
Img is the name of the image to be saved.
Params is the saved type parameter, optional.
Example 3-3 write a program to save the read image
The code is as follows:
Import cv2 as cv # Import from cv2 module image = cv.imread ("F:/picture/lena.png") # read lena image cv.imwrite ("F:/picture/lenaresult.png", image) # Save the image to F:/picture/ with the name lenaresult
02 basic operation of image channel
In the process of image processing, the channels are sometimes split and merged as needed. Split () and merge () functions are provided in OpenCV to split and merge images. These two functions are described below.
1. Split () split function
The function split () splits the channels of the image, such as the three channels of the BGR image. The general format is as follows:
Brecrum gpene r = cv2.split (img)
Where:
B, g and r are the image information of B channel, G channel and R channel, respectively.
Img is the image to be split.
Example 3-4 writes a program that uses the split () function to split the image.
The code is as follows:
Import cv2 as cv image = cv.imread ("F:/picture/lena.png") bGrady r = cv.split (image) # split the image channel into three channels cv.imshow ("b", b) # display the image information of channel b cv.imshow ("g", g) # display the image information of channel g cv.imshow ("r") R) # display the image information of r channel cv.imshow ("image", image) cv.waitKey () cv.destroyAllWindows ()
The running result of the program is shown in figure 3-5.
▲ figure 3-5 cases 3-4 results: a) original image, b) B-channel image, c) G-channel image, d) R-channel image
Among them, figure 3 # 5a is the original picture, figure 3 # 5b is the image of B channel, figure 3 # 5c is the image of G channel, and figure 3 # 5d is the image of R channel.
2. Merge () merge function
Channel merging is the inverse process of channel splitting, which can merge the grayscale images of three channels into one color image. Merge () function is provided in OpenCV to realize the merging of image channels. The basic format is as follows:
Imagebgr = cv2.merge ([bmeme gregory r])
Where:
Imagebgr is the merged image.
B, g and r are the image information of B channel, G channel and R channel, respectively.
Example 3-5 write a program to demonstrate the process of merging images
The code is as follows:
Import cv2 as cv image = cv.imread ("F:/picture/lena.png") bMagne r = cv.split (image) # split the image channel into three channels: imagebgr = cv.merge ([bGraingPowerr]) # merge the images of the three channels with cv.imshow ("image", image) cv.imshow ("imagegbgr", imagebgr) cv.waitKey () cv.destroyAllWindows ()
The running result of the program is shown in figure 3-6.
▲ figure 3-6 cases 3-5 running results: a) original image b) split and merged image
Among them, figure 3-6a is the original image, and figure 3-6b is the image that has been split and then merged.
03 acquisition of image attributes
In image processing, it is often necessary to obtain attribute information such as image size, type and so on. Here are a few common properties.
Shape: represents the size of the image. In the case of a color image, an array containing the number of rows, columns, and channels is returned; in the case of a binary or grayscale image, an array containing the number of rows and columns is returned.
Size: represents the number of pixels of the returned image.
Dtype: represents the data type of the returned image.
Example 3-6 write a program to observe the attribute values of the image
The code is as follows:
Import cv2 as cv image = cv.imread ("F:/picture/lena.png") print ("image.shape", image.shape) # output image size attribute print ("image.size", image.size) # output image pixel number attribute print ("image.dtype", image.dtype) # output image type attribute
The running result of the program is:
Image.shape (512,512,3) image.size 786432 image.dtype uint8 here, the study on "how to use Python and OpenCV for image processing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.