Skip to content

Commit 2cf8fce

Browse files
authored
Create blog20-image01.py
1 parent f13ac94 commit 2cf8fce

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

blog20-mark/blog20-image01.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
#读取原始图像
7+
img = cv2.imread('lena.png')
8+
9+
#获取图像高度和宽度
10+
height = img.shape[0]
11+
width = img.shape[1]
12+
13+
#创建一幅图像
14+
new_img = np.zeros((height, width, 3), np.uint8)
15+
16+
#图像量化操作 量化等级为2
17+
for i in range(height):
18+
for j in range(width):
19+
for k in range(3): #对应BGR三分量
20+
if img[i, j][k] < 128:
21+
gray = 0
22+
else:
23+
gray = 128
24+
new_img[i, j][k] = np.uint8(gray)
25+
26+
#显示图像
27+
cv2.imshow("src", img)
28+
cv2.imshow("", new_img)
29+
30+
#等待显示
31+
cv2.waitKey(0)
32+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)