改正此代码import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.10.tif',0) def contrast_strech(src,r1,s1,r2,s2): height, width = src.shape dst = np.zeros((height, width), np.uint8) for i in range(0, height): for j in range(0, width): if src[i, j] < r1: dst[i, j] = s1 / r1 * src[i, j] elif src[i, j] < r2: dst[i, j] = (s2 - s1) / (r2 - r1) * (src[i, j] - r1) + s1 else: dst[i, j] = (255 - s2) / (255 - r2) * (src[i, j] - r2) + s2 return dst image1 = contrast_strech(img, np.min(img), 0, np.max(img), 255) image2 = contrast_strech(img, (np.min(img) + np.max(img)) / 2, 0, (np.min(img) + np.max(img)) / 2, 255) plt.figure(figsize=(100,100)) plt.subplot(131) plt.imshow(img,cmap='gray') plt.axis('off') plt.subplot(132) plt.imshow(image1,cmap='gray') plt.axis('off') plt.subplot(133) plt.imshow(image2,cmap='gray') plt.axis('off') plt.show()
时间: 2024-04-26 08:22:34 浏览: 74
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.10.tif',0)
def contrast_stretch(src, r1, s1, r2, s2):
height, width = src.shape
dst = np.zeros((height, width), np.uint8)
for i in range(height):
for j in range(width):
if src[i, j] < r1:
dst[i, j] = s1 / r1 * src[i, j]
elif src[i, j] < r2:
dst[i, j] = (s2 - s1) / (r2 - r1) * (src[i, j] - r1) + s1
else:
dst[i, j] = (255 - s2) / (255 - r2) * (src[i, j] - r2) + s2
return dst
image1 = contrast_stretch(img, np.min(img), 0, np.max(img), 255)
image2 = contrast_stretch(img, (np.min(img) + np.max(img)) / 2, 0, (np.min(img) + np.max(img)) / 2, 255)
plt.figure(figsize=(10,10)) # 修改了 figsize 的值
plt.subplot(131)
plt.imshow(img,cmap='gray')
plt.axis('off')
plt.subplot(132)
plt.imshow(image1,cmap='gray')
plt.axis('off')
plt.subplot(133)
plt.imshow(image2,cmap='gray')
plt.axis('off')
plt.show()
阅读全文