当前位置:网站首页>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)
边栏推荐
- Tupu software 2D and 2.5D case collection | smart Park, data center, SMT production line
- Detailed explanation of deep learning technology for building an image search engine that can find similar images
- Some considerations of C language custom function
- Flink deployment mode (I) - standalone and Application
- C language data type conversion rules (implicit conversion + explicit conversion)
- Six sides tmall has already offered. After a review of my experience, is it really that difficult to enter a big factory?
- Graduation feedback! All contributors of Apache Doris community come to receive gifts!
- Flynk deployment mode summary
- It's 2022. Don't you hurry to learn typescript?
- Using Matplotlib to realize GUI interaction effect
猜你喜欢

2022 welder (primary) new version test questions and welder (primary) free test questions

1108. Defanging an IP Address

Database - basic knowledge

数据库---基础知识

In 2022, the third batch (principal) of Guangdong Provincial Safety Officer a certificate was found and analyzed, and the third batch (principal) of Guangdong Provincial Safety Officer a certificate w

Concurrent programming - thread pool

Will swift compile to native code- Does Swift compile to native code?

liunx虚拟机环境使用docker安装oracle数据库,并使用navicat连接

8. Gateway request logging
![[camp] at the beginning, improve [leopard] power - vivo activity plug-in management platform](/img/75/6a129de59c21b783622ba31f77f647.jpg)
[camp] at the beginning, improve [leopard] power - vivo activity plug-in management platform
随机推荐
Pytest (12) -allure common features allure attach、allure. step、fixture、environment、categories
Squoosh - Google's free open source image compression tool, reducing the image size by 90%! Support API development calls
The postmanutils tool class simulates the get and post requests of postman
Graph calculation on nlive:nepal's graph calculation practice
Common knowledge arrangement of numpy database
7. Gateway global filter
yarn部署模式依赖预上传设置
DTS迁移秘籍-SQLSERVER篇
Literacy of C language concept knowledge (supplemented and updated from time to time)
MySQL day03 class notes
Hash type of redis
DTS migration script Oracle
How to remove parentheses in PHP regular
北京密云区知识产权贯标的好处,补贴5-10万
rambbmitmq消费方
Some notes on the use of C language strings
Remote Dictionary Server(Redis)——基于 KV 结构的作为 Cache 使用的 NoSQL 数据库管理系统
Idea创建方法时,使用注解提示方法参数(param)、返回值(return)、方法作用(Description)
在Vs Code中搭建JSP开发环境
Non recursive printing Fibonacci sequence