import cv2 import matplotlib.pyplot as plt import numpy as np im1= cv2.imread(r"E:/postgraduate/three/DIP3E_Original_Images_CH03/3.43.tif") img = cv2.imread(r"E:/postgraduate/three/DIP3E_Original_Images_CH03/3.43.tif",0) def laplace(src): src = src.astype(float) / 255.0 #dst = np.zeros((height, width)) height, width = len(src), len(src[0]) dst = [[0] * width for _ in range(height)] #cv2.normalize(src, dst=dst, alpha=0, beta=1.0) mx = float('-inf') mn = float('inf') for i in range(height): for j in range(width): mx = max(mx, src[i][j]) mn = min(mn, src[i][j]) for i in range(height): for j in range(width): dst[i][j] = (src[i][j] - mn) / (mx - mn) kernel = [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]] for i in range(1, height-1): for j in range(1, width-1): conv_sum = 0 #dst[i,j] = np.sum(kernel*src[i-1:i+2, j-1:j+2]) for x in range(3): for y in range(3): conv_sum += kernel[x][y] * src[i-1+x][j-1+y] dst[i][j] = conv_sum dst[i][j] = min(max(dst[i][j], 0), 255) return dstim2 = laplace(img) im3 = add(img,im1)此代码有什么问题
时间: 2024-04-20 08:26:47 浏览: 200
根据提供的代码,可以发现以下问题:
1. 在导入库时,缺少`add()`函数的导入语句。因此,无法确定`add()`函数的定义和实现细节。
2. 在函数`laplace()`中,代码`return dstim2 = laplace(img)`有语法错误。应该将其分为两行,一行是调用`laplace()`函数并将结果赋值给`im2`,另一行是返回`im2`。
3. 在函数`laplace()`中,`dst[i][j] = min(max(dst[i][j], 0), 255)`语句应该位于内部循环中,以便对每个像素进行处理。
4. 在主程序中,`im3 = add(img,im1)`调用了未定义的`add()`函数,因此无法确定该函数的定义和实现细节。
需要解决这些问题后才能准确评估代码的正确性。
相关问题
此代码改正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(img,gamma): height, width = img.shape dst = np.zeros((height, width), np.uint8) cv2.normalize(img, dst=dst, alpha=0, beta=1.0) dst = pow(img/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=(8, 6)) vs1 = np.concatenate([img, image1,image2,image3]) vs2 = np.hstack((image2, image3)) result = np.vstack((vs1, vs2)) plt.show('3.8', result) cv2.waitKey(0) cv2.destroyAllWindows()
这段代码有几个问题需要修改:
1. `plt.show()` 函数没有传入参数,应该将图像数组作为参数传入,即 `plt.imshow(result)`。
2. `gamma_transformation()` 函数中的 `img` 参数与 `cv2.imread()` 中读入的图像变量名相同,会导致函数中的 `img` 变量与外部变量重名,建议将函数中的参数名改为 `src` 或其他不与外部变量名重复的名称。
3. 在 `gamma_transformation()` 函数中,计算 `dst` 的过程可以简化为 `dst = (img/255)**gamma * 255`,无需调用 `np.round(dst).astype(np.uint8)`。
4. 在使用 `cv2.imshow()` 显示图像时,应该传入窗口名称参数,如 `cv2.imshow('image', img)`。
修改后的代码如下:
```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 = (src/255)**gamma * 255
dst = 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=(8, 6))
vs1 = np.concatenate([img, image1, image2, image3], axis=1)
vs2 = np.concatenate([image2, image3], axis=1)
result = np.concatenate([vs1, vs2], axis=0)
plt.imshow(result)
plt.show()
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,`np.concatenate()` 函数第二个参数应该为 `axis=1` 或者 `axis=0`,表示沿着列或者行方向进行合并操作。
此代码有什么问题import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.14.tif',0) def bit_plane_slicing(src,z): height, width= src.shape dst = np.zeros((height, width), np.uint8) cv2.normalize(img, dst=dst, alpha=0, beta=1.0) for i in range(0, height): for j in range(0, width): pixel = format(src[i,j], '08b') if pixel[8-z] == '0': dst[i, j] = 0 else: dst[i, j] = 255 return dst img1 = bit_plane_slicing(img,8) img2 = bit_plane_slicing(img,7) img3 = bit_plane_slicing(img,6) img4 = bit_plane_slicing(img,5) image1 = img1*128 + img2*64 image2 = img1*128 + img2*64 + img3*32 image3 = img1*128 + img2*64 + img3*32 + img4*16 plt.figure(figsize=(100,100)) plt.subplot(131) plt.imshow(image1,cmap='gray') plt.axis('off') plt.subplot(132) plt.imshow(image2,cmap='gray') plt.axis('off') plt.subplot(133) plt.imshow(image3,cmap='gray') plt.axis('off') plt.show()
There are a few issues with this code:
1. The `bit_plane_slicing` function takes in a parameter `src`, but the code inside the function uses `img` instead. This will cause an error as `img` is not defined inside the function.
2. The `cv2.normalize` function is being used incorrectly. It should be `cv2.normalize(src, dst, alpha=0, beta=1.0, norm_type=cv2.NORM_MINMAX)`.
3. The `image1`, `image2`, and `image3` variables are being calculated incorrectly. The correct calculations should be:
```
image1 = img1*(2**7) + img2*(2**6)
image2 = img1*(2**7) + img2*(2**6) + img3*(2**5)
image3 = img1*(2**7) + img2*(2**6) + img3*(2**5) + img4*(2**4)
```
Here's the corrected code:
```
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.14.tif',0)
def bit_plane_slicing(src,z):
height, width = src.shape
dst = np.zeros((height, width), np.uint8)
cv2.normalize(src, dst, alpha=0, beta=1.0, norm_type=cv2.NORM_MINMAX)
for i in range(0, height):
for j in range(0, width):
pixel = format(src[i,j], '08b')
if pixel[8-z] == '0':
dst[i, j] = 0
else:
dst[i, j] = 255
return dst
img1 = bit_plane_slicing(img,8)
img2 = bit_plane_slicing(img,7)
img3 = bit_plane_slicing(img,6)
img4 = bit_plane_slicing(img,5)
image1 = img1*(2**7) + img2*(2**6)
image2 = img1*(2**7) + img2*(2**6) + img3*(2**5)
image3 = img1*(2**7) + img2*(2**6) + img3*(2**5) + img4*(2**4)
plt.figure(figsize=(100,100))
plt.subplot(131)
plt.imshow(image1,cmap='gray')
plt.axis('off')
plt.subplot(132)
plt.imshow(image2,cmap='gray')
plt.axis('off')
plt.subplot(133)
plt.imshow(image3,cmap='gray')
plt.axis('off')
plt.show()
```
阅读全文
相关推荐











