OpenPose訓練過程解析(2)

weixin_34402408發表於2018-08-24

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;
13228477-10927828917b19a4.png
mask_all_false.png
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 網格


    13228477-df0de4120fd30191.png
    meshgrid X.png
13228477-6adf880972dff042.png
meshgrid Y.png
  • Line57 : 利用Line45的分割結果seg生成mask


    13228477-63dbeefbafe060c0.png
    mask邊界.png
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取反


    13228477-38193dd8e1cc3447.png
    mask_miss_afterProcess.png
  • Line71 : coco_kpt新增mask_all列


    13228477-f95d19022e27d7a5.png
    coco_kpt_add_mask_all.png

相關文章