import numpy as np import cv2 def canny(image, minthreshold, maxthreshold): 1. 高斯滤波器,去噪声…
- User
import numpy as np import cv2
def canny(image, min_threshold, max_threshold): # 1. 高斯滤波器,去噪声 kernel_size = 5 sigma = 1.4 blur = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma)
code# 2. 计算梯度值和方向 dx = cv2.Sobel(blur, cv2.CV_64F, 1, 0) dy = cv2.Sobel(blur, cv2.CV_64F, 0, 1) magnitude = np.sqrt(dx * dx + dy * dy) # 计算梯度幅值 angle = np.arctan2(dy, dx) * 180 / np.pi # 计算梯度方向 angle[angle < 0] += 180 # 将梯度方向转换为0-180度之间 # 3. 非极大值抑制 height, width = magnitude.shape new_magnitude = np.zeros((height, width)) for i in range(1, height - 1): for j in range(1, width - 1): if (0 <= angle[i, j] < 22.5) or (157.5 <= angle[i, j] <= 180): if (magnitude[i, j] >= magnitude[i, j + 1]) and (magnitude[i, j] >= magnitude[i, j - 1]): new_magnitude[i, j] = magnitude[i, j] elif (22.5 <= angle[i, j] < 67.5): if (magnitude[i, j] >= magnitude[i + 1, j + 1]) and (magnitude[i, j] >= magnitude[i - 1, j - 1]): new_magnitude[i, j] = magnitude[i, j] elif (67.5 <= angle[i, j] < 112.5): if (magnitude[i, j] >= magnitude[i + 1, j]) and (magnitude[i, j] >= magnitude[i - 1, j]): new_magnitude[i, j] = magnitude[i, j] elif (112.5 <= angle[i, j] < 157.5): if (magnitude[i, j] >= magnitude[i - 1, j + 1]) and ( magnitude[i, j] >= magnitude[i + 1, j - 1]): new_magnitude[i, j] = magnitude[i, j] # 4. 双阈值处理,确定边缘 strong_edges = (new_magnitude > max_threshold) thresholded_edges = np.zeros_like(new_magnitude) thresholded_edges[(new_magnitude >= min_threshold) & (new_magnitude <= max_threshold)] = 1 weak_edges = (thresholded_edges > 0) & (strong_edges == 0) # 5. 边缘连接 edge_result = np.zeros_like(new_magnitude) for i in range(1, height - 1): for j in range(1, width - 1): if strong_edges[i, j]: edge_result[i, j] = 1 elif weak_edges[i, j]: if ((edge_result[i + 1, j] == 1) or (edge_result[i - 1, j] == 1) or (edge_result[i, j + 1] == 1) or (edge_result[i, j - 1] == 1) or (edge_result[i + 1, j + 1] == 1) or (edge_result[i - 1, j - 1] == 1) or (edge_result[i - 1, j + 1] == 1) or (edge_result[i + 1, j - 1] == 1)): edge_result[i, j] = 1 return edge_result - Assistant
本代码实现对输入视频进行Canny边缘检测,并将结果输出为视频文件。
实现思路:
- 使用cv2.VideoCapture()打开视频文件,读取每一帧图像。
- 将读取到的图像转换成灰度图像。
- 调用自己实现的canny()函数进行边缘检测,得到边缘图像。
- 缓存所有图像的边缘检测结果。
- 使用cv2.VideoWriter()创建输出视频文件,将每一帧边缘图像写入输出视频中。
- 释放资源。
代码实现:
- User
上述代码会报错,报错信息为:Traceback (most recent call last): File "D:\my_python\temp\canny_video.py", line 79, in height, width, _ = frame.shape AttributeError: 'NoneType' object has no attribute 'shape'
- Assistant
这是因为有一帧视频图像读取失败,返回的frame为None,在None上调用.shape方法会报错。
为了避免这种情况发生,我们需要在读取视频帧之前,判断读取是否成功,如果成功则进行下一步操作,否则忽略这一帧图像。
修改代码如下:
codewhile True: ret, frame = cap.read() if ret == False: break # 将图像灰度化 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 调用自己实现的 canny() 函数进行边缘检测 edge_result = canny(gray, 50, 150) # 缓存处理结果 frames.append(edge_result) # 显示处理结果 cv2.imshow("Canny Edge Detection", edge_result) # 按下 q 键退出 if cv2.waitKey(1) == ord('q'): break # 合成视频 if len(frames) > 0: height, width = frames[0].shape fourcc = cv2.VideoWriter_fourcc(*"mp4v") out = cv2.VideoWriter("output.mp4", fourcc, 25, (width, height), False) for frame in frames: out.write(frame * 255) # 释放资源 out.release() cv2.destroyAllWindows() cap.release() - User
你给我的代码没有缩进,能给我有缩进的代码吗
- Assistant
- User
并没有,你的回答没有缩进!
- Assistant