说明

  1. 共用了两种方法进行检测
  1. 方法1: 使用opencv的canny进行边缘检测,在此之前先变灰和加入高斯模糊
  2. 方法2: 对图片二值化,随后找到关键点,并在空白画板画关键点

代码

import cv2 as cv
import numpy as np
# 读路径下的图片
img = cv.imread("./cat.jpg")
# 创建纯黑画板
blank = np.zeros(img.shape, dtype="uint8")
# 变灰
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
# 模糊, 如果不用模糊,边缘点会非常多 
blur = cv.GaussianBlur(gray, (5,5), cv.BORDER_DEFAULT)
# 二值化 寻找边缘点并画图
ret, thresh = cv.threshold(gray, 125, 225,cv.THRESH_BINARY)
counters, heirarchies = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(blank, counters, -1, (255,255,255), 1)
# print(len(counters))
# 边缘检测
canny = cv.Canny(blur, threshold1=100, threshold2=200)
cv.imshow("canny",canny)
cv.imshow("THRESH_BINARY",blank)
cv.waitKey(0)

截图

pytorch 边缘检测 边缘检测opencv_二值化