机器学习(15)--HED网络预测实现(opencv+python实现边缘检测及源代码百度云资源)

上一篇:机器学习(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网络

HED原理基于VGG16的端对端网络,整个原理图如下:

相较于传统边缘检测方法--canny 算法的检测效果,依赖于几个阈值参数,这些阈值参数的选择,通常都是人为设置的经验值。虽然有这些阈值参数,但是最终的参数只是一组或少数几组固定的组合,所以算法的鲁棒性又会打折扣,很容易遇到边缘检测效果不理想的场景。

一篇很好的HED网络原理文章,写的短且精,上图来源:https://blog.csdn.net/u012905422/article/details/52782615


opencv

我的情况是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)


运行结果:

  • 14
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值