將labelme 生成的.json文件進行可視化的代碼+label.png 對比度處理的matlab代碼


labelme_to_dataset 指令的代碼實現:

show.py文件

#!E:\Anaconda3\python.exe

import argparse
import json
import os
import os.path as osp

import PIL.Image
import yaml

from labelme import utils


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    args = parser.parse_args()

    json_file = args.json_file

    out_dir = osp.basename(json_file).replace('.', '_')
    out_dir = osp.join(osp.dirname(json_file), out_dir)
    os.mkdir(out_dir)

    data = json.load(open(json_file))

    img = utils.img_b64_to_array(data['imageData'])
    lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])

    lbl_viz = utils.draw_label(lbl, img, lbl_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    info = dict(label_names=lbl_names)

    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    print('wrote data to %s' % out_dir)


if __name__ == '__main__':
    main()

調整label.png對比度matlab代碼

clc;
close all;
clear all;
 
src_img = imread('C:\\Users\\Fourmi\\Desktop\\5_json\\label.png');  

figure (1) 
subplot(321),imshow(src_img),title('原圖像');%顯示原始圖像  
subplot(322),imhist(src_img),title('原圖像直方圖');%顯示原始圖像直方圖  

matlab_eq=histeq(src_img);         %利用matlab的函數直方圖均衡化
subplot(323),imshow(matlab_eq),title('matlab直方圖均衡化原圖像');%顯示原始圖像  
subplot(324),imhist(matlab_eq),title('matlab均衡化后的直方圖');%顯示原始圖像直方圖 

dst_img=myHE(src_img);             %利用自己寫的函數直方圖均衡化
subplot(325),imshow(dst_img),title('手寫均衡化效果');%顯示原始圖像
imwrite(dst_img,'C:\Users\Fourmi\Desktop\result5.png')
subplot(326),imhist(dst_img),title('手寫均衡化直方圖');%顯示原始圖像直方圖 

myHe.m 文件

function dst_img=myHE(src_img)  

[height,width] = size(src_img);
dst_img=uint8(zeros(height,width));
%進行像素灰度統計;    
NumPixel = zeros(1,256);%統計各灰度數目,共256個灰度級    
for i = 1:height    
    for j = 1: width    
        NumPixel(src_img(i,j) + 1) = NumPixel(src_img(i,j) + 1) + 1;%對應灰度值像素點數量增加一    
    end    
end    
%計算灰度分布密度    
ProbPixel = zeros(1,256);    
for i = 1:256    
    ProbPixel(i) = NumPixel(i) / (height * width * 1.0);    
end    
%計算累計直方圖分布    
CumuPixel = zeros(1,256);    
for i = 1:256    
    if i == 1    
        CumuPixel(i) = ProbPixel(i);    
    else    
        CumuPixel(i) = CumuPixel(i - 1) + ProbPixel(i);    
    end    
end    
  
% 指定范圍進行均衡化  
% pixel_max=max(max(I));  
% pixel_min=min(min(I));  
pixel_max=255;  
pixel_min=0;  
%對灰度值進行映射(均衡化)    
for i = 1:height    
    for j = 1: width    
        dst_img(i,j) = CumuPixel(src_img(i,j)+1)*(pixel_max-pixel_min)+pixel_min;    
    end    
end    
return;

 

 
        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM