编写一段python opencv图像预处理的代码,并每步都用一张结果图片展示
时间: 2023-03-13 14:14:26 浏览: 76
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()
相关问题
编写一段python opencv代码,用于图像预处理
### 回答1:
import cv2
img = cv2.imread('image.jpg')
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 降噪,图像平滑
img_median = cv2.medianBlur(gray, 3)
# 使用Sobel算子获取边缘
sobel = cv2.Sobel(img_median, cv2.CV_8U, 1, 0, ksize = 3)
# 二值化
ret, binary = cv2.threshold(sobel, 0, 255, cv2.THRESH_OTSU+cv2.THRESH_BINARY)
# 膨胀和腐蚀操作的核函数
element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 7))
# 膨胀一次,让轮廓突出
dilation = cv2.dilate(binary, element2, iterations = 1)
# 腐蚀一次,去掉细节,如表格线等。
erosion = cv2.erode(dilation, element1, iterations = 1)
# 再次膨胀,让轮廓明显一些
dilation2 = cv2.dilate(erosion, element2,iterations = 3)
# 显示图像
cv2.imshow("dilation", dilation2)
cv2.waitKey(0)
### 回答2:
下面是一个使用Python和OpenCV进行图像预处理的示例代码:
```python
import cv2
import numpy as np
def image_preprocessing(image_path):
# 读取图像
image = cv2.imread(image_path)
# 使用高斯模糊平滑图像
image = cv2.GaussianBlur(image, (5, 5), 0)
# 转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 对图像进行二值化
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找图像中的轮廓
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 绘制轮廓
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
# 显示图像
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 调用函数进行图像预处理
image_preprocessing("image.jpg")
```
这段代码首先使用`cv2.imread()`函数读取指定路径下的图像。然后使用`cv2.GaussianBlur()`函数对图像进行高斯模糊处理,以减少图像中的噪声。接下来,将图像转换为灰度图像,使用`cv2.cvtColor()`函数将BGR图像转换为灰度图像。然后,使用`cv2.threshold()`函数对灰度图像进行二值化处理,将灰度值大于127的像素设为255,其余为0。接下来,使用`cv2.findContours()`函数查找图像中的轮廓,并将轮廓画在原图像上。最后,使用`cv2.imshow()`函数显示图像,`cv2.waitKey(0)`等待按下任意键关闭窗口,`cv2.destroyAllWindows()`关闭所有窗口。
请确保在运行代码之前安装了Python和OpenCV,并将代码中的`image.jpg`替换为您要处理的图像的路径。
做一个python opencv图像预处理的程序,各项阈值可调节的图形界面
好的,我可以为您编写一个基于Python和OpenCV的图像预处理程序,它包含一个可调节的图形界面,您可以通过该界面调整各项阈值来实现不同的预处理效果。
这个程序将使用Python中的Tkinter模块来创建图形用户界面(GUI),并使用OpenCV库来加载和处理图像。
以下是程序的主要实现步骤:
1. 导入所需的模块和库
```
import cv2
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
```
2. 创建GUI界面和控件
```
root = tk.Tk()
root.title("Image Preprocessing Tool")
# Create File Dialog Button
file_dialog_btn = tk.Button(root, text="Open Image", command=open_file_dialog)
file_dialog_btn.pack()
# Create Threshold Sliders
threshold1_slider = tk.Scale(root, from_=0, to=255, orient=tk.HORIZONTAL, label="Threshold1", command=update_image)
threshold1_slider.pack()
threshold2_slider = tk.Scale(root, from_=0, to=255, orient=tk.HORIZONTAL, label="Threshold2", command=update_image)
threshold2_slider.pack()
```
3. 创建函数用于打开图像文件和更新图像
```
def open_file_dialog():
file_path = filedialog.askopenfilename()
if file_path:
img = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
cv2.imshow("Original Image", img)
update_image()
def update_image():
threshold1 = threshold1_slider.get()
threshold2 = threshold2_slider.get()
# Perform image preprocessing operations here
# ...
cv2.imshow("Processed Image", processed_img)
```
4. 在update_image函数中添加所需的图像预处理操作,并在预处理完成后使用cv2.imshow函数显示处理后的图像。
您可以根据需要添加其他控件和功能,例如保存处理后的图像,撤销操作等。
希望这个程序能够满足您的需求,如果您有任何问题或需要进一步帮助,请告诉我。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)