写了一个python的代码,正确结果应该是原图直方图、线性拉伸直方图、原图、线性拉伸图
但是运行完显示的结果,线性拉伸图的位置显示的还是原图,检查了好多遍也没发现错误,可以帮忙检查哪里错啦吗
(我也不会像别人呢样直接上传一键复制的代码)
import cv2
import numpy as np
import matplotlib.pyplot as plt
def linear_img(img):
img = img
w, h = img.shape[:2] # 读取灰度图高宽
maxout = 80
minout = 20
gray_new = (img - img.min()) / (img.max() - img.min()) # 归一化到0-1
gray_out = gray_new * (maxout - minout) # maxout=255 minout=0
gray_out = np.uint8(gray_out)
def GrayHist( img ):
# 计算灰度直方图
height, width = img.shape[:2]
grayHist = np.zeros([256], np.uint64)
for i in range(height):
for j in range(width):
grayHist[img[i][j]] += 1
return grayHist
if name == 'main':
# 读取原始图像
img = cv2.imread("img",0)
#线性拉伸
gray_out = linear_img(img)
#计算原图灰度直方图
origin = GrayHist(img)
linear = GrayHist(gray_out)
x = np.arange(256)
# 绘制灰度直方图
plt.figure( num = 1 )
plt.subplot( 2, 2, 1 )
plt.bar(x, origin,color='blue')
plt.title("Origin")
plt.ylabel("number of pixels")
plt.subplot( 2, 2, 2 )
plt.bar(x, linear,color='blue')
plt.title("linear")
plt.ylabel("number of pixels")
plt.subplot( 2, 2, 3 )
plt.imshow( img,cmap = plt.cm.gray)
plt.title( 'Origin' )
plt.subplot( 2, 2, 4 )
plt.imshow( gray_out, cmap = plt.cm.gray)
plt.title( 'linear')
plt.show()