Skip to content

Commit bcffe3b

Browse files
committed
opencv
opencv
1 parent ead8743 commit bcffe3b

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

python_advance/main.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
* [Python垃圾回收机制](Python垃圾回收机制.md)
2323
* [Python上下文管理器与with语句](Python上下文管理器与with语句.md)
2424
* [Python实现单例模式](Python实现单例模式.md)
25+
* [Python opencv基础](opencv-python基础.md)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# opencv-python基础
2+
* pip install opencv-python
3+
4+
## 1.图像的读取、显示、保存
5+
```python
6+
import cv2
7+
8+
if __name__ == '__main__':
9+
10+
# 1:加载彩色图像 0:灰度模式 -1:加载图像,包括alpha通道
11+
img = cv2.imread("test.jpg", -1)
12+
cv2.namedWindow('window1', cv2.WINDOW_NORMAL) # 创建一个默认大小的窗口
13+
cv2.imshow('window1', img) # 窗口显示图像
14+
key_event = cv2.waitKey(0) # 键盘绑定,点击键盘程序继续执行
15+
if key_event == 27:
16+
# ESC按键
17+
cv2.destroyAllWindows() # 关闭所有窗口
18+
elif key_event == ord('s'):
19+
# 按键s
20+
cv2.imwrite('new_test.jpg', img) # 写图片
21+
cv2.destroyAllWindows()
22+
```
23+
24+
## 2.绘制直线、圆、矩形、多边形,文本
25+
```python
26+
import cv2
27+
import numpy as np
28+
29+
30+
if __name__ == '__main__':
31+
# 创建一个高512,宽512的黑色图片
32+
img = np.zeros((512, 512, 3), np.uint8)
33+
# 从左上角到右下角画一条直线,厚度为5像素,蓝色(255, 0, 0)
34+
cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
35+
36+
# 画矩形,指定矩形的左上角和右下角,绿色(0, 255, 0)
37+
cv2.rectangle(img, (300, 0), (500, 100), (0, 255, 0), 3)
38+
39+
# 画一个圆,指定圆心和半径,红色(0, 0, 255)
40+
cv2.circle(img, (300, 100), 150, (0, 0, 255), 3)
41+
42+
# 画一个椭圆
43+
cv2.ellipse(img, (300, 100), (100, 50), 0, 0, 180, 255, -1)
44+
45+
# 画多边形,首先需要顶点的坐标,将这些点组成形状为rowsx1x2的数组,其中rows是顶点数,并且其类型应为int32。
46+
pts = np.array([[10, 10], [100, 10], [100, 150], [10, 200]], np.int32)
47+
pts = pts.reshape((-1, 1, 2))
48+
# True是闭合的折线,False不闭合
49+
cv2.polylines(img, [pts], False, (0, 255, 255))
50+
51+
# 添加文本,指定文本内容,左下角坐标,字体,大小,颜色,厚度,线性
52+
font = cv2.FONT_HERSHEY_SIMPLEX
53+
cv2.putText(img, '123321123ddd', (0, 500), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
54+
55+
cv2.imshow('window1', img)
56+
key_event = cv2.waitKey(0)
57+
cv2.destroyAllWindows()
58+
```
59+
60+
## 3.访问修改像素值
61+
```python
62+
import time
63+
import cv2
64+
import numpy as np
65+
66+
if __name__ == '__main__':
67+
img = cv2.imread('roi.jpg')
68+
# 通过行列坐标访问像素,BGR图像返回一个[蓝色,绿色,红色值]的数组,灰度图像仅返回相应的强度
69+
px = img[100, 100]
70+
print(px)
71+
# 修改像素
72+
img[100, 100] = [255, 255, 255]
73+
print(img[100, 100])
74+
# 图像的形状,数据类型
75+
print(img.shape, img.dtype)
76+
77+
# 替换指定区域图像
78+
ball = img[220:280, 270:330]
79+
img[100:160, 100:160] = ball
80+
81+
# 分割合并图像通道,一般用numpy索引 或者:b = img[:, :, 0]
82+
b, g, r = cv2.split(img)
83+
img = cv2.merge((b, g, r))
84+
85+
cv2.imshow('window1', img)
86+
key_event = cv2.waitKey(0)
87+
cv2.destroyAllWindows()
88+
89+
# 将红色通道全部设置为0
90+
img[:, :, 2] = 0
91+
92+
cv2.imshow('window1', img) # 窗口显示图像
93+
key_event = cv2.waitKey(0)
94+
cv2.destroyAllWindows()
95+
```

0 commit comments

Comments
 (0)