这个代码有什么问题import cv2 import numpy as np img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.5.tif', 0) def logarithmic_transform(img): height, width = img.shape Image = np.zeros((height, width), np.uint8) for i in range(0, height): for j in range(0, width): imgPixel = img[i, j] Image[i, j] = np.log(1+imgPixel) return Image Image = logarithmic_transform(img) cv2.namedWindow('Image2') cv2.imshow('Image2', Image) cv2.waitKey(0) cv2.destroyAllWindows() logarithmic_transform(img)
时间: 2024-02-26 16:54:35 浏览: 56
这段代码的问题可能在于对图像进行对数变换时,像素值可能会超过255,但在代码中没有进行像素值的归一化处理,可能会导致图像显示不正确。建议在进行对数变换后,将像素值归一化到0~255范围内再进行显示,可以使用cv2.normalize()函数进行归一化。同时,函数logarithmic_transform(img)中的变量Image首字母应该小写,以符合Python的命名规范。
相关问题
这个代码有什么问题import cv2 import numpy as np img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\1.tif', 0) def invert_image(img): height, width = img.shape Image = np.zeros((height, width), np.uint8) for i in range(0, height): for j in range(0, width): imgPixel = img[i, j] Image[i, j] = 255 - imgPixel return Image Image = invert_image(img) cv2.namedWindow('Image1') cv2.imshow('Image1', Image) cv2.waitKey(0) cv2.destroyAllWindows()
这段代码的问题是在 `invert_image` 函数中,`return` 语句放在了两个循环的内部,导致函数只执行了一次循环就直接返回了结果,正确的做法是将 `return` 语句放在循环外部。另外,代码中没有对函数进行调用,需要在文件末尾加上 `invert_image(img)`。最后,代码中的路径使用了反斜杠 `\`,需要使用双反斜杠 `\\` 或者正斜杠 `/`。下面是修改后的代码:
```
import cv2
import numpy as np
img = cv2.imread('E:/postgraduate/three/DIP3E_Original_Images_CH03/1.tif', 0)
def invert_image(img):
height, width = img.shape
Image = np.zeros((height, width), np.uint8)
for i in range(0, height):
for j in range(0, width):
imgPixel = img[i, j]
Image[i, j] = 255 - imgPixel
return Image
Image = invert_image(img)
cv2.namedWindow('Image1')
cv2.imshow('Image1', Image)
cv2.waitKey(0)
cv2.destroyAllWindows()
invert_image(img)
```
改正此代码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()
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()
阅读全文