sobel算子python代码实现
时间: 2023-10-19 08:07:40 浏览: 195
Sobel 算子源代码
4星 · 用户满意度95%
以下是使用Sobel算子进行图像边缘检测的Python代码实现的示例:
```python
import cv2
def sobelEdgeDetection(image_path='image.jpg'):
img = cv2.imread(image_path, 0)
img = cv2.resize(img, (450, 450))
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobel_x = cv2.convertScaleAbs(sobel_x)
sobel_y = cv2.convertScaleAbs(sobel_y)
sobel_combined = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)
cv2.imshow('Original Image', img)
cv2.imshow('Sobel Edge Detection', sobel_combined)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
sobelEdgeDetection()
```
这段代码首先使用`cv2.imread`加载图像,然后使用`cv2.resize`调整图像的大小。接下来,分别使用`cv2.Sobel`计算水平和垂直方向上的边缘强度。`ksize=3`表示使用3x3的卷积核。然后,使用`cv2.convertScaleAbs`将结果转换为绝对值,并使用`cv2.addWeighted`将两个结果按比例相加得到最终的边缘图像。最后,使用`cv2.imshow`显示原始图像和边缘检测结果,并使用`cv2.waitKey`等待用户关闭窗口。
请注意,此代码仅为示例,你可能需要根据自己的需求进行适当的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [sobel算子及python实现](https://blog.csdn.net/weixin_41500849/article/details/80611263)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *3* [Sobel(索贝尔),Scharr(沙尔)和Laplacian(拉普拉斯)算子——python实现](https://blog.csdn.net/Keep_Trying_Go/article/details/125227338)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文