亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

coco數(shù)據(jù)集標(biāo)注圖轉(zhuǎn)為二值圖python(附代碼)

系統(tǒng) 2442 0

coco數(shù)據(jù)集大概有8w張以上的圖片,而且每幅圖都有精確的邊緣mask標(biāo)注。

后面后分享一個labelme標(biāo)注的json或xml格式轉(zhuǎn)二值圖的源碼(以備以后使用)

而我現(xiàn)在在研究顯著性目標(biāo)檢測,需要的是邊緣mask的二值圖像。搜了很久,并沒有人做過這種工作,只能得到如下的掩膜圖

coco數(shù)據(jù)集標(biāo)注圖轉(zhuǎn)為二值圖python(附代碼)_第1張圖片

而我需要的圖像為二值圖,如下

coco數(shù)據(jù)集標(biāo)注圖轉(zhuǎn)為二值圖python(附代碼)_第2張圖片

說下 我的過程 并附上代碼:

首先,coco數(shù)據(jù)集將所有的8w多張圖片標(biāo)注信息整合到一個json文件中,所以我們需要將單張圖片標(biāo)注信息json文件提取出來,以下是批量提取腳本。

注: 需要改動地方 1)第6行:將json_file改為原coco數(shù)據(jù)集json文件的地址 (coco/annotations/xxxxx.json)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2)? 第13行:設(shè)置需要提取的圖片數(shù)量 我是提取82000張

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3)第37行:設(shè)置存儲json文件的目錄 需要新建空文件夾 我是放在./coco_single_object下

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4)第33-35行:可選 將圖片的名稱寫入data.txt中 不需要的話可以注釋掉

            
               1
            
            
              #
            
            
               -*- coding:utf-8 -*-
            
            
               2
            
            
              from
            
            
              __future__
            
            
              import
            
            
               print_function

            
            
               3
            
            
              import
            
            
               json

            
            
               4
            
            
               5
            
            
              #
            
            
              json文件的地址 需要手動設(shè)置
            
            
               6
            
             json_file=
            
              '
            
            
              ../pycocotools/instances_train2014.json
            
            
              '
            
            
              #
            
            
               # Object Instance 類型的標(biāo)注
            
            
               7
            
            
              #
            
            
               person_keypoints_val2017.json  # Object Keypoint 類型的標(biāo)注格式
            
            
               8
            
            
              #
            
            
               captions_val2017.json  # Image Caption的標(biāo)注格式
            
            
               9
            
            
              10
            
             data=json.load(open(json_file,
            
              '
            
            
              r
            
            
              '
            
            
              ))

            
            
              11
            
            
              12
            
            
              #
            
            
              設(shè)置需要提取的圖片數(shù)量 我設(shè)置提取82000張
            
            
              13
            
            
              for
            
             i 
            
              in
            
             range(82000
            
              ):

            
            
              14
            
                 data_2 =
            
               {}

            
            
              15
            
                 data_2[
            
              '
            
            
              info
            
            
              '
            
            ] = data[
            
              '
            
            
              info
            
            
              '
            
            
              ]

            
            
              16
            
                 data_2[
            
              '
            
            
              licenses
            
            
              '
            
            ] = data[
            
              '
            
            
              licenses
            
            
              '
            
            
              ]

            
            
              17
            
                 data_2[
            
              '
            
            
              images
            
            
              '
            
            ] = [data[
            
              '
            
            
              images
            
            
              '
            
            ][i]]  
            
              #
            
            
               只提取第一張圖片
            
            
              18
            
                 data_2[
            
              '
            
            
              categories
            
            
              '
            
            ] = data[
            
              '
            
            
              categories
            
            
              '
            
            
              ]

            
            
              19
            
                 annotation =
            
               []

            
            
              20
            
            
              21
            
            
              #
            
            
               通過imgID 找到其所有對象
            
            
              22
            
                 imgID = data_2[
            
              '
            
            
              images
            
            
              '
            
            ][0][
            
              '
            
            
              id
            
            
              '
            
            
              ]

            
            
              23
            
            
              for
            
             ann 
            
              in
            
             data[
            
              '
            
            
              annotations
            
            
              '
            
            
              ]:

            
            
              24
            
            
              if
            
             ann[
            
              '
            
            
              image_id
            
            
              '
            
            ] ==
            
               imgID:

            
            
              25
            
            
                          annotation.append(ann)

            
            
              26
            
            
              27
            
                 data_2[
            
              '
            
            
              annotations
            
            
              '
            
            ] =
            
               annotation

            
            
              28
            
            
              #
            
            
               保存到新的JSON文件,便于查看數(shù)據(jù)特點
            
            
              29
            
            
              #
            
            
              img_file 獲取圖片名稱
            
            
              30
            
                 img_file=data_2[
            
              '
            
            
              images
            
            
              '
            
            ][0][
            
              '
            
            
              file_name
            
            
              '
            
            
              ]

            
            
              31
            
                 img_first=img_file.split(
            
              "
            
            
              .
            
            
              "
            
            
              )[0]

            
            
              32
            
            
              #
            
            
              將提取出的圖片寫入data.txt文件中并換行 (optional)
            
            
              33
            
            
              #
            
            
               with open('./coco_single_object/data.txt',mode='a') as f:
            
            
              34
            
            
              #
            
            
                       f.write(img_file)
            
            
              35
            
            
              #
            
            
                       f.write("\n")
            
            
              36
            
            
              #
            
            
              設(shè)置存儲目錄 我的是存在當(dāng)前目錄下coco_single_object文件夾下 需要手動創(chuàng)建空文件夾
            
            
              37
            
                 json.dump(data_2, open(
            
              '
            
            
              ./coco_single_object/
            
            
              '
            
            +img_first+
            
              '
            
            
              .json
            
            
              '
            
            , 
            
              '
            
            
              w
            
            
              '
            
            ), indent=4)  
            
              #
            
            
               indent=4 更加美觀顯示
            
          

