婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測

使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測

熱門標簽:公司電話機器人 騰訊外呼線路 白銀外呼系統 陜西金融外呼系統 唐山智能外呼系統一般多少錢 廣告地圖標注app 激戰2地圖標注 海南400電話如何申請 哈爾濱ai外呼系統定制

這篇博客將介紹如何使用OpenCV和深度學習應用全面嵌套的邊緣檢測。并將對圖像和視頻流應用全面嵌套邊緣檢測,然后將結果與OpenCV的標準Canny邊緣檢測器進行比較。

1. 效果圖

憤怒的小鳥——原始圖 VS Canny邊緣檢測圖 VS HED邊緣檢測圖


花朵——原始圖 VS Canny邊緣檢測圖 VS HED邊緣檢測圖

視頻效果圖GIF 如下

2. 全面嵌套邊緣檢測與Canny邊緣檢測

2.1 Hed與Canny邊緣檢測對比

Holistically-Nested Edge Detection (HED) 全面嵌套邊緣檢測

Canny Edge Detection Canny邊緣檢測

OpenCV 利用Canny邊緣檢測能夠找到圖像中對象的邊界。但是Canny邊緣檢測器存在一些問題,即:

  • 需要手動驗證(將下部和上值設置為滯后閾值,是一種需要實驗和視覺驗證的手動過程);
  • 不具備通用性(對不同照明條件下捕獲的相同圖像,適用于一個圖像,卻不適用于另一個圖像);
  • 通常需要許多預處理步驟(即轉換為灰度,模糊/平滑等),以獲得良好的邊緣圖。

整體嵌套邊緣檢測(HED)試圖通過端到端深神經網絡解決Canny邊緣檢測器的局限性。

該網絡接受RGB圖像作為輸入,然后將邊緣圖作為輸出產生。而且通過HED產生的邊緣圖在圖像中很好的保留了對象邊界。

2.2. 項目結構

2.3 deploy.prototxt, hed_pretrained_bsds.caffemodel下載

執行代碼的關鍵是獲取deploy.prototxt, hed_pretrained_bsds.caffemodel
https://github.com/opencv/opencv/blob/master/samples/dnn/edge_detection.py

https://github.com/seminar2012/hed

  • 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. We provide the pretrained model and training/testing code for the edge detection framework Holistically-Nested Edge Detection (HED).
  • Please see the Arxiv or ICCV paper for technical details. The pretrained model (fusion-output) gives ODS=.790 and OIS=.808 result on BSDS benchmark dataset.
  • Download the pretrained model (56MB) from (http://vcl.ucsd.edu/hed/hed_pretrained_bsds.caffemodel) and place it in examples/hed/ folder.

3. 源碼

3.1 對圖像進行HED檢測

# USAGE
# python detect_edges_image.py --edge-detector hed_model --image images/bird.jpg

# 導入必要的包
import argparse
import cv2
import os
import imutils

# 構建命令行參數及解析
# --edge-detector Holistically-Nested Edge Detection檢測器模型路徑
# --image 圖片路徑
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--edge-detector", type=str, required=True,
                help="path to OpenCV's deep learning edge detector")
ap.add_argument("-i", "--image", type=str, required=True,
                help="path to input image")
args = vars(ap.parse_args())


