• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

CG_图片轮廓mask提取-matlab & python

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

<script src="https://gist.github.com/novav/63a27bb4bb068be659f71dc9568aef84.js"></script>

 

% Windows
function convert_data()
    source_root_dir = \'E:/data/\';
    %cname = \'000003_1.jpg\';
    target_root_dir = \'E:/data/\';
    fine_height = 256; 
    fine_width = 192;
    mode = \'train\';
    [im_names, cloth_names] = textread([\'E:\data\train_pairs_ansi.txt\'],\'%s %s\');
    N = length(im_names);
	for i = 1:N;
        imname = im_names{i} ;
		cname = cloth_names{i};
        fprintf(\'%d/%d: %s %s\n\', i, N, imname, cname);
        convert([source_root_dir, mode], [target_root_dir, mode], cname)
    end
end    
    
function convert(source_root_dir, target_root_dir, cname)
    im_c = imread([source_root_dir \'/\' \'cloth-parse-seg/\' cname]);
    % save cloth mask
    mask = double((im_c(:,:,1) <= 250) & (im_c(:,:,2) <= 250) & (im_c(:,:,3) <= 250));
    mask = imfill(mask);
    mask = medfilt2(mask);
    imwrite(mask, [target_root_dir \'/cloth-parse-seg-mask/\' cname]);
end

  

python

import cv2
import os
import numpy as np



def convert(f, isShow=False):
    im_in = cv2.imread(f, cv2.IMREAD_GRAYSCALE)

    # th, im_th = cv2.threshold(im_in, 0, 255, cv2.THRESH_BINARY)
    th, im_th = cv2.threshold(im_in, 254, 255, cv2.THRESH_BINARY_INV) # 颜色大于240的位置,标注255
    # th, im_th = cv2.threshold(im_in, 200, 255, cv2.THRESH_OTSU)
    # th, im_th = cv2.threshold(im_in, 200, 255, cv2.THRESH_TRIANGLE)
    # th, im_th = cv2.threshold(im_in, 0, 254, cv2.THRESH_TRUNC)
    # th, im_th = cv2.threshold(im_in, 200, 255, cv2.THRESH_MASK)
    # th, im_th = cv2.threshold(im_in, 200, 255, cv2.THRESH_TOZERO)
    # th, im_th = cv2.threshold(im_in, 200, 255, cv2.THRESH_TOZERO_INV)

    im_floodfill = im_th.copy()

    h, w = im_th.shape[:2]
    mask = np.zeros((h + 2, w + 2), np.uint8)

    cv2.floodFill(im_floodfill, mask, (0, 0), 255)

    im_floodfill_inv = cv2.bitwise_not(im_floodfill)

    im_out = im_th | im_floodfill_inv

    # 中值滤波
    im_out2 = cv2.medianBlur(im_out, 3)

    print(\'save ---\', os.path.basename(f))
    cv2.imwrite(os.path.basename(f), im_out2)
    # print(im_out2.shape)
    # print(im_th.shape)
    dest = str(os.path.basename(f)).replace(\'.png\', \'_im_th.png\')
    print(dest)
    cv2.imwrite(dest, im_th)

    if isShow:
        cv2.imshow(\'Thresholded image\', im_th)
        cv2.imshow(\'Floodfilled iamge\', im_floodfill)
        cv2.imshow(\'Inverted FloodFilled Image\', im_floodfill_inv)
        cv2.imshow(\'Forground\', im_out)
        cv2.imshow(\'MedianBlur\', im_out2)
        cv2.imshow(\'Image\', im_in)

convert(\'/home/simon/Pictures/gold.jpg\', False)

# for i, f in enumerate(os.listdir(in_dir)):
#     if not f.endswith(\'.jpg\'):
#         continue
#     print(i, f)
#     convert(os.path.join(in_dir, f))
#     if i == 100 :
#         break

k = cv2.waitKey(0)
if k == 27:  # 键盘上Esc键的键值
    cv2.destroyAllWindows()

 

  

 

 

 

 

 

--


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
基于MATLAB静态目标分割的药板胶囊检测发布时间:2022-07-18
下一篇:
灰度共生矩阵(GLCM) 及matlab代码实现发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap