当前位置:网站首页>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)
边栏推荐
- When idea creates a method, it uses annotations to prompt method parameters (param), return value (return), and method function (description)
- CS 611 Game
- DTS migration script sqlserver
- long start = currentTimeMillis();
- Hanoi Tower problem
- 冰河十年前的预测如今被阿里实现了,非常震撼
- Literacy of C language concept knowledge (supplemented and updated from time to time)
- Remote dictionary server (redis) - a kV based NoSQL database management system used as a cache
- Liunx virtual machine environment uses docker to install Oracle database and Navicat to connect
- 使用keytool从.jks文件获取SSL certificate
猜你喜欢

Analysis of T elevator repair in 2022 and simulation test questions of T elevator repair

6. Local - custom filter factory

图扑软件2D与2.5D案例合集|智慧园区、数据中心、SMT 生产线...

Sort ten integers using selection

Use keytool to access the JKS file get SSL certificate

1108. Defanging an IP Address

Six sides tmall has already offered. After a review of my experience, is it really that difficult to enter a big factory?

【云原生】2.2 kubeadm创建集群

Geographic location type of redis

毕业回馈!Apache Doris 社区所有贡献者来领礼品啦!
随机推荐
畢業回饋!Apache Doris 社區所有貢獻者來領禮品啦!
C语言自定义函数的一些注意事项
加快推进工业互联网,图扑“智”绘发展新蓝图
Accelerate the promotion of industrial Internet, and map out a new blueprint for development
C语言概念知识扫盲(不定期补充更新)
Monorepo Sliding methodology: Reference module Hot Update
How to display loading animation when requesting data
The yarn deployment mode depends on the pre upload settings
在Vs Code中搭建JSP开发环境
DTS迁移秘籍-SQLSERVER篇
Chapter 6 infinite series_ Fourier series
毕业回馈!Apache Doris 社区所有贡献者来领礼品啦!
Restframework read and non read sequence processing
Graduation feedback! All contributors of Apache Doris community come to receive gifts!
In 2022, the postgraduate entrance examination ran aground | how to counter attack famous IT enterprises and become a programmer in three months
89--- Dirac delta function
Tidb upgrade version
DTS migration script sqlserver
Database - basic knowledge
VirtualBox 6.1.34 release