最后的結(jié)果是82000張 json文件

coco數(shù)據(jù)集標(biāo)注圖轉(zhuǎn)為二值圖python(附代碼)_第3張圖片

---------------------------------------------------------------------------------------------------------------------------------------

有了單張json文件之后,就是將mask掩膜提取出二值圖片的過程了

注:函數(shù)傳入4個參數(shù)?json_path,img_path,color_img_save,binary_img_save

? ? ? ?分別對應(yīng)? json_path: 上一步提取出的json文件的文件夾路徑

? ? ? ? ? ? ? ? ? ? ? ?img_path: coco數(shù)據(jù)集下載時原圖目錄?

? ? ? ? ? ? ? ? ? ? ? ?color_img_save: 存放原圖的目錄 (需要新建此文件夾)

? ? ? ? ? ? ? ? ? ? ? ?binary_img_save: 存放二值圖的目錄(需要新建此文件夾)

            
               1
            
            
              from
            
            
              __future__
            
            
              import
            
            
               print_function

            
            
               2
            
            
              from
            
             pycocotools.coco 
            
              import
            
            
               COCO

            
            
               3
            
            
              import
            
            
               os, sys, zipfile

            
            
               4
            
            
              import
            
            
               urllib.request

            
            
               5
            
            
              import
            
            
               shutil

            
            
               6
            
            
              import
            
            
               numpy as np

            
            
               7
            
            
              import
            
            
               skimage.io as io

            
            
               8
            
            
              import
            
            
               matplotlib.pyplot as plt

            
            
               9
            
            
              import
            
            
               pylab

            
            
              10
            
             pylab.rcParams[
            
              '
            
            
              figure.figsize
            
            
              '
            
            ] = (8.0, 10.0
            
              )

            
            
              11
            
            
              import
            
            
               os

            
            
              12
            
            
              def
            
            
               get_single_binaryImg(json_path,img_path,color_img_save,binary_img_save):

            
            
              13
            
            
              #
            
            
               json_path json文件路徑  從coco數(shù)據(jù)集的annotations標(biāo)注json文件中提取出的單個json文件
            
            
              14
            
            
              #
            
            
                img_path 原圖目錄   下載coco數(shù)據(jù)集時的原圖目錄
            
            
              15
            
            
              #
            
            
               color_img_save 原圖存放目錄
            
            
              16
            
            
              #
            
            
               binary_img_save 二值圖存放目錄
            
            
              17
            
                 dir=
            
              os.listdir(json_path)

            
            
              18
            
            
              for
            
             jfile 
            
              in
            
            
               dir:

            
            
              19
            
                     annFile =
            
              os.path.join(json_path,jfile)

            
            
              20
            
                     coco =
            
               COCO(annFile)

            
            
              21
            
                     imgIds =
            
               coco.getImgIds()

            
            
              22
            
                     img =
            
               coco.loadImgs(imgIds[0])[0]

            
            
              23
            
                     dataDir =
            
               img_path

            
            
              24
            
                     shutil.copy(os.path.join(dataDir, img[
            
              '
            
            
              file_name
            
            
              '
            
            
              ]), color_img_save)

            
            
              25
            
            
              26
            
            
              #
            
            
               load and display instance annotations
            
            
              27
            
            
              #
            
            
               加載實例掩膜
            
            
              28
            
                     catIds =
            
               []

            
            
              29
            
            
              for
            
             ann 
            
              in
            
             coco.dataset[
            
              '
            
            
              annotations
            
            
              '
            
            
              ]:

            
            
              30
            
            
              if
            
             ann[
            
              '
            
            
              image_id
            
            
              '
            
            ] ==
            
               imgIds[0]:

            
            
              31
            
                             catIds.append(ann[
            
              '
            
            
              category_id
            
            
              '
            
            
              ])

            
            
              32
            
            
              33
            
                     annIds = coco.getAnnIds(imgIds=img[
            
              '
            
            
              id
            
            
              '
            
            ], catIds=catIds, iscrowd=
            
              None)

            
            
              34
            
                     width = img[
            
              '
            
            
              width
            
            
              '
            
            
              ]

            
            
              35
            
                     height = img[
            
              '
            
            
              height
            
            
              '
            
            
              ]

            
            
              36
            
                     anns =
            
               coco.loadAnns(annIds)

            
            
              37
            
                     mask_pic =
            
               np.zeros((height, width))

            
            
              38
            
            
              for
            
             single 
            
              in
            
            
               anns:

            
            
              39
            
                         mask_single =
            
               coco.annToMask(single)

            
            
              40
            
                         mask_pic +=
            
               mask_single

            
            
              41
            
            
              42
            
            
              for
            
             row 
            
              in
            
            
               range(height):

            
            
              43
            
            
              for
            
             col 
            
              in
            
            
               range(width):

            
            
              44
            
            
              if
            
             (mask_pic[row][col] >
            
               0):

            
            
              45
            
                                 mask_pic[row][col] = 255

            
              46
            
            
              47
            
                     imgs = np.zeros(shape=(height, width, 3), dtype=
            
              np.float32)

            
            
              48
            
                     imgs[:, :, 0] =
            
               mask_pic[:, :]

            
            
              49
            
                     imgs[:, :, 1] =
            
               mask_pic[:, :]

            
            
              50
            
                     imgs[:, :, 2] =
            
               mask_pic[:, :]

            
            
              51
            
                     imgs =
            
               imgs.astype(int)

            
            
              52
            
                     img_name = img[
            
              '
            
            
              file_name
            
            
              '
            
            ].split(
            
              "
            
            
              .
            
            
              "
            
            
              )[0]

            
            
              53
            
                     plt.imsave(binary_img_save + 
            
              "
            
            
              /
            
            
              "
            
             + img_name + 
            
              "
            
            
              .png
            
            
              "
            
            
              , imgs)

            
            
              54
            
            
              55
            
            
              if
            
            
              __name__
            
             == 
            
              '
            
            
              __main__
            
            
              '
            
            
              :

            
            
              56
            
            
              57
            
                 json_path =r
            
              "
            
            
              G:\jianfeng\code\dataset\cocoapi-master\PythonAPI\get_json\test
            
            
              "
            
            
              58
            
                 img_path=r
            
              "
            
            
              G:\jianfeng\code\dataset\coco\train2014
            
            
              "
            
            
              59
            
                 color_img_save = r
            
              "
            
            
              G:\jianfeng\code\dataset\cocoapi-master\PythonAPI\get_json\color_img
            
            
              "
            
            
              60
            
                 binary_img_save = r
            
              "
            
            
              G:\jianfeng\code\dataset\cocoapi-master\PythonAPI\get_json\binary_img
            
            
              "
            
            
              61
            
            
              62
            
                 get_single_binaryImg(json_path,img_path,color_img_save,binary_img_save)
          

