在Python中使用OpenCV(CV2)对图像进行边缘检测
转载:https://blog.csdn.net/cumtb2002/article/details/107798767
Modules used:
使用的模块:
For this, we will use the opencv-python module which provides us various functions to work on images.
为此,我们将使用opencv-python模块,该模块为我们提供了处理图像的各种功能。
Download opencv-python
下载opencv-python
- General Way:
- pip install opencv-python
- Pycharm Users:
- Go to the project Interpreter and install this module from there.
opencv-python Module:
opencv-python模块:
opencv-python is a python library that will solve the Computer Vision Problems and provides us various functions to edit the Images.
opencv-python是一个python库,它将解决计算机视觉问题并为我们提供编辑图像的各种功能。
Note: The edge Detection is possible only in grayscale Image.
注意:只能在灰度图像中进行边缘检测。
What we will do in this script?
我们将在此脚本中做什么?
To detect the edges of the images we will use opencv-python various Functions and Provide thresholds.
为了检测图像的边缘,我们将使用opencv-python的各种功能并提供阈值。
In this article we will detect the edge of the Image with the help of various functions and the accuracy of edge increases as we go down,
在本文中,我们将借助各种功能来检测图像的边缘,并且当我们下降时边缘的精度会提高,
Sobel Function: This Function will create the Horizontal and vertical edges and after that, we will use the Bitwise or operator to combine them
Sobel函数 :此函数将创建水平边缘和垂直边缘,然后,我们将使用按位或运算符将它们组合
Laplacian Function: This Function is the simplest Function in which we just have to put the Grayscale Variable into it, and we will get the edge detected image.
拉普拉斯函数 :此函数是最简单的函数,只需要将灰度变量放入其中,就可以得到边缘检测到的图像。
Canny Function: This is the most powerful function for edge detection and most accurate.
Canny功能 :这是边缘检测功能最强大且最准确的功能。
Let's see the code:
让我们看一下代码:
1)使用Sobel函数 (1) Using Sobel Function)
- # importing the module
- import cv2
- # read the image and store the data in a variable
- image=cv2.imread("/home/abhinav/PycharmProjects/untitled1/b.jpg")
- # make it grayscale
- Gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
- # Make it with the help of sobel
- # make the sobel_horizontal
- # For horizontal x axis=1 and yaxis=0
- # for vertical x axis=0 and y axis=1
- Horizontal=cv2.Sobel(Gray,0,1,0,cv2.CV_64F)
- # the thresholds are like
- # (variable,0,<x axis>,<y axis>,cv2.CV_64F)
- Vertical=cv2.Sobel(Gray,0,0,1,cv2.CV_64F)
- # DO the Bitwise operation
- Bitwise_Or=cv2.bitwise_or(Horizontal,Vertical)
- # Show the Edged Image
- cv2.imshow("Sobel Image",Bitwise_Or)
- cv2.imshow("Original Image",Gray)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Output:
输出:

2)拉普拉斯函数 (2) Laplacian Function)
- # importing the module
- import cv2
- # read the image and store the data in a variable
- image=cv2.imread("/home/abhinav/PycharmProjects/untitled1/b.jpg")
- # make it grayscale
- Gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
- # Make Laplacian Function
- Lappy=cv2.Laplacian(Gray,cv2.CV_64F)
- cv2.imshow("Laplacian",Lappy)
- cv2.imshow("Original",Gray)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Output:
输出:

3)使用Canny函数 (3) Using Canny Function)
- # importing the module
- import cv2
- # read the image and store the data in a variable
- image=cv2.imread("/home/abhinav/PycharmProjects/untitled1/b.jpg")
- # make it grayscale
- Gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
- # Make canny Function
- canny=cv2.Canny(Gray,40,140)
- # the threshold is varies bw 0 and 255
- cv2.imshow("Canny",canny)
- cv2.imshow("Original",Gray)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Output:
输出:

