常用又容易忘记的代码

MATLAB中

1.  img_name = sprintf('dataset/COCO/mask2014/val2014_mask_miss_%012d.png', coco_kpt(i).image_id);

2.  从矩阵中通过设定的阀值 thre 快速筛选对象:

eg.  map = rand([ 2 , 5 ])    %随机生成范围是 0~1 的  2 x 5 矩阵

       map( map < 0.5 ) = 0  %小于0.5的值全部设为 0 

3. 找出矩阵中的peak点:

function [X,Y,score] = findPeaks(map, thre)
    %filter = fspecial('gaussian', [3 3], 2);
    %map_smooth = conv2(map, filter, 'same');
    map_smooth = map;
    map_smooth(map_smooth < thre) = 0;
    
    map_aug = -1*zeros(size(map_smooth,1)+2, size(map_smooth,2)+2);
    map_aug1 = map_aug;
    map_aug2 = map_aug;
    map_aug3 = map_aug;
    map_aug4 = map_aug;
    
    map_aug(2:end-1, 2:end-1) = map_smooth;
    map_aug1(2:end-1, 1:end-2) = map_smooth;
    map_aug2(2:end-1, 3:end) = map_smooth;
    map_aug3(1:end-2, 2:end-1) = map_smooth;
    map_aug4(3:end, 2:end-1) = map_smooth;
    
    peakMap = (map_aug > map_aug1) & (map_aug > map_aug2) & (map_aug > map_aug3) & (map_aug > map_aug4);
    peakMap = peakMap(2:end-1, 2:end-1);
    [X,Y] = find(peakMap);
    score = zeros(length(X),1);
    for i = 1:length(X)
        score(i) = map(X(i),Y(i));
    end
    
    if isempty(X)
        return;
    end
    
    deleIdx = [];
    flag = ones(1, length(X));
    for i = 1:length(X)
        if(flag(i)>0)
            for j = (i+1):length(X)
                if norm([X(i)-X(j),Y(i)-Y(j)]) <= 6
                    flag(j) = 0;
                    deleIdx = [deleIdx;j];
                end
            end
        end
    end
    X(deleIdx,:) = [];
    Y(deleIdx,:) = [];
    score(deleIdx,:) = [];
end

4.根据某一列的值对行进行排序(经常碰到score)

temp = sortrows(temp,-3); %based on connection score  从第三列开始降序排列

5. 在图片中加入一点并显示出来

image = insertShape(image, 'FilledCircle', [X Y 5], 'Color', joint_color(i,:));

 6. 矩阵(图像转置)

featureMaps = permute(feature, [2 1 3]);

python中

1.截取文件路径:

f = open('ucf101_split1_testVideos.txt', 'r')
f_lines = f.readlines()
f.close()

video_dict = {}
current_line = 0
video_order = []
for ix, line in enumerate(f_lines):
    video = line.strip().split(' ')[0].split('/')[1]
    l = int(line.split(' ')[1])

 2.解析.mat文件:

mat文件原型:  heatmap(cell 60x151)  --> heatmap{1,1} 是 1x1 struct --> Field: 'name' || 'score' || 'num_frames'   其中 score 是 46x82x44 的矩阵 范围 0 ~ 1

解析:

import scipy.io as scio
dataFile = '/home/cc/dataset/MultiPerson_PoseTrack_v0.1/mat/heatmap.mat'
data = scio.loadmat(dataFile)
data.keys()
data['heatmap'][0,0]['name']
data['heatmap'][0,0]['score'].shape
(output: 1x1)
score = data['heatmap'][0,0]['score'][0,0]   #前一个可改为[i,j],最后的[0,0]是固定的
score.shape
(output: 46*82*44)
原文地址:https://www.cnblogs.com/caffeaoto/p/8018010.html