在Python中使用OpenCV(CV2)对图像进行边缘检测

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:

输出:

Python | Edge Detection of Image using OpenCV (CV2) (1)

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:

输出:

Python | Edge Detection of Image using OpenCV (CV2) (2)

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:

输出:

Python | Edge Detection of Image using OpenCV (CV2) (3)

翻译自: https://www.includehelp.com/python/edge-detection-of-image-using-opencv-cv2.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379301.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

需保留小数点两位,但同时不要小数点后多余0的前后台代码实现

今天碰到一个需求。关于小数点的处理&#xff0c;看起来非常简单的事情&#xff0c;却花了一定时间做了一些试验。最后简单总结一下&#xff0c;以便备忘。 需求简化一下表达是这样的&#xff1a; 有A、B两列&#xff0c;A/BC。这3列在数据库中都以decimal存放。 在应用中&…

汇编语言-017(SCASW 、STRUCT 、STRUCT_ALLPOINTS 、STRUCT_ALIGN 、SYSTEMTIME、UNION 、 STRUCTTEST )

1&#xff1a;SCASW : 在wordArray中扫描16位数值0100h,将匹配元素的偏移量复制到EAX .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data wordArray WORD 0500h,0400h,0300h,0200h,0100h.code main PROCmov ax,0100hmov edi,OFFSET wordArraymov …

【笔记】正则表达式[1]

元字符 符号 意思 示范 详例 \d 任意一个数字 \d{2}-\d{8} 22-12345678 * *前的符号重复任意次数 次数可以为零 \d* 222222222222... 或 2 \s 任意的空白符 全半角空格&#xff0c;tab&#xff0c;换行符 \bhi\b\s\bLucy\b hi Lucy 和*用法相似 次数>1 \d …

编写一个汇编语言程序,完成以下要求。从BUF单元处定义有10个带符号字数据:-1,3,24,94,62,72,55,0,-48,99,试找出他们中的最大值和平均值,并以此分别存放至该数据区的后两个单元

编写一个汇编语言程序&#xff0c;完成以下要求。从BUF单元处定义有10个带符号字数据:-1,3,24,94,62,72,55,0,-48,99&#xff0c;试找出他们中的最大值和平均值&#xff0c;并以此分别存放至该数据区的后两个单元中(假设这10个数的和值不超过16位范围) P176 4.12 编程思路&am…

prototype 的ajax

原文&#xff1a;http://www.prototypejs.org/learn/introduction-to-ajax]翻 译&#xff1a;www.ruby-china.cn 站长]Prototype框架提供了非常容易和有意思的方法处理Ajax的调用&#xff0c;同时它也是浏 览器安全的 。除了简单的请求外&#xff0c;这个模块&#xff08;指pro…

汇编语言-018(FLD 、FST、FSTP、FCHS、FABS 、浮点运算符、浮点比较 )

1&#xff1a;FLD : FPU&#xff08;浮点处理器&#xff09;的加载浮点数到堆栈指令 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data array REAL8 10 DUP(?) dblOne REAL8 234.56 dblTwo REAL8 10.1.code main PROCfld array …

mcq 队列_MCQ | 基础知识 免费和开源软件| 套装4

mcq 队列Q1. What do you call the technique of storing encrypted user passwords in Linux? Q1。 您如何称呼在Linux中存储加密的用户密码的技术&#xff1f; System Password Management 系统密码管理 Shadow Password 影子密码 Encrypted Password 加密密码 None of the…

将AX寄存器中的16位数据分成4组(从高到低),每组4位,然后把这4组数作为数当中的低4位分别放在AL,BL,CL,DL中。

将AX寄存器中的16位数据分成4组&#xff08;从高到低&#xff09;&#xff0c;每组4位&#xff0c;然后把这4组数作为数当中的低4位分别放在AL&#xff0c;BL&#xff0c;CL&#xff0c;DL中。 P176 4.14 编程思路&#xff1a;首先用BX、DX存放AX&#xff0c;即原AX原BX原DX&…

一个很不错的wp企业站模板

http://zjuhpp.com/chinese-localization-of-business-wordpress-theme-devster.html转载于:https://www.cnblogs.com/i-kyle/archive/2012/09/13/2683817.html

著名的自由软件圣战- “KDE/QT .VS. Gnome/Gtk”

