Skip to content

Commit 1751cb9

Browse files
authored
Add files via upload
1 parent db14e4c commit 1751cb9

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

blog37-histogram/blog37-11.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#coding:utf-8
2+
#By:Eastmount CSDN 2021-02-05
3+
import cv2
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
#读取图像
8+
img = cv2.imread('lena.bmp')
9+
10+
#图像灰度转换
11+
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
12+
13+
#二进制阈值化处理
14+
r, result = cv2.threshold(grayImage, 127, 255, cv2.THRESH_BINARY)
15+
16+
#计算原图的直方图
17+
hist = cv2.calcHist([img], [0], None, [256], [0,256])
18+
19+
#计算阈值化处理的直方图
20+
hist_res = cv2.calcHist([result], [0], None, [256], [0,256])
21+
22+
#原始图像
23+
plt.figure(figsize=(8, 6))
24+
plt.subplot(221), plt.imshow(img, 'gray'), plt.title("(a)"), plt.axis('off')
25+
26+
#绘制原始图像直方图
27+
plt.subplot(222), plt.plot(hist), plt.title("(b)"), plt.xlabel("x"), plt.ylabel("y")
28+
29+
#阈值化处理后的图像
30+
plt.subplot(223), plt.imshow(result, 'gray'), plt.title("(c)"), plt.axis('off')
31+
32+
#阈值化处理图像的直方图
33+
plt.subplot(224), plt.plot(hist_res), plt.title("(d)"), plt.xlabel("x"), plt.ylabel("y")
34+
plt.show()

0 commit comments

Comments
 (0)