在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.pswp.cn/news/379301.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379301.shtml
英文地址,請注明出處:http://en.pswp.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;不同性格的人在團隊開發當中將面臨什么樣的問題以及不同性格的人在團隊開發中又將發揮著什么樣…