轉載: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