当前位置:网站首页>Opencv function usage details 1~10, including code examples
Opencv function usage details 1~10, including code examples
2022-06-22 05:17:00 【Vegetable chicken Saint dragon evolution vegetable squirrel】
opencv Common methods 1
1.cv2.imread()
Read image ( Read image ; The first parameter is the image path ; The second is cv2.IMREAD_COLOR: Read in the color image ;cv2.IMREAD_GRAYSCALE: Read in grayscale image .)
img_bg=cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG")
print(img_bgr)
If you print img_bgr, Will output its pixels
2.cv2.imshow()
Display images (image1 It's the window name ,img_bgr Is the image to display )
cv2.imshow('image1', img_bgr)
3.cv2.waitKey()
For waiting for the key , When the user presses the keyboard , This statement will be executed , And get its return value delay > 0 when , Program at given delay Wait for the user's key to trigger within the time perhaps Waiting for one delay Time , Program continues . if delay = 0 when , It means that the user must click the keyboard to trigger the program to continue .
cv2.waitKey(5000) # 5000ms,5s Program continues , Of course, you can click the keyboard to execute immediately
4.cv2.destroyWindow()
Used to release or destroy the specified window ,winname It's the window name
None = cv2.destroyWindow(winname)
5.cv2.destroyAllWindows()
Used to release or destroy all windows
None = cv2.destroyAllWindows()
6.cv2.imwwrite()
Used to save images ,retval Is the return value . If saved successfully , Returns the logical value true (True); If the save is unsuccessful , Returns the logical value false (False)
filename Is the path name of the target file to be saved , Include file extensions .img Is the name of the saved image .
params Is to save type parameters , It's optional .
retval = cv2.imwwrite(filename,img[, params])
Runnable cases
import cv2 as cv
import numpy as np
img = np.ones([400, 400, 1], np.uint8)
img = img*0
# img[:, :, 0] = np.ones([400, 400])*127
cv.imshow("new image", img)
cv.waitKey(5000) # 5000ms,5s Program continues , Of course, you can click the keyboard to execute immediately
cv.imwrite("D:/myImage.png", img)
7. Image classification
1. Binary image
Binary image ( Only black and white are included )
White pixels are 1, Black pixels are 0
2. Grayscale image
Grayscale image ( Grayscale processing as 256 Gray scale ( It's exactly a byte ) Use numerical interval [0,255] Express ),
among .[0] It means pure black ,[255] It means pure white
3. Color images
Color images Three primary colors ( red 、 green 、 blue )
RGB In the color space , There is R the red channel 、G Green channel 、B Blue channel
After different combinations, common 256256256=16777216 Color
The channel order is R->G->B for example (205,89,68)
8. Pixel processing
# Generate a 8*8 Two dimensional array of , All values are 0,,, The data type is np.uint8,( In fact, it can be seen as a black image .)
#img[0, 3] = 255 It will be the first 0 Xing di 3 The pixels of the column are set to 255
#img = np.zeros((8, 8), dtype=np.uint8)
import cv2
import numpy as np
img = np.zeros((8, 8), dtype=np.uint8)
print("img = \n", img)
cv2.imshow("one",img)
print(" Read pixels img[0, 3]=", img[0, 3])
img[0, 3] = 255
print(" After modification img = \n", img)
print(" Read the modified pixels img [0, 3] = ",img[0, 3])
cv2.imshow("two", img)
cv2.waitKey()
cv2.destroyAllWindows()
9. Editing an image
( Is to change a whole piece into a certain color )
import cv2
img = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG", 0)
cv2.imshow("before", img)
for i in range(10, 100):
for j in range(80, 100):
img[i, j] = 255
cv2.imshow("after", img)
cv2.waitKey()
cv2.destroyAllWindows()
10. Create three monochrome pictures
among ! The first two : Represents the number of all lines of the selected picture , And number of columns ,1 On behalf of the 2 Channels (0 Represents the first channel ).
If there is a picture img, be img[20:30, 60:70, 1] It means the... Of the selected picture 20 To 30 That's ok , The first 60 To 70 Column , And the first 2 Channels .
#[300( Number of lines in the picture ),300( The number of columns in the picture ,3( The number of channels in the picture )]
import numpy as np
import cv2
# ---------------blue---------------
blue = np.zeros((300, 300, 3), dtype=np.uint8)
blue[:, :, 0] = 255
print("blue = \n", blue)
cv2.imshow("blue", blue)
# -------------green---------------
green = np.zeros((300, 300, 3), dtype=np.uint8)
green[:, :, 1] = 255
print("green = \n", green)
cv2.imshow("green", green)
# -------------red---------------
red = np.zeros((300, 300, 3), dtype=np.uint8)
red[:, :, 2] = 255
print("red = \n", red)
cv2.imshow("red", red)
# ------------- release window ---------------
cv2.waitKey()
cv2.destroyAllWindows()
边栏推荐
- DTS migration script sqlserver
- 2022 new test questions for tea specialists (primary) and summary of tea specialists (primary) examination
- Software architecture and pattern: structure, component and relationship
- Tidb performance overview panel
- 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
- Graduation feedback! All contributors of Apache Doris community come to receive gifts!
- 风阀执行EN 1634-1耐火测试完整性能坚持 4小时吗?
- Monorepo Sliding methodology: Reference module Hot Update
- 汉诺塔问题
- Flink deployment mode (I) - standalone and Application
猜你喜欢

JS regular expression to implement the thousands separator
![Force buckle 33 [search rotation sort array]](/img/0f/d7e2f2fdf48bcd70e6c69bca7d4002.png)
Force buckle 33 [search rotation sort array]

【chrome】 谷歌小技巧 谷歌浏览器 自带 滚动截图(全屏截图)
Please, use three JS make 2D pictures have 3D effect cool, OK

flink部署模式总结

C语言变量的存储方式和生存期

TIDB-performance overview面板

Accelerate the promotion of industrial Internet, and map out a new blueprint for development

畢業回饋!Apache Doris 社區所有貢獻者來領禮品啦!

In 2022, the super intern plans to make a breakthrough in the offer of it famous enterprises, and the nine high salary skills help the dream of famous enterprises
随机推荐
7. Gateway global filter
php正则怎么去掉括号内容
Free questions for polymerization process and test papers for polymerization process in 2022
8. Gateway request logging
6. Local - custom filter factory
Remote dictionary server (redis) - a kV based NoSQL database management system used as a cache
Understanding of service container, service provider and facade of laravel
How to display loading animation when requesting data
Pytest (12) -allure common features allure attach、allure. step、fixture、environment、categories
C语言字符串的一些使用注意事项
Detailed explanation of deep learning technology for building an image search engine that can find similar images
The yarn deployment mode depends on the pre upload settings
DTS迁移秘籍-SQLSERVER篇
Analysis of T elevator repair in 2022 and simulation test questions of T elevator repair
Some considerations of C language custom function
【chrome】 谷歌小技巧 谷歌浏览器 自带 滚动截图(全屏截图)
SQL分类,用户属性
在Vs Code中搭建JSP开发环境
Topic selection system of college graduation design based on SSM
[chrome] Google tips Google browser comes with scrolling screenshot (full screen shot)