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

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

系統(tǒng) 2702 0

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

后面后分享一個(gè)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)注信息整合到一個(gè)json文件中,所以我們需要將單張圖片標(biāo)注信息json文件提取出來,以下是批量提取腳本。

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

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

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3)第37行:設(shè)置存儲(chǔ)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文件的地址 需要手動(dòng)設(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 找到其所有對(duì)象
            
            
              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ù)特點(diǎn)
            
            
              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è)置存儲(chǔ)目錄 我的是存在當(dāng)前目錄下coco_single_object文件夾下 需要手動(dòng)創(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個(gè)參數(shù)?json_path,img_path,color_img_save,binary_img_save

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

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

? ? ? ? ? ? ? ? ? ? ? ?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文件中提取出的單個(gè)json文件
            
            
              14
            
            
              #
            
            
                img_path 原圖目錄   下載coco數(shù)據(jù)集時(shí)的原圖目錄
            
            
              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
            
            
              #
            
            
               加載實(shí)例掩膜
            
            
              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張圖片

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

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號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 欧美一级特黄乱妇高清视频 | 久久精品国产亚洲黑森林 | 欧美成人一区二免费视频 | 日日干天天爽 | 老司机成人午夜精品福利视频 | 国产激情在线 | a级爱爱视频 | 国产精品资源在线观看 | 亚洲欧美在线综合一区二区三区 | 99热成人精品热久久669 | 91视频免费观看 | 亚洲欧洲尹人香蕉综合 | 老子影院午夜伦手机不卡无 | freesex寂寞老妇hd | 福利在线视频观看 | 成人亚洲国产精品久久 | 色综合精品 | 国产v欧美v日韩在线观看 | 免费国产不卡午夜福在线 | 久久这里只有精品国产 | 99久久99热久久 | 日本三级强在线观看 | 国产亚洲精品一区999 | 韩国 欧美 日产 国产精品 | 欧美一级美片在线观看免费 | 日本高清中文字幕一区二区三区 | 操操操综合网 | 狠色狠狠色狠狠狠色综合久久 | 亚洲精品高清久久 | 青青操夜夜操 | 婷婷的久久五月综合先锋影音 | 国产日韩精品欧美一区色 | 亚洲国产人成在线观看69网站 | 久久久久久久久综合影视网 | 综合久久综合 | 九九影院在线观看 | 国产亚洲精品福利片 | japanese护士奶水 | 久久草精品视频 | 最近中文字幕无免费视频 | 国产成人永久免费视 |