Python computer vision -- OpenCV for image arithmetic and logical operation

catalogue

1. Image addition

(1) Randomly generated gray image

(2) Read original image

(3) Add two images

(4) Image pixel addition constant

2. Image subtraction

(1) Reduce brightness

(2) Restore image with grayscale image added  

3. Image and operation

4. Image or operation

5. Image XOR operation

6. Image non operation

7. Significance of image logic operation

Conclusion

1. Image addition

Image addition operation is to add an image to another image or operate a pixel value of the image, and add a constant to change its brightness or color

(1) Randomly generated gray image

reference material: Pixel addition and subtraction of image

import numpy as np
import cv2
#Np.random.random (a, b) generates random integers greater than or equal to a and less than B
img_gray = np.random.randint(0, 256, size=[406, 600], dtype=np.uint8)#Generate a 406 * 600 random gray image
cv2.namedWindow("gray")
cv2.imshow("gray", img_gray)
cv2.waitKey(delay = 0)

(2) Read original image

First, get the downloaded image from the computer:

import numpy as np
import cv2

img1 = cv2.imread('E:\From Zhihu\For the desk\de.jpg') #Read image
cv2.namedWindow("awindow")    #create a window
img1_1 = cv2.resize(img1, dsize= None, fx = 0.5, fy = 0.5)#Adjust image size
cv2.imshow("awindow", img1_1)   #Display image
cv2.waitKey(delay = 0)    #wait for
print(img1_1.shape)   #Read image information

And get the image information:

(406, 600, 3)   The resolution and channel of the image. The previous gray image is generated based on the size of this image  

  (3) Add two images

  Use the cv2.addWeighted() function

The function includes parameters: image 1, image 1 weight, image 2, image 2 weight, which is the number added to the pixel value of the synthesized image

When using this function, the resolution of the two images must be the same, and the number of image channels must be the same

In this example, the above gray image and the read image are added

The main implementation methods are as follows. I have added comments

"""
Author:Huijun
date:2021/10/15
"""
import numpy as np
import cv2

img1 = cv2.imread('E:\From Zhihu\For the desk\de.jpg')  #Read image
cv2.namedWindow("awindow") #create a window
img1_1 = cv2.resize(img1, dsize= None, fx = 0.5, fy = 0.5)#Resize
img_gray0 = cv2.cvtColor(img1_1, cv2.COLOR_BGR2GRAY)          #Gray processing of image
cv2.imshow("awindow", img_gray0)   #Display image
cv2.waitKey(delay = 0) #wait for

img_gray = np.random.randint(0, 256, size=[406, 600], dtype=np.uint8)#Generate random grayscale image
cv2.namedWindow("gray") 
cv2.imshow("gray", img_gray)
print(img_gray.shape) #Read grayscale attributes
cv2.waitKey(delay = 0)

final_img = cv2.addWeighted(img_gray0, 0.8, img_gray, 0.2, 8) #Add two graphs
cv2.namedWindow("windowfinal")
cv2.imshow("windowfinal", final_img)
cv2.waitKey(delay = 0)

Get the final image:

It can be seen that the two images are added, but the beauty can still be distinguished. This is because we adjust the weight of the beauty to 0.8 when adding, so the beauty is mainly displayed in the whole picture

If you modify the weight:

final_img = cv2.addWeighted(img_gray0, 0.5, img_gray, 0.5, 8)

Let the two be fifty-five to get the following image:  

(4) Image pixel addition constant

As mentioned in the previous article, the pixel value of an image represents its brightness

Here we try to add 50 to each pixel and compare it with the original image

First, we have to create a matrix of the same size as the original drawing

Mix = np.ones(img1_1.shape, dtype=np.uint8)*50 #Generate an identity matrix and multiply by 50, identity matrix dimension and img1_1 same
Addimg = cv2.add(Mix, img1_1)   #Add 50 to each pixel
cv2.namedWindow("Addimg")
cv2.imshow("Addimg", Addimg)
cv2.waitKey(delay = 0)

The resulting images are as follows:  

2. Image subtraction

(1) Reduce brightness

