In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use image pixels in OpenCV, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this OpenCV article on how to use image pixels, let's take a look.
Pixel is an important attribute of image in computer vision. They are values that represent the color intensity of light in a particular space in the image and are the smallest unit of data in the image.
The total number of pixels in an image is the product of height, width, and channel.
Since the image in the OpenCV is read as an Numpy array of pixel values, an array slicing operation can be used to obtain and process the image region represented by the pixels of the region.
Slicing operations are used to retrieve subsets of sequences, such as lists, tuples, and arrays, so they can be used to obtain pixel values of an image region for processing such as editing, formatting, or cropping.
Slicing operation
Script: use the slicing operation to get a subset of the list.
# list of sliced letters letters = ['First three letters,' baked, 'cased,' dashed,'e'] # the first three letters first_three = letters [: 3] # the last three letters last_three = letters [- 3:] # get the second to fourth letters second_to_fourth = letters [1:4] # display the result print ('First three letters:', first_three) print ('Last three letters:' Last_three) print ('Second to fourth letters:', second_to_fourth)
Notice that I use index values to slice the list of letters. For example, passing the starting index 1 (the index of the second letter in the list) and 4 returns a fragment of the list, from the second value to the fourth value.
Because index values are used to retrieve subsets in this way, they are also used to locate and retrieve areas of interest in the image.
Slices targeting areas in the image are defined by the start and end values of the two axes of the image (horizontal (X) and vertical (Y)), in the following format:
Image [startY: endY, startx:endX]
It returns the Numpy array of the desired region of interest (image pixels).
So, how do we determine the start and end values of the X and Y axes of the region of interest?
These values (startX, endX, startY, endY) are coordinate values that map out the region of interest.
When using OpenCV display, these values do not appear next to the image, but we can use other applications (such as Photoshop, Corel Draw, Paint e.t.c) or other python visualization libraries (such as Matplotlib) to display images with X and Y coordinate values.
As always, this has been better understood in practice. Let's use matplotlib to display an image. Pyplot, from which we can retrieve coordinates that map the region of interest of the target in the image.
I use the image of the flag of the Republic of Ghana to prove this. Here, my goal is the area of the image that surrounds the black star.
Get the coordinate values of the region of interest
Load and display images using Matplotlib
# plotimport matplotlib.pyplot as plt imported into matplotlib # load and display the original image image = plt.imread ('Flag_of_Ghana.png') # display picture plt.imshow ("Original", image) plt.plot ()
Output: the loaded image and its X and Y coordinates.
As you can see, the plt.imshow function returns the read image and the coordinate values of the x and y axes.
We can then retrieve the start and end coordinates of the region of interest (black star).
Tracking the coordinate values of the black star region
This image shows how to track the coordinates of the area around the black star.
We can retrieve coordinates (startY (y1), endY (y2), startX (x1), endX (x2)) from the image. We can then define the start and end coordinates of the two axes and crop them to:
Image [y1: y2, x1:x2]
If we get y1, y2 = [145,295] and x1, x2 = [245,400]
Then the area where the black star is drawn will be:
Black_star = image [145VZ 295,245RU 400]
This returns the pixel value (in the Numpy array) that maps the region of interest (in this case, the black star).
Now, we can use this technology to locate and slice the image region for a variety of image processing.
Use the slice operation to crop the image 1. Load and display original image # plotimport matplotlib.pyplot as plt imported into matplotlib # load and display original image image = plt.imread ('Flag_of_Ghana.png') # display picture plt.imshow ("Original", image) plt.plot ()
Output: displays the loaded image.
two。 Get the spatial dimension of the image # get the spatial size of the image # initialize originY, originX = 0,0 # get the height and width of the image height, width = image.shape [: 2] # calculate the center of the image (centerX, centerY) = (width / / 2, height / / 2) print ('Image height:', height) print ('Image width:', width) print ('Center location:', (centerY, centerX))
Output: displays the space size of the image.
3. Crop image
Cut out the upper left corner of the image
# the upper left corner of the cropped image # height from origin to center # width from origin to center top_left = image [originY:centerY, originX:centerX] cv2.imshow ("Top-Left Corner", top_left) cv2.waitKey (0)
Output: upper-left corner of the image
Cut out the upper right corner of the image
# crop the upper right corner # height from origin to center # width from center to upper right corner (width) top_right = image [originY:centerY, centerX:width] cv2.imshow ("Top-Right Corner", top_right) cv2.waitKey (0)
Output: upper right corner of the image
Cut out the lower left corner of the image
# crop the lower left corner # height from center to lower left corner (height) # width from center to upper right corner (width) bottom_left = image [centerY:height, originX:centerX] cv2.imshow ("Bottom-Left Corner", bottom_left) cv2.waitKey (0)
Output: lower left corner of the image
Cut out the lower right corner of the image
# cut the lower right corner # height from center to lower left corner (height) # width from center to upper right corner (width) bottom_right = image [centerY:height, centerX:width] cv2.imshow ("Bottom-Right Corner", bottom_right) cv2.waitKey (0)
Output: lower left corner.
4. Use dimensions to set part of the image to a specific color. # set the upper left corner of the original image to green image [originY:centerY, originX:centerX] = (0,255,0) # display the updated image cv2.imshow ("Updated", image) cv2.waitKey (0)
Output: set the upper left corner to green
This is the end of the article on "how to use Image Pixels in OpenCV". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use image pixels in OpenCV". If you want to learn more, 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.