class CropLayer(object):
    def __init__(self, params, blobs):
        # 初始化剪切區域開始和結束點的坐標
        self.xstart = 0
        self.ystart = 0
        self.xend = 0
        self.yend = 0

    # 計算輸入圖像的體積
    def getMemoryShapes(self, inputs):
        # 剪切類將接收倆個參數
        # 剪切第一個輸入blob以匹配第二個blob,保持批次和通道數
        # 輸出輸入容積的形狀及目標形狀
        # 提取批量大小及通道數
        # 分別提取目標形狀的高和寬
        (inputShape, targetShape) = (inputs[0], inputs[1])
        (batchSize, numChannels) = (inputShape[0], inputShape[1])
        (H, W) = (targetShape[2], targetShape[3])

        # 計算開始和結束剪切坐標的值
        self.xstart = int((inputShape[3] - targetShape[3]) // 2)
        self.ystart = int((inputShape[2] - targetShape[2]) // 2)
        self.xend = self.xstart + W
        self.yend = self.ystart + H

        # 返回體積,接下來進行實際裁剪
        return [[batchSize, numChannels, H, W]]

    def forward(self, inputs):
        return [inputs[0][:, :, self.ystart:self.yend, self.xstart:self.xend]]


# 從磁盤加載序列化的邊緣檢測器模型
print("[INFO] loading edge detector...")
protoPath = os.path.sep.join([args["edge_detector"],
                              "deploy.prototxt"])
modelPath = os.path.sep.join([args["edge_detector"],
                              "hed_pretrained_bsds.caffemodel"])
net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

# 綁定剪裁類到模型
cv2.dnn_registerLayer("Crop", CropLayer)

# 加載輸入圖像,獲取其維度
image = cv2.imread(args["image"])
image = imutils.resize(image, width=400)
(H, W) = image.shape[:2]

# 轉換圖像為灰度圖,高斯平滑,執行Canny邊緣檢測
print("[INFO] performing Canny edge detection...")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(blurred, 30, 150)

# 根據輸入圖像為全面的嵌套邊緣檢測器(Holistically-Nested Edge Detector)構建一個輸出blob
blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(W, H),
                             mean=(104.00698793, 116.66876762, 122.67891434),
                             swapRB=False, crop=False)

# # 設置blob作為網絡的輸入并執行算法以計算邊緣圖
print("[INFO] performing holistically-nested edge detection...")
net.setInput(blob)
hed = net.forward()
# 調整輸出為原始圖像尺寸的大小
hed = cv2.resize(hed[0, 0], (W, H))
# 將圖像像素縮回到范圍[0,255]并確保類型為“UINT8”
hed = (255 * hed).astype("uint8")

# 展示HED邊緣檢測的結果及Canny邊緣檢測的結果
cv2.imshow("Input", image)
cv2.imshow("Canny", canny)
cv2.imshow("HED", hed)
cv2.waitKey(0)

3.2 對視頻進行HED檢測

# USAGE 默認使用電腦自帶的攝像頭
# python detect_edges_video.py --edge-detector hed_model
# 使用視頻文件流
# python detect_edges_video.py --edge-detector hed_model --input xl.mp4

# 導入必要的包
from imutils.video import VideoStream
import argparse
import imutils
import time  # 此模塊允許放置睡眠命令以允許視頻流建立和“熱身”。
import cv2
import os

# 構建命令行參數及解析
# --edge-detector Holistically-Nested Edge Detection檢測器模型路徑
# --input 視頻源:網絡攝像頭,視頻文件或其他源。
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--edge-detector", type=str, required=True,
                help="path to OpenCV's deep learning edge detector")
ap.add_argument("-i", "--input", type=str,
                help="path to optional input video (webcam will be used otherwise)")
args = vars(ap.parse_args())