在 Unix 的图形界面一向是以 MIT 的 X Window 系统为标准&#xff0c; 可是在商业应用上有两大流派&#xff0c;一派是以 Sun 公司领导的 Openlook 阵营&#xff0c;一派是 IBM/HP 领导的OSF (Open Software Foundation) 的 Motif&#xff0c; 双方经过多年竞争之后&#xff0c…

汇编语言-019(汇编程序与c\c++相互调用)

1&#xff1a;在C程序中使用__asm块插入汇编代码程序&#xff08;不能用LENGTHOF与SIZEOF运算符&#xff0c;而是LENGTH和SIZE&#xff09; struct Package {long originZip; //4long destinationzip;//4float shippingPrice; //4 };int main(int argcount,char* args[]) {c…

kotlin 判断数字_Kotlin程序检查数字是偶数还是奇数

kotlin 判断数字Given a number N, we have to check whether it is EVEN or ODD. 给定数字N &#xff0c;我们必须检查它是偶数还是奇数 。 Example: 例&#xff1a; Input:N 13Output:"ODD"Input:N 24Output:"EVEN"程序在Kotlin检查偶数或奇数 (Prog…

微机原理与接口技术(第2版)考点

第一章 1&#xff0c;微型计算机的特点&#xff1a; 功能强、可靠性高价格低廉系统设计灵活&#xff0c;适应性强体积小&#xff0c;重量轻&#xff0c;维护方便 2&#xff0c;微型计算机的硬件组成 微处理器内存储器I/O接口电路I/O设备系统总线 3&#xff0c;微机的工作过…

搜狗面试笔试一面二面全经历

09.25 华科西十二教&#xff0c;搜狗招聘笔试&#xff1a; C搜索引擎研发。同时有威盛、烽火两家笔试&#xff0c;就没有去。 09.26 华科校内某酒店&#xff0c;搜狗一面&#xff1a; 笔试做的不错&#xff0c;客观题错了3.5&#xff08;20个&#xff09;&#xff0c;后两个算法…

UltraEdit语法高亮

语法加亮分支位于配置&#xff0d;编辑器显示之下&#xff0c;允许用户配置语法加亮选项&#xff1a;语法加亮可以识别预定词语&#xff0c;并用不同颜色显示它们。该功能对于程序员来说尤其有用&#xff0c;并且对那些想用不同颜色显示文档中词语的用户也非常有用。提供二十种…

线性代数 向量长度_用户定义长度的向量| 使用Python的线性代数

线性代数 向量长度Prerequisite: Defining a vector 先决条件&#xff1a; 定义向量 Linear algebra is the branch of mathematics concerning linear equations by using vector spaces and through matrices. In other words, a vector is a matrix in n-dimensional space…

顺序表(代码、分析、汇编)

目录&#xff1a;代码&#xff1a;分析&#xff1a;汇编&#xff1a;代码&#xff1a; SeqList.h #ifndef _SEQLIST_H_ #define _SEQLIST_H_ typedef void SeqList; //定义链表数据类型&#xff0c;void因为要适用不同链表数据类型 typedef void SeqListNode; //定义链表节…

设有两个16位整数变量A和B,试编写完成下述操作的程序。

设有两个16位整数变量A和B&#xff0c;试编写完成下述操作的程序。 &#xff08;1&#xff09;若有两个数中一个是奇数&#xff0c;则将奇数存入A中&#xff0c;偶数存入B中。 &#xff08;2&#xff09;若两个数均为奇数&#xff0c;则两数分别减1&#xff0c;并存回原变量中…

棋牌游戏服务器架构: 详细设计(三) 数据库设计

主要有3类Database: ServerInfoDB,UserInfoDB和GameDB。 ServerInfoDB主要存储的是游戏列表的信息,UserInfoDB存储玩家的全局信息&#xff0c;而GameDB就是积分以及积分变化情况。下面分别加以描述。 1. ServerInfoDB ServerInfoDB主要存储游戏列表信息。主要有以下几个表: 1. …

程序开发与性格特征

程序开发与性格特征 引言&#xff1a; 程序员给很多人的印象一般是不善于交际、表情严肃、思维紧密、做事认真、沉着冷静等等。那么这些特征到底和程序开发有没有关系呢&#xff1f;不同性格的人在团队开发当中将面临什么样的问题以及不同性格的人在团队开发中又将发挥着什么样…