当前位置:网站首页>Detailed explanation of OpenCV function usage 11~20, including code examples

Detailed explanation of OpenCV function usage 11~20, including code examples

2022-06-22 05:17:00 Vegetable chicken Saint dragon evolution vegetable squirrel

opencv2

1.item()

The parameters are the rows and columns that access the pixels of the image ( That's ok , Column )

If you are accessing a color image , Then you need to supplement the channel information ( That's ok , Column , passageway )

# The parameters are index values ( It can also be a ternary index ), And the new value 
itemset()     
#  The first two parameters are the range of random numbers , After a size[,] It's lines and columns , Finally, the data type 
random.randint()   

Example

import numpy as np
img = np.random.randint(10, 99, size=[5, 5], dtype=np.uint8)
print(" Read pixels  img.item(3, 2) = ", img.item(3, 2))
img.itemset((3, 2), 255)
print(" Read the modified pixels  img.item(3, 2) = ", img.item(3, 2))

2. Generate a color image of random values

import cv2
import numpy as np
img = np.random.randint(0, 256, size=[256, 256, 3], dtype=np.uint8)
cv2.imshow("demo", img)
cv2.waitKey()
cv2.destroyAllWindows()

3. Region of interest (RIO)

#img[200:200, 200:400]
# It represents a square area in a box 

import numpy as np
img = np.random.randint(10, 99, size=[5, 5], dtype=np.uint8)
a = img[200:400, 200:400]     # python The assignment of is really powerful !!!
img[200:400, 600:800] = a

4. Code a square ( Code example )

import cv2
import numpy as np
a = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG", cv2.IMREAD_UNCHANGED)
cv2.imshow("original", a)
face = np.random.randint(0, 256, (180,100,3))
a[220:400, 250:350] = face
cv2.imshow("result", a)
cv2.waitKey()
cv2.destroyAllWindows()

5. Index splitting of channel splitting

import cv2
lena = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG")
cv2.imshow("lena", lena)
b = lena[:, :, 0]
g = lena[:, :, 1]
r = lena[:, :, 2]
# Split 
cv2.imshow("b", b)
cv2.imshow("g", g)
cv2.imshow("r", r)
lena[:, :, 0] = 0 #  This change value is still a little unknown 
cv2.imshow("lenab0", lena)
lena[:, :, 1] = 0  # Now I understand 
cv2.imshow("lenabog0",lena)
cv2.waitKey()
cv2.destroyAllWindows()

6. Function splitting of channel splitting

#b, g, r = cv2.split(img)

# Equivalent to 
#b = cv2.split(img)[0]
#g = cv2.split(img)[1]
#r = cv2.split(img)[2]

7. Channel merging

The order of consolidation is :B—>G—>R

If you change the channel order , You will get different patterns

bgr = cv2.merge([b,g,r])

8. Image properties

shape: I know , It can be judged as grayscale ( Binary image ) Or color images

size: Returns the number of pixels

dtype: Returns the data type of the image

import cv2
gray = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG")
print("shape = ", gray.shape)
print("size = ", gray.size)
print("dtype = ", gray.dtype)

9. Change shape

m1 = np.ones([3, 3], np.uint8)
m1.fill(199)       #  Fill the array 
print(m1)

m2 = m1.reshape([1, 9])       # Into a one-dimensional array 
print(m2)

10. Image addition

The pixel values are added and the module is automatically taken (254+58)= 63

(a + b) = mod(a+b, 256), Just write it alone + that will do

add(a+b) It is , Maximum 256

import cv2
a = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG")
b = a
r1 = a+b
r2 = cv2.add(a, b)
cv2.imshow("o",a)
cv2.imshow("r1",r1)
cv2.imshow("r2",r2)
cv2.waitKey()
cv2.destroyAllWindows()

Other arithmetic functions

# Image subtraction :
cv2.subtract(m1, m2)
# except :
cv2.divide(m1, m2)
# ride :
cv2.multip(m1, m2)
# Calculating mean :( Proportion of each channel )
cv2.mean(m1)
# Variance estimation :( Show pixel differences )
cv2.meanStdDev(m1)
原网站

版权声明
本文为[Vegetable chicken Saint dragon evolution vegetable squirrel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220951068128.html