genCOCOMask.m
16 L = length(coco_kpt);
17 %%
18
19 for i = 1:L
20 if mode == 1
21 img_paths = sprintf('images/train2014/COCO_train2014_%012d.jpg', coco_kpt(i).image_id); %sprintf('%012d', 20); ans = 000000000020
22 img_name1 = sprintf('dataset/COCO/mask2014/train2014_mask_all_%012d.png', coco_kpt(i).image_id);
23 img_name2 = sprintf('dataset/COCO/mask2014/train2014_mask_miss_%012d.png', coco_kpt(i).image_id);
24 else
25 img_paths = sprintf('images/val2014/COCO_val2014_%012d.jpg', coco_kpt(i).image_id);
26 img_name1 = sprintf('dataset/COCO/mask2014/val2014_mask_all_%012d.png', coco_kpt(i).image_id);
27 img_name2 = sprintf('dataset/COCO/mask2014/val2014_mask_miss_%012d.png', coco_kpt(i).image_id);
28 end
29
30 try
31 display([num2str(i) '/ ' num2str(L)]);
32 imread(img_name1); %读取失败,将跳转到catch块进行mask的制作
33 imread(img_name2);
34 continue;
35 catch
36 display([num2str(i) '/ ' num2str(L)]); %% num2str:把数值转换成字符串, 转换后可以使用fprintf或disp函数进行输出
37 %joint_all(count).img_paths = RELEASE(i).image_id;
38 [h,w,~] = size(imread(['dataset/COCO/', img_paths])); % h = image.height ; w = image.width
39 mask_all = false(h,w); %创建大小 h×w (与原图像相同)的矩阵,所有的元素为逻辑假,即0,下同
40 mask_miss = false(h,w);
41 flag = 0;
42 for p = 1:length(coco_kpt(i).annorect) % i 为图片的数量, p 为每张图片annorect的维度,即为图片中的人数)
43 %if this person is annotated
44 try
45 seg = coco_kpt(i).annorect(p).segmentation{1}; %分割的结果(验证是否已进行分割)
46 catch
47 %display([num2str(i) ' ' num2str(p)]);
48 mask_crowd = logical(MaskApi.decode( coco_kpt(i).annorect(p).segmentation )); % logical函数: 将括号里的非零值变为1; MaskApi.decode - Decode binary masks encoded via RLE.(Run Length Encoding自行百度). https://blog.csdn.net/chengyq116/article/details/80489439
49 temp = and(mask_all, mask_crowd);
50 mask_crowd = mask_crowd - temp;
51 flag = flag + 1;
52 coco_kpt(i).mask_crowd = mask_crowd;
53 continue;
54 end
55
56 [X,Y] = meshgrid( 1:w, 1:h ); % 用于生成网格矩阵 https://blog.csdn.net/hhhhhyyyyy8/article/details/76209094
57 mask = inpolygon( X, Y, seg(1:2:end), seg(2:2:end)); %inpolygon(x,y,xv,yv)%注意xv,yv构成了多边形边界。x,y对应的是单点坐标,判断是否在多边形内,返回结果为逻辑logical类型(不是数字类型哦),如果在对应的就返回1,否则为0
58 mask_all = or(mask, mask_all); % mask_all之前为全0
59
60 if coco_kpt(i).annorect(p).num_keypoints <= 0 % 如果没有keypoints标注,则标记为mask_miss,取反后未标注处值为1,避免进行惩罚; 若一张图片中每个人的keypoints均有标注,则mask_miss矩阵全为0,然后在Line68中取反,这样所有标注的关节点W(p) = 1;
61 mask_miss = or(mask, mask_miss);
62 end
63 end
-
Line56 : meshgrid 网格
-
Line57 : 利用Line45的分割结果seg生成mask
64 if flag == 1 %注意,此处程序处理完了单张图片中的所有人,进入flag判断
65 mask_miss = not(or(mask_miss,mask_crowd));
66 mask_all = or(mask_all, mask_crowd);
67 else
68 mask_miss = not(mask_miss); %取反
69 end
70
71 coco_kpt(i).mask_all = mask_all;
72 coco_kpt(i).mask_miss = mask_miss;
73
74 if mode == 1
75 img_name = sprintf('dataset/COCO/mask2014/train2014_mask_all_%012d.png', coco_kpt(i).image_id);
76 imwrite(mask_all,img_name);
77 img_name = sprintf('dataset/COCO/mask2014/train2014_mask_miss_%012d.png', coco_kpt(i).image_id);
78 imwrite(mask_miss,img_name);
79 else
80 img_name = sprintf('dataset/COCO/mask2014/val2014_mask_all_%012d.png', coco_kpt(i).image_id);
81 imwrite(mask_all,img_name);
82 img_name = sprintf('dataset/COCO/mask2014/val2014_mask_miss_%012d.png', coco_kpt(i).image_id);
83 imwrite(mask_miss,img_name);
84 end
85
86 if flag == 1 && vis == 1 %用于查看
87 im = imread(['dataset/COCO/', img_paths]);
88 mapIm = mat2im(mask_all, jet(100), [0 1]); %mat2im - convert to rgb image https://ww2.mathworks.cn/matlabcentral/fileexchange/26322-mat2im
89 mapIm = mapIm*0.5 + (single(im)/255)*0.5;
90 figure(1),imshow(mapIm);
91 mapIm = mat2im(mask_miss, jet(100), [0 1]); %jet是颜色图数组 https://ww2.mathworks.cn/help/matlab/ref/jet.html
92 mapIm = mapIm*0.5 + (single(im)/255)*0.5;
93 figure(2),imshow(mapIm);
94 mapIm = mat2im(mask_crowd, jet(100), [0 1]);
95 mapIm = mapIm*0.5 + (single(im)/255)*0.5;
96 figure(3),imshow(mapIm);
97 pause;
98 close all;
99 elseif flag > 1
100 display([num2str(i) ' ' num2str(p)]);
101 end
102 end
103 end
-
Line68 : mask_miss取反
-
Line71 : coco_kpt添加mask_all列