class CropLayer(object):
    def __init__(self, params, blobs):
        # 初始化剪切區域開始和結束點的坐標
        self.xstart = 0
        self.ystart = 0
        self.xend = 0
        self.yend = 0

    # 計算輸入圖像的體積
    def getMemoryShapes(self, inputs):
        # 剪切類將接收倆個參數
        # 剪切第一個輸入blob以匹配第二個blob,保持批次和通道數
        # 輸出輸入容積的形狀及目標形狀
        # 提取批量大小及通道數
        # 分別提取目標形狀的高和寬
        (inputShape, targetShape) = (inputs[0], inputs[1])
        (batchSize, numChannels) = (inputShape[0], inputShape[1])
        (H, W) = (targetShape[2], targetShape[3])

        # 計算開始和結束剪切坐標的值
        self.xstart = int((inputShape[3] - targetShape[3]) // 2)
        self.ystart = int((inputShape[2] - targetShape[2]) // 2)
        self.xend = self.xstart + W
        self.yend = self.ystart + H

        # 返回體積,接下來進行實際裁剪
        return [[batchSize, numChannels, H, W]]

    def forward(self, inputs):
        # 使用派生(x,y)-oordinate來執行裁剪
        return [inputs[0][:, :, self.ystart:self.yend, self.xstart:self.xend]]


# 初始化視頻流,腳本將動態選取使用視頻文件流還是網絡攝像頭流
webcam = not args.get("input", False)

# 如果未提供視頻文件路徑,則使用電腦自帶攝像頭
if webcam:
    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    time.sleep(2.0)
# 否則,獲取視頻文件流指針
else:
    print("[INFO] opening video file...")
    vs = cv2.VideoCapture(args["input"])

# 從磁盤加載序列化的HED檢測器模型
print("[INFO] loading edge detector...")
protoPath = os.path.sep.join([args["edge_detector"],
                              "deploy.prototxt"])
modelPath = os.path.sep.join([args["edge_detector"],
                              "hed_pretrained_bsds.caffemodel"])
net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

# 將剪裁類注冊到模型
cv2.dnn_registerLayer("Crop", CropLayer)

# 遍歷視頻流的幀
while True:
    # 獲取每一幀,如果使用網絡攝像頭,獲取下一幀
    frame = vs.read()
    frame = frame if webcam else frame[1]

    # 如果在處理視頻文件流,沒有獲取到幀則代表已經到了文件尾部,則跳出循環
    if not webcam and frame is None:
        break

    # 等比例縮放幀為寬度500,并獲取其維度
    frame = imutils.resize(frame, width=300)
    (H, W) = frame.shape[:2]

    # 轉換灰度圖,高斯模糊并執行Canny邊緣檢測
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    canny = cv2.Canny(blurred, 30, 150)

    # 為HED邊緣檢測器構建輸入幀的blob,設置blob,并執行檢測以計算邊緣圖
    blob = cv2.dnn.blobFromImage(frame, scalefactor=1.0, size=(W, H),
                                 mean=(104.00698793, 116.66876762, 122.67891434),
                                 swapRB=False, crop=False)
    net.setInput(blob)
    hed = net.forward()
    hed = cv2.resize(hed[0, 0], (W, H))
    hed = (255 * hed).astype("uint8")

    # 展示Canny、HED的檢測結果
    cv2.imshow("Frame", frame)
    cv2.imshow("Canny", canny)
    cv2.imshow("HED", hed)
    key = cv2.waitKey(1)  0xFF
    # 按下‘q'鍵表示退出循環
    if key == ord("q"):
        break

# 如果在使用網絡攝像頭流,則終止相機視頻流
if webcam:
    vs.stop()
# 否則,釋放視頻文件流指針
else:
    vs.release()

# 關閉所有打開的window
cv2.destroyAllWindows()

參考

 https://www.pyimagesearch.com/2019/03/04/holistically-nested-edge-detection-with-opencv-and-deep-learning/

到此這篇關于使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測的文章就介紹到這了,更多相關OpenCV和深度學習全面嵌套邊緣檢測內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python OpenCV實現邊緣檢測
  • OpenCV中Canny邊緣檢測的實現
  • Python實現Opencv cv2.Canny()邊緣檢測
  • OpenCV實現灰度、高斯模糊、邊緣檢測的示例
  • Python使用Opencv實現邊緣檢測以及輪廓檢測的實現
  • 如何利用Python 進行邊緣檢測
  • python實現canny邊緣檢測
  • OpenCV半小時掌握基本操作之邊緣檢測

標簽:鷹潭 四川 黑龍江 黔西 常德 上海 惠州 益陽

巨人網絡通訊聲明:本文標題《使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測》,本文關鍵詞  使用,Python,中,OpenCV,和,深度,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測》相關的同類信息!
  • 本頁收集關于使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 永靖县| 梨树县| 承德市| 尼玛县| 邵东县| 开原市| 宜黄县| 东兰县| 肇源县| 丰城市| 织金县| 溧水县| 镇远县| 滦南县| 宁阳县| 兴和县| 光山县| 海晏县| 交城县| 太谷县| 镶黄旗| 大邑县| 西昌市| 民乐县| 平泉县| 临沭县| 保靖县| 太原市| 洛川县| 沧州市| 屏东市| 土默特右旗| 睢宁县| 页游| 浮梁县| 安丘市| 紫金县| 察雅县| 翁牛特旗| 玉环县| 且末县|