使用如下python代码:import cv2 import numpy as np import matplotlib.pyplot as plt def fix_threshold(img, thresh, maxval=255): return np.where(((img > thresh) & (img < maxval)), 255, 0) img = cv2.imread("C:\\Users\\YOLO\\UNET\\UNETtrain\\image\\1.png") img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) ret, th = cv2.threshold(img_gray, 80, 255, cv2.THRESH_BINARY) fix = fix_threshold(img_gray, 127, 255) # plt.subplot(131), plt.imshow(img_gray, cmap='gray') # plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(), plt.imshow(th, cmap='gray') plt.xticks([]), plt.yticks([]) plt.savefig("C:\\Users\\YOLO\\UNET\\UNETtrain\\label\\L1.png",bbox_inches='tight',pad_inches = -0.1),为什么保存下来的图片跟原图尺寸不一样
时间: 2023-10-15 22:07:03 浏览: 92
抱歉,我之前给出的回答是错误的。`bbox_inches='tight'` 参数在这种情况下无法解决图像尺寸不一致的问题。
如果你希望保存的图像尺寸与原图一致,可以使用 `cv2.imwrite()` 函数来替代 `plt.savefig()`。下面是修改后的代码:
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
def fix_threshold(img, thresh, maxval=255):
return np.where(((img > thresh) & (img < maxval)), 255, 0)
img = cv2.imread("C:\\Users\\YOLO\\UNET\\UNETtrain\\image\\1.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
ret, th = cv2.threshold(img_gray, 80, 255, cv2.THRESH_BINARY)
fix = fix_threshold(img_gray, 127, 255)
plt.subplot(), plt.imshow(th, cmap='gray')
plt.xticks([]), plt.yticks([])
cv2.imwrite("C:\\Users\\YOLO\\UNET\\UNETtrain\\label\\L1.png", th)
```
通过使用 `cv2.imwrite()` 函数,保存的图像将与原图具有相同的尺寸。