<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          使用PixelLib來(lái)實(shí)現(xiàn)圖像分割

          共 7749字,需瀏覽 16分鐘

           ·

          2022-02-17 19:35

          點(diǎn)擊上方小白學(xué)視覺(jué)”,選擇加"星標(biāo)"或“置頂

          重磅干貨,第一時(shí)間送達(dá)

          年來(lái),計(jì)算機(jī)視覺(jué)快速發(fā)展。目前流行的計(jì)算機(jī)視覺(jué)技術(shù)如圖像分類(lèi)、目標(biāo)檢測(cè)等已被廣泛應(yīng)用于解決許多計(jì)算機(jī)視覺(jué)問(wèn)題。在圖像分類(lèi)中,對(duì)整個(gè)圖像進(jìn)行分類(lèi)。在目標(biāo)檢測(cè)中,則是通過(guò)檢測(cè)圖像中單個(gè)目標(biāo)的位置來(lái)擴(kuò)展圖像分類(lèi)。
          圖像分割
          一些計(jì)算機(jī)視覺(jué)問(wèn)題需要讓計(jì)算機(jī)對(duì)圖像內(nèi)容有更深入的理解。分類(lèi)和目標(biāo)檢測(cè)可能不適合解決這些問(wèn)題,我們非常需要一種有效的技術(shù)來(lái)解這類(lèi)的計(jì)算機(jī)視覺(jué)問(wèn)題,圖像分割技術(shù)應(yīng)運(yùn)而生。
          每個(gè)圖像都由一組像素值組成。圖像分割是在像素級(jí)對(duì)圖像進(jìn)行分類(lèi)的任務(wù)。機(jī)器能夠根據(jù)分配給圖像中每個(gè)像素值的類(lèi)將圖像分割成不同的段,從而更有效地分析圖像。
          在圖像分割中采用的獨(dú)特技術(shù)使其適用于解決某些計(jì)算機(jī)視覺(jué)問(wèn)題。這些問(wèn)題需要有關(guān)圖像中出現(xiàn)的對(duì)象詳細(xì)信息,無(wú)法通過(guò)對(duì)整個(gè)圖像進(jìn)行分類(lèi)或?yàn)閳D像中存在的對(duì)象提供邊界框來(lái)提供詳細(xì)信息。圖像分割的一些主要應(yīng)用包括:
          • 幫助無(wú)人駕駛汽車(chē)視覺(jué)系統(tǒng)有效的了解道路場(chǎng)景。
          • 醫(yī)學(xué)圖像分割:為執(zhí)行診斷測(cè)試提供身體部位的分割。
          • 衛(wèi)星圖像分析。
          圖像分割有兩種主要類(lèi)型:
          語(yǔ)義分割:使用相同類(lèi)別的對(duì)象使用相同的顏色映射進(jìn)行分割。
          實(shí)例分割:它不同于語(yǔ)義分割,它會(huì)對(duì)同一對(duì)象的不同實(shí)例用不同的顏色映射來(lái)進(jìn)行分割。
          下面三幅圖片有助于你理解語(yǔ)義分割和實(shí)例分割。
          原圖:
          語(yǔ)義分割:
          實(shí)例分割:
          PixelLib:是為了在現(xiàn)實(shí)生活中更容易實(shí)現(xiàn)圖像分割而構(gòu)建的庫(kù)。PixelLib是一個(gè)靈活的庫(kù),可以集成到需要應(yīng)用圖像分割的軟件解決方案中。
          語(yǔ)義分割和實(shí)例分割可以用五行代碼實(shí)現(xiàn)。
          安裝PixelLib及其依賴項(xiàng):
          安裝最新版本的tensorflow(tensorflow 2.0),使用:
          • pip3 install tensorflow
          使用以下命令安裝opencv python:
          • pip3 install opencv-python
          使用以下命令安裝scikit映像:
          • pip3 install scikit-image
          安裝Pillow :
          • pip3 install pillow
          安裝Pixellib:
          • pip3 install pixellib
          用PixelLib實(shí)現(xiàn)語(yǔ)義分割:
          在pascal voc數(shù)據(jù)集上訓(xùn)練deeplabv3+模型來(lái)實(shí)現(xiàn)語(yǔ)義分割的代碼。
          import pixellibfrom pixellib.semantic import semantic_segmentation
          segment_image = semantic_segmentation()segment_image.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5") segment_image.segmentAsPascalvoc("path_to_image", output_image_name = "path_to_output_image")
          我們來(lái)觀察每一行代碼:
          import pixellibfrom pixellib.semantic import semantic_segmentation segment_image = semantic_segmentation()
          執(zhí)行語(yǔ)義分割的類(lèi)是從pixelLib導(dǎo)入的,我們創(chuàng)建了該類(lèi)的一個(gè)實(shí)例。
          segment_image.load_pascalvoc_model(“deeplabv3_xception_tf_dim_ordering_tf_kernels.h5”)
          在上面的代碼中,我們加載了在pascal voc上訓(xùn)練的用于分割對(duì)象的xception 模型。模型可以從這里下載。
          • https://github.com/ayoolaolafenwa/PixelLib/releases/download/1.1/deeplabv3_xception_tf_dim_ordering_tf_kernels.h5
          segment_image.segmentAsPascalvoc(“path_to_image”, output_image_name = “path_to_output_image)
          我們加載該函數(shù)對(duì)圖像執(zhí)行分割。這個(gè)函數(shù)有兩個(gè)參數(shù)…
          • path_to_image:這個(gè)是要分割的圖像路徑。
          • output_image_name:這個(gè)是保存分割圖像的路徑。它將保存在當(dāng)前工作目錄中。
          sample1.jpg:
          import pixellibfrom pixellib.semantic import semantic_segmentation
          segment_image = semantic_segmentation()segment_image.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5") segment_image.segmentAsPascalvoc("sample1.jpg", output_image_name = "image_new.jpg")
          對(duì)圖像中的對(duì)象進(jìn)行分割并保存結(jié)果。如果需要,可以在圖像上應(yīng)用覆蓋分割。
          segment_image.segmentAsPascalvoc("sample1.jpg", output_image_name = "image_new.jpg", overlay = True)
          我們添加了額外的參數(shù)overlay并將其設(shè)置為true,我們得到了一個(gè)對(duì)象上具有覆蓋分割的圖像。
          通過(guò)修改下面的代碼,可以檢查執(zhí)行分割所需的時(shí)間。
          import pixellibfrom pixellib.semantic import semantic_segmentationimport time
          segment_image = semantic_segmentation()segment_image.load_pascalvoc_model("pascal.h5")
          start = time.time()segment_image.segmentAsPascalvoc("sample1.jpg", output_image_name= "image_new.jpg")
          end = time.time()print(f"Inference Time: {end-start:.2f}seconds")
          Inference Time: 7.38seconds
          對(duì)圖像進(jìn)行語(yǔ)義分割需要7.38秒。
          該模型是在pascal voc數(shù)據(jù)集上訓(xùn)練的,這個(gè)數(shù)據(jù)集有20個(gè)對(duì)象類(lèi)別。
          對(duì)象及其對(duì)應(yīng)的顏色映射:
          PixelLib也是可以返回分割輸出的數(shù)組:
          使用此代碼獲取分割輸出的數(shù)組,
          output, segmap = segment_image.segmentAsPascalvoc()
          通過(guò)修改下面的語(yǔ)義分割代碼,可以測(cè)試獲取數(shù)組的代碼并打印出輸出的形狀。
          import pixellibfrom pixellib.semantic import semantic_segmentationimport cv2
          segment_image = semantic_segmentation()segment_image.load_pascalvoc_model("pascal.h5")output, segmap = segment_image.segmentAsPascalvoc("sample1.jpg")cv2.imwrite("img.jpg", output)print(output.shape)
          使用此代碼獲取輸出和覆蓋分割的數(shù)組,
          segmap, segoverlay = segment_image.segmentAsPascalvoc(overlay = True)
          import pixellibfrom pixellib.semantic import semantic_segmentationimport cv2segment_image = semantic_segmentation()segment_image.load_pascalvoc_model("pascal.h5")segmap, segoverlay = segment_image.segmentAsPascalvoc("sample1.jpg", overlay= True)cv2.imwrite("img.jpg", segoverlay)print(segoverlay.shape)


          使用PIXELLIB的實(shí)例分割:
          基于Mask R-CNN框架的PixelLib實(shí)例分割。
          實(shí)現(xiàn)實(shí)例分割的代碼:
          import pixellibfrom pixellib.instance import instance_segmentation
          segment_image = instance_segmentation()segment_image.load_model("mask_rcnn_coco.h5") segment_image.segmentImage("path_to_image", output_image_name = "output_image_path")


          觀察每一行代碼


          import pixellibfrom pixellib.instance import instance_segmentationsegment_image = instance_segmentation()
          導(dǎo)入執(zhí)行實(shí)例分割的類(lèi),我們創(chuàng)建了該類(lèi)的一個(gè)實(shí)例。
          segment_image.load_model("mask_rcnn_coco.h5")
          這是加載mask r-cnn模型執(zhí)行實(shí)例分割的代碼。從這里下載mask r-cnn模型。
          • https://github.com/ayoolaolafenwa/PixelLib/releases/download/1.2/mask_rcnn_coco.h5
          segment_image.segmentImage("path_to_image", output_image_name = "output_image_path")
          這是對(duì)圖像執(zhí)行實(shí)例分割的代碼,它需要兩個(gè)參數(shù):
          • path_to_image:模型要預(yù)測(cè)的圖像路徑。
          • output_image_path:保存分割結(jié)果的路徑。它將保存在當(dāng)前工作目錄中。
          sample2.jpg:

          import pixellibfrom pixellib.instance import instance_segmentation
          segment_image = instance_segmentation()segment_image.load_model("mask_rcnn_coco.h5") segment_image.segmentImage("sample2.jpg", output_image_name = "image_new.jpg")
          這是當(dāng)前工作目錄中保存的圖像。
          可以使用邊界框?qū)崿F(xiàn)分割。這可以通過(guò)修改代碼來(lái)實(shí)現(xiàn)。
          segment_image.segmentImage("path_to_image", output_image_name = "output_image_path", show_bboxes = True)
          我們添加了一個(gè)額外的參數(shù)show_bboxes并將其設(shè)置為true,分割掩碼由邊界框生成。
          通過(guò)修改下面的代碼,可以檢查執(zhí)行分割所需的時(shí)間。
          import pixellibfrom pixellib.instance import instance_segmentationimport time
          segment_image = instance_segmentation()segment_image.load_model("mask_rcnn_coco.h5")
          start = time.time()segment_image.segmentImage("former.jpg", output_image_name= "image_new.jpg")
          end = time.time()print(f"Inference Time: {end-start:.2f}seconds")
          Inference Time: 12.87seconds
          在圖像上運(yùn)行實(shí)例分割需要12.87秒。
          Mask Rúu CNN模型是在microsoftco數(shù)據(jù)集上訓(xùn)練的,該數(shù)據(jù)集有80個(gè)公共對(duì)象類(lèi)別。該模型可以對(duì)這些對(duì)象類(lèi)別進(jìn)行實(shí)例分割。
          Coco數(shù)據(jù)集中的對(duì)象類(lèi)別列表:
          [‘BG’, ‘person’, ‘bicycle’, ‘car’, ‘motorcycle’, ‘a(chǎn)irplane’, ‘bus’, ‘train’, ‘truck’, ‘boat’, ‘traffic light’, ‘fire hydrant’, ‘stop sign’, ‘parking meter’, ‘bench’, ‘bird’, ‘cat’, ‘dog’, ‘horse’, ‘sheep’, ‘cow’, ‘elephant’, ‘bear’, ‘zebra’, ‘giraffe’, ‘backpack’, ‘umbrella’, ‘handbag’, ‘tie’, ‘suitcase’, ‘frisbee’, ‘skis’, ‘snowboard’, ‘sports ball’, ‘kite’, ‘baseball bat’, ‘baseball glove’, ‘skateboard’, ‘surfboard’, ‘tennis racket’, ‘bottle’, ‘wine glass’, ‘cup’, ‘fork’, ‘knife’, ‘spoon’, ‘bowl’, ‘banana’, ‘a(chǎn)pple’, ‘sandwich’, ‘orange’, ‘broccoli’, ‘carrot’, ‘hot dog’, ‘pizza’, ‘donut’, ‘cake’, ‘chair’, ‘couch’, ‘potted plant’, ‘bed’, ‘dining table’, ‘toilet’, ‘tv’, ‘laptop’, ‘mouse’, ‘remote’, ‘keyboard’, ‘cell phone’, ‘microwave’, ‘oven’, ‘toaster’, ‘sink’, ‘refrigerator’, ‘book’, ‘clock’, ‘vase’, ‘scissors’, ‘teddy bear’, ‘hair drier’, ‘toothbrush’]
          PixelLib的專(zhuān)業(yè)用途有很多,例如分割。
          獲取以下數(shù)組:
          • 檢測(cè)到的對(duì)象數(shù)組
          • 對(duì)象對(duì)應(yīng)類(lèi)的id數(shù)組
          • 分割掩碼數(shù)組
          • 輸出的數(shù)組
          使用此代碼
          segmask, output = segment_image.segmentImage()
          通過(guò)修改下面的實(shí)例分割代碼,可以測(cè)試獲取數(shù)組的代碼并打印出輸出的形狀。
          import pixellibfrom pixellib.instance import instance_segmentationimport cv2
          instance_seg = instance_segmentation()instance_seg.load_model("mask_rcnn_coco.h5")segmask, output = instance_seg.segmentImage("sample2.jpg")cv2.imwrite("img.jpg", output)print(output.shape)
          通過(guò)包含參數(shù)show_bboxes,獲得帶邊界框的分割數(shù)組。
          segmask, output = segment_image.segmentImage(show_bboxes = True)
          import pixellibfrom pixellib.instance import instance_segmentationimport cv2
          instance_seg = instance_segmentation()instance_seg.load_model("mask_rcnn_coco.h5")segmask, output = instance_seg.segmentImage("sample2.jpg", show_bboxes= True)cv2.imwrite("img.jpg", output)print(output.shape)
          安裝PixelLib并用你的圖像來(lái)測(cè)試它。
          訪問(wèn)PixelLib的官方github存儲(chǔ)庫(kù)。
          • https://github.com/ayoolaolafenwa/PixelLib
          訪問(wèn)PixelLib的官方文檔
          • https://pixellib.readthedocs.io/en/latest/
          參考鏈接:https://towardsdatascience.com/image-segmentation-with-six-lines-0f-code-acb870a462e8
          下載1:OpenCV-Contrib擴(kuò)展模塊中文版教程
          在「小白學(xué)視覺(jué)」公眾號(hào)后臺(tái)回復(fù):擴(kuò)展模塊中文教程,即可下載全網(wǎng)第一份OpenCV擴(kuò)展模塊教程中文版,涵蓋擴(kuò)展模塊安裝、SFM算法、立體視覺(jué)、目標(biāo)跟蹤、生物視覺(jué)、超分辨率處理等二十多章內(nèi)容。

          下載2:Python視覺(jué)實(shí)戰(zhàn)項(xiàng)目52講
          小白學(xué)視覺(jué)公眾號(hào)后臺(tái)回復(fù):Python視覺(jué)實(shí)戰(zhàn)項(xiàng)目,即可下載包括圖像分割、口罩檢測(cè)、車(chē)道線檢測(cè)、車(chē)輛計(jì)數(shù)、添加眼線、車(chē)牌識(shí)別、字符識(shí)別、情緒檢測(cè)、文本內(nèi)容提取、面部識(shí)別等31個(gè)視覺(jué)實(shí)戰(zhàn)項(xiàng)目,助力快速學(xué)校計(jì)算機(jī)視覺(jué)。

          下載3:OpenCV實(shí)戰(zhàn)項(xiàng)目20講
          小白學(xué)視覺(jué)公眾號(hào)后臺(tái)回復(fù):OpenCV實(shí)戰(zhàn)項(xiàng)目20講,即可下載含有20個(gè)基于OpenCV實(shí)現(xiàn)20個(gè)實(shí)戰(zhàn)項(xiàng)目,實(shí)現(xiàn)OpenCV學(xué)習(xí)進(jìn)階。

          交流群


          歡迎加入公眾號(hào)讀者群一起和同行交流,目前有SLAM、三維視覺(jué)、傳感器自動(dòng)駕駛、計(jì)算攝影、檢測(cè)、分割、識(shí)別、醫(yī)學(xué)影像、GAN、算法競(jìng)賽等微信群(以后會(huì)逐漸細(xì)分),請(qǐng)掃描下面微信號(hào)加群,備注:”昵稱(chēng)+學(xué)校/公司+研究方向“,例如:”張三?+?上海交大?+?視覺(jué)SLAM“。請(qǐng)按照格式備注,否則不予通過(guò)。添加成功后會(huì)根據(jù)研究方向邀請(qǐng)進(jìn)入相關(guān)微信群。請(qǐng)勿在群內(nèi)發(fā)送廣告,否則會(huì)請(qǐng)出群,謝謝理解~


          瀏覽 44
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人黄色视频网 | 成人 逼特逼视频 | 琪琪在线视频 | 翔田千里一区二区三区 | 看操逼网|