OpenCV教程-OpenCV Canny边缘检测
整理:python技术迷
edges = cv2.Canny('/path/to/img', minVal, maxVal, apertureSize, L2gradient)
参数-
/path/to/img: 图像文件路径(必填)
minVal: 最小强度梯度(必填)
maxVal: 最大强度梯度(必填)
aperture: 这是可选参数。
L2gradient: 默认值为false,如果值为true,则Canny()使用更消耗计算资源的方程来检测边缘,以提供更高的准确性。
示例: 1
import cv2img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat_16x9.jpg')edges = cv2.Canny(img, 100, 200)cv2.imshow("Edge Detected Image", edges)cv2.imshow("Original Image", img)cv2.waitKey(0) # waits until a key is pressedcv2.destroyAllWindows() # destroys the window showing image
输出:
👉点击领取:最全Python资料合集
示例: 实时边缘检测
# import libraries of python OpenCVimport cv2# import Numpy by alias name npimport numpy as np# capture frames from a cameracap = cv2.VideoCapture(0)# loop runs if capturing has been initializedwhile (1):# reads frames from a cameraret, frame = cap.read()# converting BGR to HSVhsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)# define range of red color in HSVlower_red = np.array([30, 150, 50])upper_red = np.array([255, 255, 180])# create a red HSV colour boundary and# threshold HSV imagemask = cv2.inRange(hsv, lower_red, upper_red)# Bitwise-AND mask and original imageres = cv2.bitwise_and(frame, frame, mask=mask)# Display an original imagecv2.imshow('Original', frame)# discovers edges in the input image image and# marks them in the output map edgesedges = cv2.Canny(frame, 100, 200)# Display edges in a framecv2.imshow('Edges', edges)# Wait for Esc key to stopk = cv2.waitKey(5) & 0xFFif k == 27:break# Close the windowcap.release()# De-allocate any associated memory usagecv2.destroyAllWindows()
输出:
热门推荐