改正此代码import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.8.tif', 0) def gamma_transformation(src,gamma): height, width = src.shape dst = np.zeros((height, width), np.uint8) cv2.normalize(src, dst=dst, alpha=0, beta=1.0) dst = pow(src/255,gamma) * 255 dst =np.round(dst).astype(np.uint8) return dst image1 = gamma_transformation(img,0.6) image2 = gamma_transformation(img,0.4) image3 = gamma_transformation(img,0.3) fig = plt.figure(figsize=(3, 2)) result = np.concatenate([img, image1,image2,image3],axis=1) plt.show(result) plt.show() cv2.imshow('3.8',img) cv2.waitKey(0) cv2.destroyAllWindows()
时间: 2023-12-05 12:06:41 浏览: 201
以下是修改后的代码:
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.8.tif', 0)
def gamma_transformation(src, gamma):
height, width = src.shape
dst = np.zeros((height, width), np.uint8)
cv2.normalize(src, dst=dst, alpha=0, beta=1.0)
dst = np.power(dst, gamma) * 255
dst = np.round(dst).astype(np.uint8)
return dst
image1 = gamma_transformation(img, 0.6)
image2 = gamma_transformation(img, 0.4)
image3 = gamma_transformation(img, 0.3)
result = np.concatenate([img, image1, image2, image3], axis=1)
plt.imshow(result, cmap='gray')
plt.show()
cv2.imshow('3.8', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
修改说明:
1. 将代码中的缩进格式化。
2. 在 `plt.imshow()` 方法中添加了 `cmap='gray'` 参数以显示灰度图像。
3. 将 `plt.show(result)` 改为 `plt.imshow(result)`。
4. 将 `plt.show()` 移到 `plt.imshow()` 之后。
5. 删除了不必要的 `fig = plt.figure(figsize=(3, 2))`。
6. 将 `np.power(src/255, gamma)` 改为 `np.power(dst, gamma)`。
7. 删除了不必要的空格。
阅读全文
相关推荐


















