给我一段python图像预处理的代码,并把每一步的结果以样例图片的形式展示给我
时间: 2023-03-13 11:20:40 浏览: 68
import cv2 # 导入图像处理库
import numpy as np # 导入数字处理库# 读取图像
img = cv2.imread('example.png')# 转换为灰度图像
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 高斯模糊
Gauss_img = cv2.GaussianBlur(gray_img, (3, 3), 0)# 计算边缘
canny_img = cv2.Canny(Gauss_img, 100, 200)# 将边缘图像转换为RGB图像
rgb_img = cv2.cvtColor(canny_img, cv2.COLOR_GRAY2RGB)# 保存图像
cv2.imwrite('result.png', rgb_img)# 以上是一段用于图像预处理的Python代码,它可以将输入的样例图片转换为由边缘组成的RGB图像,结果如下所示。
相关问题
给我一段python图像预处理的代码
# 导入必要的库
import numpy as np
import matplotlib.pyplot as plt
import cv2# 读取图像
image = cv2.imread('image.jpg')# 将图像转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 将灰度图像降噪
denoised_image = cv2.fastNlMeansDenoising(gray_image,None,9,13)# 使用高斯滤波平滑图像
smoothed_image = cv2.GaussianBlur(denoised_image,(5,5),0)# 使用阈值分割图像
threshold_image = cv2.threshold(smoothed_image, 60, 255, cv2.THRESH_BINARY_INV)[1]# 将图像膨胀
dilated_image = cv2.dilate(threshold_image, None, iterations=2)# 显示预处理后的图像
plt.imshow(dilated_image, cmap='gray')
plt.show()
编写一段python opencv图像预处理的代码,并每步都用一张结果图片展示
import cv2
import numpy as np # 读取图像
img = cv2.imread("example.jpg")# 灰度化
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", grayImg)
cv2.waitKey(0) # 二值化
ret, thresh = cv2.threshold(grayImg, 127, 255, 0)
cv2.imshow("thresh", thresh)
cv2.waitKey(0) # 腐蚀
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(thresh, kernel, iterations=1)
cv2.imshow("erosion", erosion)
cv2.waitKey(0) # 膨胀
dilation = cv2.dilate(erosion, kernel, iterations=1)
cv2.imshow("dilation", dilation)
cv2.waitKey(0) # 边缘检测
edges = cv2.Canny(dilation, 150, 200)
cv2.imshow("edges", edges)
cv2.waitKey(0) # 显示最终结果
cv2.imshow("final", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()