In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to achieve the introduction to OpenCV, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
OpenCV
OpenCV is an open source library originally developed by Intel. It contains convenient methods and functions to support computer vision and machine learning.
I will concentrate on learning how to read an image, how to display an image in Jupyter notebook, and how to check and change some of its properties.
Import cv2import numpy as npimport matplotlib.pyplot as plt
Let's start loading the picture from .imread, and then we can use .imshow to display it in a new window.
Image = cv2.imread ('img.jpg') cv2.imshow (' Some title', image) cv2.waitKey (0) cv2.destroyAllWindows ()
The waitkey and .destroyAllWindows methods are critical for running code without crashing.
The first command tells Jupyter to continue running the block until a key is pressed, and the second command closes the window at the end.
We can also try to display the image using Matplotlib.imshow; in this way, it will be displayed inline rather than in a new window.
Image = cv2.imread ('img.jpg') plt.imshow (image)
It looks strange. The colors are all messed up.
OpenCV loads images as Numpy arrays, and they have three dimensions: red, green, and blue. Dimensions are often called channels, and they hold values between 0 and 255, representing the color intensity of each pixel.
> print (type (image)) > print (image.shape) (776,960,3)
Which means it's RGB, right?
It's not true. This is BGR, it's the same thing, but in a different order.
Matplotlib uses RGB, which is why our pictures look strange. This is not a problem because OpenCV has some very convenient ways to convert colors.
Image = cv2.imread ('img.jpg') image = cv2.cvtColor (image, cv2.COLOR_BGR2RGB) plt.imshow (image)
It's so cool that we have to use OpenCV to read and display images and learn how to convert GBR colors to RGB to display them inline with Matplolib.
Other color formats can be processed using OpenCV, such as HSV,CMYK, and so on.
color
Since we will repeat it many times, we create a method of drawing using Matplotlib. We can set the size of the diagram and delete the axis to make it better.
Def show (img): fig, ax = plt.subplots (1, figsize= (122.8)) ax.axis ('off') plt.imshow (img, cmap='Greys')
Notice that I also define the color graph in .imshow as "gray". This parameter will be ignored when we draw the RGB image, but will be useful later when drawing the various dimensions of the array. Now, let's try our method.
Image = cv2.imread ('img2.jpeg') image = cv2.cvtColor (image, cv2.COLOR_BGR2RGB) show (image)
Now let's try to convert it to grayscale and then to RGB.
Image = cv2.imread ('img2.jpeg') gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) image = cv2.cvtColor (gray, cv2.COLOR_GRAY2RGB) show (image)
We can use .split to get a single array of colors and then combine the picture with .merge. This is useful for modifying, checking, and filtering individual dimensions of an array.
For example, we can multiply the array by zero to delete it
Img = cv2.imread ('img2.jpeg') B, G, R = cv2.split (img) img = cv2.merge ([Bath0, G, Rene0]) img = cv2.cvtColor (img, cv2.COLOR_BGR2RGB) show (img)
We can increase or decrease the intensity of the color, or build a new Numpy array with the same shape to replace it, or you can consider using any other method.
Img = cv2.merge ([np.ones_like (B) * 255G, R]) img = cv2.cvtColor (img, cv2.COLOR_BGR2RGB) show (img)
The same concepts of split and merge apply to other formats, such as HSV and HSL.
Img = cv2.imread ('img2.jpeg') img = cv2.cvtColor (img, cv2.COLOR_BGR2HSV) H, S, V = cv2.split (img) img = cv2.merge ([np.ones_like (H) * 30, Song10, Vmur20]) img = cv2.cvtColor (img, cv2.COLOR_HSV2RGB) show (img)
HSV: hue, saturation and lightness.
This format is suitable for tones, so it is easy to filter colors-which means that we can use the angle range without having to calculate the combination range between red, green, and blue.
We can use Numpy to define the upper and lower boundaries of HSV. Apply the function .inRange to filter these values and create a mask. We can then use .bitwise _ and to apply this mask in a saturated state, which turns everything outside the boundary to zero.
In other words: we can filter some colors and set all the rest to grayscale.
# read img and convert to HSVimg = cv2.imread ('img2.jpeg') img = cv2.cvtColor (img, cv2.COLOR_BGR2HSV) # split dimensionsH, S, V = cv2.split (img) # upper and lower boundarieslower = np.array ([80,0,0]) upper = np.array ([120,255,255]) # build maskmask = cv2.inRange (img, lower, upper) # apply mask to saturationS = cv2.bitwise_and (S, S, mask=mask) # assemble imageimg = cv2.merge ([H, S V]) # convert to RGB and displayimg = cv2.cvtColor (img, cv2.COLOR_HSV2RGB) show (img)
Segmenting an image also makes it easier for us to check its composition.
We can draw colors from RGB, saturation from HSV, or any other channel we want.
Img = cv2.imread ('img2.jpeg') B, G, R = cv2.split (img) show (B) img = cv2.cvtColor (img, cv2.COLOR_BGR2HSV) H, S, V = cv2.split (img) show (S)
When using the Gray color chart, the values range from white (low) to black (high).
By looking at the first map, we can see that the blue intensity on the ground is higher than that in the building, and the saturation map shows that the value around the skateboard is higher than that of the rest of the image.
The above content is how to get started with OpenCV. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow 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.