此代码问题import cv2 import numpy as np import matplotlib.pyplot as plt img1 = cv2.imread(r"E:/postgraduate/three/DIP3E_Original_Images_CH03/3.16.1.tif") img2 = cv2.imread(r"E:/postgraduate/three/DIP3E_Original_Images_CH03/3.16.2.tif") img3 = cv2.imread(r"E:/postgraduate/three/DIP3E_Original_Images_CH03/3.16.3.tif") img4 = cv2.imread(r"E:/postgraduate/three/DIP3E_Original_Images_CH03/3.16.4.tif") def bhistogram(src): height, width= src.shape dst = np.zeros((height, width), np.uint8) cv2.calcHist([src],[0],None,[256],[0,256]) / np.size(src) return dst image1 = bhistogram(img1) image2 = bhistogram(img2) image3 = bhistogram(img3) image4 = bhistogram(img4) plt.figure(figsize=(100,100)) plt.subplot(421) plt.imshow(img1,cmap='gray') plt.axis('off') plt.subplot(422) plt.plot(image1) plt.xlim([0,256]) plt.ylim([0,1]) plt.subplot(423) plt.imshow(img2,cmap='gray') plt.axis('off') plt.subplot(424) plt.plot(image2) plt.xlim([0,256]) plt.ylim([0,1]) plt.subplot(425) plt.imshow(img3,cmap='gray') plt.axis('off') plt.subplot(426) plt.plot(image3) plt.xlim([0,256]) plt.ylim([0,1]) plt.subplot(427) plt.imshow(img4,cmap='gray') plt.axis('off') plt.subplot(428) plt.plot(image4) plt.xlim([0,256]) plt.ylim([0,1]) plt.show()
时间: 2024-03-27 12:36:58 浏览: 141
这段代码存在一个问题,即在bhistogram函数中,虽然调用了cv2.calcHist函数计算了灰度直方图,但是并没有将其结果赋值给dst变量,导致bhistogram函数始终返回一个全为0的数组。因此,在绘制灰度直方图时,实际上绘制的是全0直方图,而不是图像的真实灰度分布情况。要解决这个问题,需要将cv2.calcHist函数的计算结果赋值给dst变量。修改后的bhistogram函数如下:
```
def bhistogram(src):
height, width = src.shape
dst = cv2.calcHist([src], [0], None, [256], [0, 256]) / np.size(src)
return dst
```
这样,就能够正确计算图像的灰度直方图并绘制了。
相关问题
改正此代码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()
此代码有什么问题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()
```
阅读全文