翻译自: https://www.includehelp.com/python/edge-detection-of-image-using-opencv-cv2.aspx
在Python中使用OpenCV(CV2)对图像进行边缘检测的更多相关文章
- OpenCV-Python(1)在Python中使用OpenCV进行人脸检测
OpenCV是如今最流行的计算机视觉库,而我们今天就是要学习如何安装使用OpenCV,以及如何去访问我们的摄像头.然后我们一起来看看写一个人脸检测程序是如何地简单,简单到只需要几行代码. 在开始之前, ...
- Python下的OpenCV学习 02 —— 图像的读取与保存
OpenCV提供了众多对图片操作的函数,其中最基本的就是图片的读取与输出了. 一.读取图片 利用OpenCV读取一张图片是非常容易的,只需要用到 imread() 函数,打开shell或者cmd,进入 ...
- python中使用Opencv进行车牌号检测——2018.10.24
初学Python.Opencv,想用它做个实例解决车牌号检测. 车牌号检测需要分为四个部分:1.车辆图像获取.2.车牌定位.3.车牌字符分割和4.车牌字符识别 在百度查到了车牌识别部分车牌定位和车牌字 ...
- python中使用Opencv进行人脸识别
上一节讲到人脸检测,现在讲一下人脸识别.具体是通过程序采集图像并进行训练,并且基于这些训练的图像对人脸进行动态识别. 人脸识别前所需要的人脸库可以通过两种方式获得:1.自己从视频获取图像 2.从人 ...
- python中使用Opencv进行人脸检测
这两天学习了人脸识别,看了学长写的代码,边看边码边理解搞完了一边,再又是自己靠着理解和记忆硬码了一边,感觉还是很生疏,就只能来写个随笔加深一下印象了. 关于人脸识别,首先需要了解的是级联分类器Casc ...
- python中使用OpenCV处理图片
1.导入OpenCV包 import cv2 2.读取图片 cv2.imread(image_path, mode) 读入函数,包含两个参数,第一个为图片路径及图片名,第二个为读取图片方 ...
- Python中cv2库和matplotlib库色彩空间排布不一致
今天在python中读如图片时发现以下问题: 1.在from matplotlib import pyplot as plt之后,再import cv2 cv2.imshow()不能正常使用,还不知道 ...
- Python开发:OpenCV版本差异所引发的cv2.findContours()函数传参问题
一.问题如下: cv2.findContours()这个方法是用来找出轮廓值的: # cv2.findContours找出轮廓值,cv2.RETR_EXTERNAL表示图像的外轮廓 binary, c ...
- 使用python开启你的opencv之旅---图像的读入,存储
python的便捷是如此的引人着迷,而opencv给python提供的接口使我们能够使用python来快速验证我们的想法,或者与别的模块快速结合,在这个系列文章我会通过jupyter notebook ...
随机推荐
- [ASP.NET Core开发实战]基础篇01 Startup
Startup,顾名思义,就是启动类,用于配置ASP.NET Core应用的服务和请求管道. Startup有两个主要作用: 通过ConfigureServices方法配置应用的服务.服务是一个提供应 ...
- 浅谈 FTP、FTPS 与 SFTP
无论是网盘还是云存储,上传都是一项很简单的操作.那些便捷好用的上传整理工具所用的 FTP 协议到底是什么意义,繁杂的模式又有何区别? 二狗子最近搭建了一个图片分享网站,每天都有好多人在他的网站上传许多 ...
- 如何用CMake构建Android C++库
https://fireflytech.org/2017/11/04/compiling-cc-libraries-for-android/ https://blog.csdn.net/xhp2014 ...
- 【小白学PyTorch】8 实战之MNIST小试牛刀
文章来自微信公众号[机器学习炼丹术].有什么问题都可以咨询作者WX:cyx645016617.想交个朋友占一个好友位也是可以的~好友位快满了不过. 参考目录: 目录 1 探索性数据分析 1.1 数据集 ...
- 20190926-02Redis五大数据类型之Set 000 028
- hyperledger explorer 结合 fabric1.4 搭建 区块链浏览器 踩坑记录
博主通过这篇博客的步骤搭建区块链浏览器:https://blog.csdn.net/qq_32675427/article/details/99946945 进行到下面这一步时出现各种异常,浪费了博主 ...
- (超详细)动手编写-链表(Java实现)
目录 前言 概念 链表的设计 完整代码 List接口 抽象父类设计 链表-LinkedList 虚拟头结点 概念 结构设计 方法变动 双向链表 概念 双向链表设计 方法变动 循环链表 单向循环链表 双 ...
- CentOS7重装yum和python
卸载现有的Python和Yum 1.删除现有Python ##强制删除已安装程序及其关联 rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ...
- [HDU2577]How to Type(DP)
题目链接 题意 给一个大小写字符串,求最少敲击字符串次数,最开始和最后要求shift都是down的.如日常,大小写转换可以ctrl+z或者shift保持 up/down. 题解 两个dp数组,一个表示 ...
- Combine 框架,从0到1 —— 4.在 Combine 中使用 KVO
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 4.在 Combine 中使用 KVO. 内容概览 前言 用 KVO 监控改动 将 KVO 代 ...