In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of image arithmetic and logic operations in Python, I believe that most people do not know much about it, so share this article for your reference, I hope you will gain a lot after reading this article, let's go to know it!
one。 Image addition operation
There are two main methods of image addition. The first is to call the Numpy library, and the target image pixel is the sum of the pixels of the two images; the second is to call the add () function through OpenCV. The function prototype of the second method is as follows:
Dst = add (src1, src2 [, dst [, mask [, dtype])
-src1 represents the pixel matrix of the first image
-src2 represents the pixel matrix of the second image
-dst represents the output image, which must have the same size and number of channels as the input image
-mask represents an optional operation mask (an 8-bit single-channel array) that specifies the elements of the output array to change.
-dtype indicates the optional depth of the output array
Note that when the pixel value addition result of two images is less than or equal to 255, the output image is directly assigned to the result, such as 120: 48 assigned to 168; if the addition is greater than 255, the pixel result of the output image is set to 255, such as (255 / 64) assigned to 255. The following code implements the image addition operation.
# coding:utf-8# By:Eastmountimport cv2 import numpy as np # read picture img = cv2.imread ("luo.png") # Image each pixel plus 100m = np.ones (img.shape, dtype= "uint8") * 100#OpenCV addition result = cv2.add (img, m) # display image cv2.imshow ("original", img) cv2.imshow ("result", result) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 4-1, with the original image of "Xiao Luo" on the left and the image after the pixel value is increased by 100 pixels on the right, and the output image is more white.
two。 Image subtraction operation
The image subtraction operation is mainly implemented by calling the subtract () function, and its prototype is as follows:
Dst = subtract (src1, src2 [, dst [, mask [, dtype])
-src1 represents the pixel matrix of the first image
-src2 represents the pixel matrix of the second image
-dst represents the output image, which must have the same size and number of channels as the input image
-mask represents an optional operation mask (an 8-bit single-channel array) that specifies the elements of the output array to change.
-dtype indicates the optional depth of the output array
The specific implementation code is as follows:
# coding:utf-8# By:Eastmountimport cv2 import numpy as np # read picture img = cv2.imread ("luo.png") # Image pixel minus 50m = np.ones (img.shape, dtype= "uint8") * 50#OpenCV subtraction result = cv2.subtract (img, m) # display image cv2.imshow ("original", img) cv2.imshow ("result", result) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 4-2, with the original image on the left and the image on the right after the pixel value is reduced by 50 pixels. The output image is darker.
three。 Image and operation
The and operation is a basic logical operation in the computer, the symbol is expressed as "&", and its operation rules are:
0-0-0
0-1-0
1: 00: 0
1-1-1
The sum operation of image refers to the binary "and" operation of each pixel value of two images (either grayscale image or color image) to realize image clipping.
Dst = bitwise_and (src1, src2 [, dst [, mask]])
-src1 represents the pixel matrix of the first image
-src2 represents the pixel matrix of the second image
-dst represents the output image, which must have the same size and number of channels as the input image
-mask represents an optional operation mask (an 8-bit single-channel array) that specifies the elements of the output array to change.
The following code is to realize the function of image clipping through image and operation.
# coding:utf-8# By:Eastmountimport cv2 import numpy as np # read picture img = cv2.imread ("luo.png", cv2.IMREAD_GRAYSCALE) # get image width and height rows, cols = img.shape [: 2] print (rows, cols) # draw a circle circle = np.zeros ((rows, cols), dtype= "uint8") cv2.circle (circle, (int (rows/2), int (cols/2), 100,255,- 1) print (circle.shape) print (img.size) Circle.size) # OpenCV Image and Operation result = cv2.bitwise_and (img, circle) # display Image cv2.imshow ("original", img) cv2.imshow ("circle", circle) cv2.imshow ("result", result) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 4-3. After the original image is calculated with the circle, the central outline of the original image is extracted. The shape of the output image is 377 × 326 at the same time. Note that the two images must be the same size and type.
four。 Image or operation
Logic or operation means that if one or more operands are true, the logic or operator returns a Boolean value true;. Only if all operands are false, the result is false. The OR operation of an image refers to the binary OR operation of each pixel value of two images (either a grayscale image or a color image) to realize image clipping. The function prototype is as follows:
Dst = bitwise_or (src1, src2 [, dst [, mask]])
-src1 represents the pixel matrix of the first image
-src2 represents the pixel matrix of the second image
-dst represents the output image, which must have the same size and number of channels as the input image
-mask represents an optional operation mask (an 8-bit single-channel array) that specifies the elements of the output array to change.
The following code is to realize the function of image clipping through image or operation.
# coding:utf-8# By:Eastmountimport cv2 import numpy as np # read picture img = cv2.imread ("luo.png", cv2.IMREAD_GRAYSCALE) # get image width and height rows, cols = img.shape [: 2] # draw a circle circle = np.zeros ((rows, cols), dtype= "uint8") cv2.circle (circle, (int (rows/2), int (cols/2), 100,255,- 1) # OpenCV image or operation result = cv2.bitwise_or (img) Circle) # display image cv2.imshow ("original", img) cv2.imshow ("circle", circle) cv2.imshow ("result", result) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 4-4. After the original image is operated with the circle, the pixel value of the image except the central prototype is extracted.
five。 Image non-operation
Image non-operation is the pixel inverse color processing of the image, which converts the black pixel of the original image into white pixel, and the white pixel into black pixel. The function prototype is as follows:
Dst = bitwise_not (src1, src2 [, dst [, mask]])
-src1 represents the pixel matrix of the first image
-src2 represents the pixel matrix of the second image
-dst represents the output image, which must have the same size and number of channels as the input image
-mask represents an optional operation mask (an 8-bit single-channel array) that specifies the elements of the output array to change.
The implementation code of the image non-operation is as follows.
# coding:utf-8import cv2 import numpy as np # read picture img = cv2.imread ("Lena.png", cv2.IMREAD_GRAYSCALE) # OpenCV image non-operational result = cv2.bitwise_not (img) # display image cv2.imshow ("original", img) cv2.imshow ("result", result) # wait for cv2.waitKey (0) cv2.destroyAllWindows ()
The output of the original image after non-operation is shown in figure 4-5.
six。 Image XOR operation
Logical xor is a mathematical operator with the mathematical symbol "⊕" and the computer symbol "xor". Its algorithm is: if the values of an and b are different, the XOR result is 1; if the values of an and b are the same, the XOR result is 0.
The XOR operation of an image refers to the binary XOR operation of each pixel value of two images (either grayscale image or color image) to realize image clipping. The function prototype is as follows:
Dst = bitwise_xor (src1, src2 [, dst [, mask]])
-src1 represents the pixel matrix of the first image
-src2 represents the pixel matrix of the second image
-dst represents the output image, which must have the same size and number of channels as the input image
-mask represents an optional operation mask (an 8-bit single-channel array) that specifies the elements of the output array to change.
The implementation code of the image XOR operation is as follows.
# coding:utf-8# By:Eastmountimport cv2 import numpy as np # read picture img = cv2.imread ("luo.png", cv2.IMREAD_GRAYSCALE) # get image width and height rows, cols = img.shape [: 2] # draw a circle circle = np.zeros ((rows, cols), dtype= "uint8") cv2.circle (circle, (int (rows/2), int (cols/2), 100,255,-1) # OpenCV image XOR operation result = cv2.bitwise_xor (img Circle) # display image cv2.imshow ("original", img) cv2.imshow ("circle", circle) cv2.imshow ("result", result) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output of the original image after XOR operation with the circle is shown in figure 4-6.
The above is all the contents of the article "example Analysis of Image arithmetic and Logic Operation in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.