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--
In this article Xiaobian for you to introduce in detail "what is the basic operation of opencv and numpy images", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the basic operation of opencv and numpy images" can help you solve your doubts.
1. Pixel basic operation
1.1 read and modify pixels
You can access pixel data through [row, column] coordinates, for multi-channel data, return an array containing the values of all channels, for single-channel data (such as gray), return values of specified coordinates, or use [row, column, channel index] to access the values of a certain channel in a certain coordinate.
> import cv2 > import numpy as np > img = cv2.imread ('messi5.jpg') > px = img [100100] > print (px) [157166200] # accessing only blue pixel > blue = img [100fui 100L0] > print (blue) 157
Pixel values can be modified directly through coordinates
> img [100100] = [255255255] > > print (img [100100]) [255255255]
However, it is relatively inefficient to read and modify the value of each pixel directly as above, and the following methods can be used to achieve higher efficiency.
# accessing RED value > > img.item > > img.itemset > img.itemset > > img.item
1.2 read image properties
Read the image size and return a tuple (rows, columns, channels)
> > print (img.shape) (342,548,3)
Read pixel size, number of row and column channels
> > print (img.size) 562248
Pixel data type
> print (img.dtype) uint8
1.3Image ROI operation
You can edit the pixel area directly, for example, copy 50 / 50 pixels from the lower left corner of the image to the upper left corner.
Import cv2import numpy as npimg = cv2.imread ("test.jpg") print (img.shape) roiTest = img [475 print 525,0:50] img [0:50, 0:50] = roiTestcv2.imshow ("image", img) cv2.waitKey (0)
1.4 split and merge channels
In some cases, it is necessary to operate on a certain channel data of the image, and the channel data will be segmented and merged.
> bmeme gpene r = cv2.split (img) > img = cv2.merge ((bmeme gjre r))
Or
B = img [:, 0]
Suppose you want to edit the data of the red channel, all set to 0, you don't need to edit it after segmentation, img [:,:, 2] = 0. Cv2.split operation is a very time-consuming operation, you can use numpy index instead of operation, try to use numpy index to do.
1.4 generate image frames
Use the cv2.copyMakeBorder function to add image frames and support multiple frame algorithms
Void cv::copyMakeBorder (InputArray src, / / original image / / target image (in cpp version, if this data is imported and BORDER_TRANSPARENT is selected, the roi part of the data cut by top/bottom/left/right will not be modified. The image size = dst.rows+top+bottom Dst.cols+left+right) the border pixel int bottom,int left,int right,int borderType in the four directions of OutputArray dst, int top, / / top/left/bottom/right. / / the border pixel in the following figure const Scalar & value = Scalar () / / the border pixel when the frame type is BORDER_CONSTANT)
BLUE = [255,0,0] img1 = cv2.imread ("test.jpg") replicate = cv2.copyMakeBorder (img1, 100,100,100,100, cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder (img1, 100,100,100,100, cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder (img1, 100,100,100,100, cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder (img1, 100,100,100,100, cv2.BORDER_WRAP) constant = cv2.copyMakeBorder (img1, 100,100,100,100) Cv2.BORDER_CONSTANT, value=BLUE) print (img1.shape, reflect.shape) plt.subplot, plt.imshow (img1, 'gray'), plt.title (' ORIGINAL') plt.subplot, plt.imshow (replicate, 'gray'), plt.title (' REPLICATE') plt.subplot, plt.imshow (reflect, 'gray'), plt.title (' REFLECT') plt.subplot (234), plt.imshow (reflect101, 'gray') Plt.title ('REFLECT_101') plt.subplot, plt.imshow (wrap,' gray'), plt.title ('WRAP') plt.subplot, plt.imshow (constant,' gray'), plt.title ('CONSTANT') plt.show ()
The above example can more intuitively see the effects of a variety of border, at the same time, we can also find that the python version of api compared with the cpp version, the default initialization of a piece of original image size + direction frame size of the image memory, as a built-in dst parameter.
Output size: (525,700,3) (725,900,3)
two。 Basic arithmetic operation of image
2.1 Image addition
For image addition, two images should have the same shape, or the image should be added to a scalar, or the image should be added to an one-dimensional array with the same number of channels.
When the addition of opencv is different from that of numpy, it is handled differently when it is outside the range of data types.
> x = np.uint8 ([10]) > y = np.uint8 ([10]) > print (cv2.add (X-ray)) # 250mm 10 = 260 = > 255[ 255i] > print (x = 255y) # 250mm = 260% 256 = 4 [4]
Cpp version of api also supports parameters such as mask.
Void cv::add (InputArray src1,InputArray src2,OutputArray dst,InputArray mask = noArray (), int dtype =-1)
2.2 Image mixing
Opencv provides a way to mix two images together through the cv::addWeighted function
Dst= α ⋅ img1+ β ⋅ img2+ γ
Img1 = cv2.imread ('ml.png') img2 = cv2.imread (' opencv-logo.png') dst = cv2.addWeighted (img1,0.7,img2,0.3,0) cv2.imshow ('dst',dst) cv2.waitKey (0) cv2.destroyAllWindows ()
More fine image local fusion can also be done by cv2.seamlessClone function.
Read here, this article "what is the basic operation of opencv and numpy images" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, 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.