cv2.subtract() function can realize subtraction between images

Let's restore the above image:

img1_2 = cv2.subtract(Addimg, Mix)  #Subtract the matrix from the resulting image
cv2.namedWindow("subimg")
cv2.imshow("subimg", img1_2)
cv2.waitKey(delay = 0)

  The results are as follows:

It can be seen that the top image restores the image in the middle  

(2) Restore image with grayscale image added  

In front, we add the gray image and the original image to get a fuzzy image. Here we try to restore it

img1_3 = cv2.subtract(final_img, img_gray)
cv2.namedWindow("subimg1")
cv2.imshow("subimg1", img1_3)
cv2.waitKey(delay = 0)

The final image obtained is as follows (subimg1 window):

  It can be seen that the image obtained by subtracting the gray image does not restore the original image. This is because we added the two images according to the weight, so directly subtracting the gray image will reduce the brightness of the whole image

3. Image and operation

Reference materials in image operation: openCV -- Python(6) -- image arithmetic and logical operation

img1_4 = cv2.bitwise_and(img_gray0, img_gray)  #And operation of two images
cv2.namedWindow("And")
cv2.imshow("And", img1_4)
cv2.waitKey(delay = 0)

The resulting images are as follows:

Since the generated gray image is a single channel image, it is necessary to convert the original image into a gray image before it can be matched, and then read in a three channel image for and operation:

The code is roughly as follows:

"""
Author:Huijun
date:2021/10/15
"""
import numpy as np
import cv2

img1 = cv2.imread('E:\From Zhihu\For the desk\de.jpg')
cv2.namedWindow("beauty0")
img1_1 = cv2.resize(img1, dsize= None, fx = 0.5, fy = 0.5)
cv2.imshow("beauty0", img1_1)
print(img1_1.shape)
cv2.waitKey(delay = 0)

img1_5 = cv2.imread('E:\From Zhihu\For the desk\de1.jpg') 
img1_50 = cv2.resize(img1_5, dsize= None, fx = 0.834, fy = 0.903) #Resize to match previous image
cv2.namedWindow("beauty1")
cv2.imshow("beauty1", img1_50)
cv2.waitKey(delay = 0)
print(img1_50.shape)

img1_51 = cv2.bitwise_and(img1_1, img1_50)
cv2.namedWindow("Add1")
cv2.imshow("Add1", img1_51)
cv2.waitKey(delay = 0)

  Image obtained:

. . . . . It's a big spectrum! beauty0 and beauty1 are gone

4. Image or operation

The operation mode is the same as the image operation, but the name of the function has changed

img1_51 = cv2.bitwise_or(img1_1, img1_50) #Or operation on two images

Final image obtained:
  

5. Image XOR operation

img1_51 = cv2.bitwise_xor(img1_1, img1_50) #XOR two images

The resulting images are as follows:  

6. Image non operation

Non operation on an image

img1_51 = cv2.bitwise_not(img1_1) #Non operation on the read image
cv2.namedWindow("Not")
cv2.imshow("Not", img1_51)
cv2.waitKey(delay = 0)

7. Significance of image logic operation

reference material: Know how to answer

Image processing - logic operation

First of all, simple arithmetic operation is to process the pixel value for an image, add or subtract some elements for an image to observe the change or difference of the image

In the logical operation, the intersecting part of the two images can be obtained by the and operation, that is, if the corresponding pixels are true at the same time, the result is true;

Non operation mainly changes black into white and white into black, which is very effective for gray image processing;

Or operation can calculate the combination of two images. When one pixel is true, the result is true;

XOR operation when two pixels are the same, the result is true and the result is false at different times, the disjoint parts of the two images can be obtained, and the images can be deducted or other operations.

Conclusion

This article mainly summarizes some arithmetic operations and logical operations in the process of image processing, and briefly introduces their significance, mainly involving some basic operations, so as to lay a foundation for later learning; For some reference materials, I have also added them in the article. If you want to have an in-depth understanding, you can click in to learn.

Tags: Python Computer Vision image processing

Posted on Sat, 16 Oct 2021 00:51:15 -0400 by sk8erh4x0r