CG_图片轮廓mask提取-matlab & python
<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()


--

浙公网安备 33010602011771号