最終出現(xiàn)這些結(jié)果:

coco數(shù)據(jù)集標(biāo)注圖轉(zhuǎn)為二值圖python(附代碼)_第4張圖片 ? ? ? coco數(shù)據(jù)集標(biāo)注圖轉(zhuǎn)為二值圖python(附代碼)_第5張圖片

最后在搜索得到二值圖方法時,也找到了一個不錯的源碼,但是他是將labelme格式的json或者xml轉(zhuǎn)為二值圖,雖然不是將coco格式轉(zhuǎn)為二值圖,但是記錄下也許以后也會用的到

https://github.com/samr28/labelme-to-binary-image

?參考:

https://blog.csdn.net/wc781708249/article/details/79603522

https://blog.csdn.net/u013735511/article/details/79099483


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 香蕉狠狠干| 久久鸭综合久久国产 | 亚洲欧美精品中字久久99 | 久久久免费 | 美女a毛片 | 夜夜摸夜夜操 | 日本色综合网 | 欧美熟a| 伊人网在线视频 | 欧美亚洲精品小说一区二三区 | 一级欧美视频 | 亚洲精品久久片久久 | 全部免费毛片在线 | 老司机福利免费 | 999无色码中文字幕 999小视频 | 激情亚洲婷婷 | 美女久久精品 | 香蕉在线精品一区二区 | 精品伊人久久久久7777人 | 亚洲国产成人久久精品图片 | 久久久久亚洲国产 | 亚洲精品美女国产一区 | 九九天天影视 | 九九久久久久午夜精选 | 夜夜操com| 国产色婷婷视频在线观看 | 亚洲欧美国产18 | 久久香蕉国产线看观看乱码 | 免费在线观看福利 | 国产精品欧美一区二区在线看 | 久久这里有精品视频任我鲁 | 日本欧美一二三区色视频 | 国产高清一区二区三区免费视频 | 香蕉青草久久成人网 | 色综合五月天 | 一级片在线免费看 | 精品天海翼一区二区 | 中文字幕在线视频观看 | 成人免费观看网站 | 香蕉久久夜色精品国产2020 | 大乳孕妇一级毛片 |