上一篇:机器学习(14)--经典边缘检测canny算法(计算机视觉从0到1)https://blog.csdn.net/qq_36187544/article/details/89548363
下一篇:
目录
HED网络
opencv
源代码
百度云资源:
链接:https://pan.baidu.com/s/1ET2-Ke-WsxMhRHrbur49Jg
提取码:j8vc
附带源码,模型文件,结构文件,测试图片都带上了,直接用即可(要先安装opencv!)
HED原理基于VGG16的端对端网络,整个原理图如下:
相较于传统边缘检测方法--canny 算法的检测效果,依赖于几个阈值参数,这些阈值参数的选择,通常都是人为设置的经验值。虽然有这些阈值参数,但是最终的参数只是一组或少数几组固定的组合,所以算法的鲁棒性又会打折扣,很容易遇到边缘检测效果不理想的场景。
一篇很好的HED网络原理文章,写的短且精,上图来源:https://blog.csdn.net/u012905422/article/details/52782615
我的情况是anaconda 安装,在anaconda prompt下输入:
pip install opencv-python
不要输入pip install opencv,安装不了,而且在anaconda和pycharm直接安装时总会报错,真的无语,搞了一整天,突然发现这最简单的方法一下就OK了。系统里需要,如果装不上可以先安装试一下
版本:
这是别人训练好的网络可以直接使用,相当于这是做一个预测,此代码不可单独运行,需要配置模型文件和网络文件,见文章开头百度云。如果要进行HED网络的调整就需要详细研究了,其实HED网络并不复杂,关键是这是一个神经网络原型,需要损失函数,需要类似标签的结果计算损失函数实现逼近,这是样本数据太难做了,调整较为困难,这里会使用就行了!全网推的HED网络开山鼻祖链接:https://github.com/s9xie/hed
import cv2 as cv
import argparse
'''
利用opencv进行HED网络边缘检测
'''
#设置图片、模型文件、网络文件
parser = argparse.ArgumentParser(
description='This sample shows how to define custom OpenCV deep learning layers in Python. '
'Holistically-Nested Edge Detection (https://arxiv.org/abs/1504.06375) neural network '
'is used as an example model. Find a pre-trained model at https://github.com/s9xie/hed.')
parser.add_argument('--input', help='Path to image or video. Skip to capture frames from camera', default='001.JPG')
parser.add_argument('--prototxt', help='Path to deploy.prototxt', default='deploy.prototxt')
parser.add_argument('--caffemodel', help='Path to hed_pretrained_bsds.caffemodel',
default='hed_pretrained_bsds.caffemodel')
parser.add_argument('--width', help='Resize input image to a specific width', default=500, type=int)
parser.add_argument('--height', help='Resize input image to a specific height', default=500, type=int)
args = parser.parse_args()
# ! [CropLayenr]
class CropLayer(object):
def __init__(self, params, blobs):
self.xstart = 0
self.xend = 0
self.ystart = 0
self.yend = 0
# Our layer receives two inputs. We need to crop the first input blob
# to match a shape of the second one (keeping batch size and number of channels)
def getMemoryShapes(self, inputs):
inputShape, targetShape = inputs[0], inputs[1]
batchSize, numChannels = inputShape[0], inputShape[1]
height, width = targetShape[2], targetShape[3]
# self.ystart = (inputShape[2] - targetShape[2]) / 2
# self.xstart = (inputShape[3] - targetShape[3]) / 2
self.ystart = int((inputShape[2] - targetShape[2]) / 2)
self.xstart = int((inputShape[3] - targetShape[3]) / 2)
self.yend = self.ystart + height
self.xend = self.xstart + width
return [[batchSize, numChannels, height, width]]
def forward(self, inputs):
return [inputs[0][:, :, self.ystart:self.yend, self.xstart:self.xend]]
# ! [CropLayer]
# ! [Register]
cv.dnn_registerLayer('Crop', CropLayer)
# ! [Register]
# Load the model.
net = cv.dnn.readNet(cv.samples.findFile(args.prototxt), cv.samples.findFile(args.caffemodel))
kWinName = 'Holistically-Nested Edge Detection'
cv.namedWindow('Input', cv.WINDOW_NORMAL)
cv.namedWindow(kWinName, cv.WINDOW_NORMAL)
frame = cv.imread('001.JPG')
cv.imshow('Input', frame)
# cv.waitKey(0)
inp = cv.dnn.blobFromImage(frame, scalefactor=1.0, size=(args.width, args.height),
mean=(104.00698793, 116.66876762, 122.67891434),
swapRB=False, crop=False)
net.setInput(inp)
out = net.forward()
out = out[0, 0]
out = cv.resize(out, (frame.shape[1], frame.shape[0]))
cv.imshow(kWinName, out)
cv.imwrite('result.png', out)
cv.waitKey(0)
运行结果: