Matlab 进阶学习记录
Matlab 进阶学习记录
Error: Invalid MEX-file '/media/wangxiao/Acer/dataset/LDES/utility/mexfiles/mpolar.mexa64': /usr/local/MATLAB/R2017a/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4).
$: sudo LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6 ./glnxa64/MATLAB
1. conf_proposal = proposal_config('image_means', model.mean_image, 'feat_stride', model.feat_stride);
function conf = proposal_config(varargin)
% conf = proposal_config(varargin)
% --------------------------------------------------------
% Faster R-CNN
% Copyright (c) 2015, Shaoqing Ren
% Licensed under The MIT License [see LICENSE for details]
% --------------------------------------------------------
ip = inputParser ;
%% training
ip.addParamValue('use_gpu', gpuDeviceCount > 0, ...
@islogical);
% whether drop the anchors that has edges outside of the image boundary
ip.addParamValue('drop_boxes_runoff_image', ...
true, @islogical);
% Image scales -- the short edge of input image
ip.addParamValue('scales', 600, @ismatrix);
% Max pixel size of a scaled input image
ip.addParamValue('max_size', 1000, @isscalar);
% Images per batch, only supports ims_per_batch = 1 currently
ip.addParamValue('ims_per_batch', 1, @isscalar);
% Minibatch size
ip.addParamValue('batch_size', 256, @isscalar);
% Fraction of minibatch that is foreground labeled (class > 0)
ip.addParamValue('fg_fraction', 0.5, @isscalar);
% weight of background samples, when weight of foreground samples is
% 1.0
ip.addParamValue('bg_weight', 1.0, @isscalar);
% Overlap threshold for a ROI to be considered foreground (if >= fg_thresh)
ip.addParamValue('fg_thresh', 0.7, @isscalar);
% Overlap threshold for a ROI to be considered background (class = 0 if
% overlap in [bg_thresh_lo, bg_thresh_hi))
ip.addParamValue('bg_thresh_hi', 0.3, @isscalar);
ip.addParamValue('bg_thresh_lo', 0, @isscalar);
% mean image, in RGB order
ip.addParamValue('image_means', 128, @ismatrix);
% Use horizontally-flipped images during training ?
ip.addParamValue('use_flipped', true, @islogical);
% Stride in input image pixels at ROI pooling level (network specific)
% 16 is true for {Alex,Caffe}Net, VGG_CNN_M_1024, and VGG16
ip.addParamValue('feat_stride', 16, @isscalar);
% train proposal target only to labled ground-truths or also include
% other proposal results (selective search, etc.)
ip.addParamValue('target_only_gt', true, @islogical);
% random seed
ip.addParamValue('rng_seed', 6, @isscalar);
%% testing
ip.addParamValue('test_scales', 600, @isscalar);
ip.addParamValue('test_max_size', 1000, @isscalar);
ip.addParamValue('test_nms', 0.3, @isscalar);
ip.addParamValue('test_binary', false, @islogical);
ip.addParamValue('test_min_box_size',16, @isscalar);
ip.addParamValue('test_drop_boxes_runoff_image', ...
false, @islogical);
ip.parse(varargin{:});
conf = ip.Results;
assert(conf.ims_per_batch == 1, 'currently rpn only supports ims_per_batch == 1');
% if image_means is a file, load it...
if ischar(conf.image_means)
s = load(conf.image_means);
s_fieldnames = fieldnames(s);
assert(length(s_fieldnames) == 1);
conf.image_means = s.(s_fieldnames{1});
end
end
The inputParser
object allows you to manage inputs to a function by creating an input scheme. To check the input, you can define validation functions for required arguments, optional arguments, and name-value pair arguments. Optionally, you can set properties to adjust the parsing behavior, such as handling case sensitivity, structure array inputs, and inputs that are not in the input scheme.
After calling the parse
method to parse the inputs, the inputParser
saves names and values of inputs that match the input scheme (stored in Results
), names of inputs that are not passed to the function and, therefore, are assigned default values (stored in UsingDefaults
), and names and values of inputs that do not match the input scheme (stored in Unmatched
).
Check the validity of required and optional function inputs.
Create a custom function with required and optional inputs in the file findArea.m.
function a = findArea(width,varargin)
p = inputParser;
defaultHeight = 1;
defaultUnits = 'inches';
defaultShape = 'rectangle';
expectedShapes = {'square','rectangle','parallelogram'};
addRequired(p,'width',@isnumeric);
addOptional(p,'height',defaultHeight,@isnumeric);
addParameter(p,'units',defaultUnits);
addParameter(p,'shape',defaultShape,...
@(x) any(validatestring(x,expectedShapes)));
parse(p,width,varargin{:});
a = p.Results.width .* p.Results.height;
The input parser checks whether width and height are numeric, and whether the shape matches a string in cell array expectedShapes. @ indicates a function handle, and the syntax @(x) creates an anonymous function with input x.
Call the function with inputs that do not match the scheme. For example, specify a nonnumeric value for the width input:
findArea('text')
Error using findArea (line 14)
The value of 'width' is invalid. It must satisfy the function: isnumeric.
Specify an unsupported value for shape:
findArea(4,'shape','circle')
Error using findArea (line 14)
The value of 'shape' is invalid. Expected input to match one of these strings:
square, rectangle, parallelogram
The input, ''circle'', did not match any of the valid strings.
http://www.cnblogs.com/heleifz/p/matlab-function-handle.html
2. assert 语句的使用:
assert: Generate an error when a condition is violated.
assert(EXPRESSION, ERRMSG) evaluates EXPRESSION and, if it is false, displays the string contained in ERRMSG. When ERRMSG is the last input to assert, MATLAB displays it literally, without performing any substitutions on the characters in ERRMSG.
例如:如果 contion 不成立,则会输出对应的:提示错误信息。
assert(mod(conf.batch_size, num_images) == 0, ...
sprintf('num_images %d must divide BATCH_SIZE %d', num_images, conf.batch_size));
3. permute 函数:
Permute array dimensions.
B = permute(A,ORDER) rearranges the dimensions of A so that they
are in the order specified by the vector ORDER.
重新安排矩阵的x,y,z , 在二维中就相当于把x,y 对换,在三维中相当于可以把三个坐标的位置互换。
比如A =
A(:,:,1)=repmat(1,3,3);
A(:,:,2)=repmat(2,3,3);
A(:,:,3)=repmat(3,3,3);
disp(A);
A(:,:,1) =
1 1 1
1 1 1
1 1 1
A(:,:,2) =
2 2 2
2 2 2
2 2 2
A(:,:,3) =
3 3 3
3 3 3
3 3 3
At = permute(A,[3,2,1]);
disp(At);
At(:,:,1) =
1 1 1
2 2 2
3 3 3
At(:,:,2) =
1 1 1
2 2 2
3 3 3
At(:,:,3) =
1 1 1
2 2 2
3 3 3
permute(A,[3,2,1])
4. cellfun 函数:
cellfun: Apply a function to each cell of a cell array. A = cellfun(FUN, C) applies the function specified by FUN to the contents of each cell of cell array C, and returns the results in the array A.
5. 从列表 A 中去搜索列表 B 中是否存在有相交元素,即:求 A and B 的差。
select = importdata('/home/wangxiao/Documents/Sun-80-dataset/VGG_16/iter_1/SUN80_50%_selected_without_HD.txt');
Unlabel = importdata('/home/wangxiao/Documents/Sun-80-dataset/iter_1/Sun_100_UnLabel_Train_0.5_.txt');
fid = fopen('/home/wangxiao/Documents/Sun-80-dataset/VGG_16/iter_1/SUN80_50%_Unselected_data.txt', 'a') ;
selected_list = [] ;
unselected_list = [] ;
for i = 1:size(Unlabel.data, 1)
disp(['deal with: ', num2str(i) , '/' , num2str(size(Unlabel.data, 1))]) ;
unlabel_name = Unlabel.textdata{i, 1}; % Unlabel image name
unlabel_label = Unlabel.data(i, 1) ; % Unlabel image label
count = 0;
for j = 1:size(select.textdata, 1)
select_name = select.textdata{j, 1}; % selected image name
if strcmp(unlabel_name, select_name) % if have selected, jump it.
selected_list = [selected_list; unlabel_name];
% break;
else
count = count + 1;
end
if count == size(select.textdata, 1)
fprintf(fid, '%s ', num2str(unlabel_name));
fprintf(fid, '%s \n', num2str(unlabel_label));
end
end
end
6. containers.Map() 的用法
matlab中的containers.Map()有点类似于C++ STL中的map容器,具有key/value映射的功能.
num = containers.Map({1, 2, 3}, {'one', 'two', 'three'})
myMap = containers.Map(KEYS, VALUES) constructs a Map object myMap that contains one or more keys and a value for each of these keys, as specified in the KEYS and VALUES arguments.
例如:从 Map 上提取一个值:myValue = myMap(key) ;
修改键值对 (key-values pairs): myMap(key) = newValue ;
增加一个新的键值对:myMap(key) = newValue ;
可以通过 remove 的方法将 values 删除掉。
7. try catch end 机制:
该机制可以防止由于程序中可能出现的错误而终止运行的情况:
try
ld = load(anchor_cache_file) ;
anchors = ld.anchors ;
catch
base_anchor = [1, 1, opts.base_size, opts.base_size] ;
ratio_anchors = ratio_jitter(base_anchor, opts.ratios) ;
end
8. About change the gray image into 3 channel RGB image:
1 clc; close all; clear all;
2 image = imread('/home/wangxiao/Documents/mnist_dataset/mnist_0_.png');
3 image = im2double(image);
4 image = double(image);
5 width = size(image, 1); height = size(image, 2);
6
7 synthetic = zeros([224, 224]);
8
9 for i = 1:size(image, 1)
10 for j = 1:size(image, 2)
11
12 synthetic(i, j) = image(i, j);
13 synthetic(i, j) = image(i, j);
14 synthetic(i, j) = image(i, j);
15 end
16 end
17 synthetic = im2uint8(synthetic);
18 imshow(synthetic);
19 figure; imshow(image);
20
21 %%
22 synthetic2 = zeros([224, 224, 3]);
23
24 for i = 1:size(image, 1)
25 for j = 1:size(image, 2)
26
27 synthetic2(i, j, 1) = image(i, j, 1);
28 synthetic2(i, j, 2) = image(i, j, 2);
29 synthetic2(i, j, 3) = image(i, j, 3);
30 end
31 end
32 synthetic2 = im2uint8(synthetic2);
33 imshow(synthetic2);
synthetic is a single channel image, and synthetic2 is a three channel image.
Another Solution is:
% if grayscale repeat one channel to match filters size
if(size(im, 3)==1)
im = repmat(im, [1 1 3]);
end
9. Divided the image into specific patches using matlab function: mat2cell
This is a really cool function. For example, you read one image and divide it into 3*3 = 9 patches, and we assume the resolution of the image is: 100*100, you just need set the vectors M = [20, 30, 50]; N = [20, 20, 60] ;
Actually, as long as the sum of three values you set equal to 100 (here is 20, 30, 50), it will be ok. The other vector N have the same reason.
10. Read images from disks and save these frames into avi video files.
1 %% change the frame to videos to save.
2 clc; close all; clear all;
3 path = '/home/wangxiao/Downloads/files/Visual_Tracking/MDNet-CVPR2016/saved_tracking_results_MDNet_OTB100/Biker/';
4 files = dir([path, '*.png']);
5 count = 0;
6
7 for i=1:size(files, 1)
8 xxx = strtok(files(i).name, 'M');
9 name = xxx(7:end-1);
10 image = imread([path, files(i).name]);
11 index = sprintf('%04d', str2double(name));
12 newName = [ index,'.jpg'];
13
14 % a = sprintf('%04d',i);
15
16 imwrite(image, [path, newName]);
17 end
18
19 disp('==>> deal with image done !')
20
21
22 jpgFiles = dir([path, '*.jpg']);
23 videoName = '/home/wangxiao/Videos/Biker_MDNet_OTB100.avi';
24 fps = 25; %帧率
25 startFrame = 1; %从哪一帧开始
26 endFrame = size(jpgFiles, 1); %哪一帧结束
27
28 %生成视频的参数设定
29 aviobj=VideoWriter(videoName); %创建一个avi视频文件对象,开始时其为空
30 aviobj.FrameRate=fps;
31
32 open(aviobj);%Open file for writing video data
33
34 for i=startFrame:endFrame
35 frames = imread([path, jpgFiles(i).name]);
36 frames = im2frame(frames);
37 writeVideo(aviobj, frames);
38 end
39 close(aviobj);
40
41 disp('==>> saved the video !')
11. Matlab中save实现保存数据到mat文件的正确使用 参考:http://blog.csdn.net/fx677588/article/details/52836348
1 1. 普通保存在当前文件夹下
2
3 save matPath.mat A B; % A B都是生成的数据矩阵
4
5 需要注意这种方式只能将数据保存在当前文件夹下的第一个参数文件中,下面这样写并不能将数据保存到你想要的文件夹中的。
6
7 saldir = './result/';
8 savePath = [saldir imnames(len).name(1:end-4) '_KSD'];
9 save savePath A;
10
11 上面程序也只能实现在当前文件夹下生成savePath.mat文件,然后数据保存到该文件中。并不能保存到需要的文件夹中。正确的写法是下面的方式。
12
13 2. 保留数据到其他文件夹下
14
15 saldir = './result/';
16 savePath = [saldir imnames(len).name(1:end-4) '_KSD' '.mat'];
17 save(savePath,'A'); % 保存到其他文件夹的写法
18
19 这里也需要注意,保存的数据矩阵,即save函数的第二个参数不可以忘记单引号。
12. 根据 attention maps 置信度的高低,生成对应的 bounding box :
clc;close all;clear all;
Img=imread('/home/wangxiao/Documents/files/Visual_Tracking/MDNet-CVPR2016/MDNet-master/attentionMap/Basketball/0001.png');
if ndims(Img)==3
I=rgb2gray(Img);
else
I=Img;
end
I=im2bw(I,graythresh(I));
[m,n]=size(I);
imshow(I);title('binary image');
txt=get(gca,'Title');
set(txt,'fontsize',16);
L=bwlabel(I);
stats=regionprops(L,'all');
set(gcf,'color','w');
set(gca,'units','pixels','Visible','off');
q=get(gca,'position');
q(1)=0;%设置左边距离值为零
q(2)=0;%设置右边距离值为零
set(gca,'position',q);
for i=1:length(stats)
hold on;
rectangle('position',stats(i).BoundingBox,'edgecolor','y','linewidth',2);
temp = stats(i).Centroid;
plot(temp(1),temp(2),'r.');
drawnow;
end
frame=getframe(gcf,[0,0,n,m]);
im=frame2im(frame);
imwrite(im,'a.jpg','jpg');%可以修改保存的格式
Python Implementation: (from: https://www.jianshu.com/p/7693222523c0 )
import numpy as np import scipy.ndimage as ndi from skimage import measure,color import matplotlib.pyplot as plt #编写一个函数来生成原始二值图像 def microstructure(l=256): n = 5 x, y = np.ogrid[0:l, 0:l] #生成网络 mask = np.zeros((l, l)) generator = np.random.RandomState(1) #随机数种子 points = l * generator.rand(2, n**2) mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1 mask = ndi.gaussian_filter(mask, sigma=l/(4.*n)) #高斯滤波 return mask > mask.mean() data = microstructure(l=128)*1 #生成测试图片 labels=measure.label(data,connectivity=2) #8连通区域标记 dst=color.label2rgb(labels) #根据不同的标记显示不同的颜色 print('regions number:',labels.max()+1) #显示连通区域块数(从0开始标记) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4)) ax1.imshow(data, plt.cm.gray, interpolation='nearest') ax1.axis('off') ax2.imshow(dst,interpolation='nearest') ax2.axis('off') fig.tight_layout() plt.show()
如果想分别对每一个连通区域进行操作,比如计算面积、外接矩形、凸包面积等,则需要调用measure子模块的regionprops()函数。该函数格式为:
skimage.measure.regionprops(label_image)
返回所有连通区块的属性列表,常用的属性列表如下表:
属性名称 类型 描述
area int 区域内像素点总数
bbox tuple 边界外接框(min_row, min_col, max_row, max_col)
centroid array 质心坐标
convex_area int 凸包内像素点总数
convex_image ndarray 和边界外接框同大小的凸包
coords ndarray 区域内像素点坐标
Eccentricity float 离心率
equivalent_diameter float 和区域面积相同的圆的直径
euler_number int 区域欧拉数
extent float 区域面积和边界外接框面积的比率
filled_area int 区域和外接框之间填充的像素点总数
perimeter float 区域周长
label int 区域标记
13. 将 video 切割为 frame:
%% Input 2 videos and divide it into frames
clc; clear all; close all;
infraredvideo = 'C:\Users\王逍\Desktop\跟踪数据集\videos\';
savePath = 'C:\Users\王逍\Desktop\跟踪数据集\frames\';
% devide the infrared video into infrared images
videoList1=dir(fullfile(infraredvideo,'*.mp4'));
video_num=length(videoList1);
for j=1:video_num
infraredOutPath = [savePath, videoList1(j).name, '\'];
mkdir(infraredOutPath);
frames = VideoReader([strcat(infraredvideo,videoList1(j).name)]);
numFrames =frames.NumberOfFrames;
for k = 1 : numFrames
disp(['==>> processing video ',num2str(k),' frames, please waiting....']);
frame = read(frames,k);
frame = imresize(frame, [480, 640]);
% figure(1); imshow(frame);
imwrite(frame, [infraredOutPath, sprintf('%08d.png',k)]);
end
end
14. divide the total attention maps according to given video frames such as TC128.
1 %%
2 clc; close all; clear all;
3 path = '/media/wangxiao/E1F171026416B63F/tracking_benchmark/Temple-color-128/predicted_attentionMaps-v1/';
4 attentionfiles = dir([path, '*.png']);
5
6 videoPath = '/media/wangxiao/E1F171026416B63F/tracking_benchmark/Temple-color-128/videos/';
7 videoFiles = dir(videoPath);
8 videoFiles = videoFiles(3:end);
9 total = 0;
10 savepath = '/media/wangxiao/E1F171026416B63F/tracking_benchmark/Temple-color-128/attentionMaps_per_video-v1/';
11
12
13 for i=1:size(videoFiles, 1)
14 numCount = 0;
15 videoName = videoFiles(i).name;
16 newVideoPath = [videoPath videoName '/img/'];
17
18 videoframes = dir([newVideoPath, '*.jpg']);
19
20 savePath = [savepath videoName '/'];
21 mkdir(savePath);
22
23 disp(['==>> deal with video file: ', num2str(i)]);
24 % total = total + size(videoframes, 1);
25
26 for j=1:size(videoframes, 1)
27 if numCount < size(videoframes, 1)
28 total = total + 1;
29 numCount = numCount + 1;
30 img = imread([path attentionfiles(total).name]);
31 % figure(1); imshow(img);
32
33 temp = sprintf('%04d', numCount);
34 saveName = [temp '.png'];
35 imwrite(img, [savePath saveName]);
36 else
37 break;
38 end
39
40
41
42 end
43
44
45
46 end
47
15. count the image list into txt files.
1 %%
2 market1501_path = '/home/wangxiao/Downloads/person-ReID/open-reid/examples/data/market1501/';
3 txtsavePath = '/home/wangxiao/Downloads/person-ReID/open-reid/examples/data/market1501/image_txt_list/';
4
5 bounding_box_test = [market1501_path 'bounding_box_test/'];
6 bounding_box_train = [market1501_path 'bounding_box_train/'];
7 gt_bbox = [market1501_path 'gt_bbox/'];
8 gt_query = [market1501_path 'gt_query/'];
9 images = [market1501_path 'images/'];
10 query = [market1501_path 'query/'];
11
12
13 % #####################
14 bounding_box_test_files = dir([bounding_box_test, '*.jpg']);
15 bounding_box_train_files = dir([bounding_box_train, '*.jpg']);
16 gt_bbox_files = dir([gt_bbox, '*.jpg']);
17 gt_query_files = dir([gt_query, '*.jpg']);
18 images_files = dir([images, '*.jpg']);
19 query_files = dir([query, '*.jpg']);
20
21 %% image files I
22 fid = fopen([txtsavePath 'bounding_box_test_files_image_list.txt'], 'w');
23 for i=1:size(bounding_box_test_files, 1)
24 imgName = bounding_box_test_files(i).name;
25 fprintf(fid, '%s \n', imgName);
26 end
27 fclose(fid);
28 disp('==>> done I');
29
30 %% image files II
31 fid = fopen([txtsavePath 'bounding_box_train_files_image_list.txt'], 'w');
32 for i=1:size(bounding_box_train_files, 1)
33 imgName = bounding_box_train_files(i).name;
34 fprintf(fid, '%s \n', imgName);
35 end
36 fclose(fid);
37 disp('==>> done II');
38
39 %% image files III
40 fid = fopen([txtsavePath 'gt_bbox_files_image_list.txt'], 'w');
41 for i=1:size(gt_bbox_files, 1)
42 imgName = gt_bbox_files(i).name;
43 fprintf(fid, '%s \n', imgName);
44 end
45 fclose(fid);
46 disp('==>> done III');
47
48
49 %% image files IV
50 fid = fopen([txtsavePath 'gt_query_files_image_list.txt'], 'w');
51 for i=1:size(gt_query_files, 1)
52 imgName = gt_query_files(i).name;
53 fprintf(fid, '%s \n', imgName);
54 end
55 fclose(fid);
56 disp('==>> done IV');
57
58
59 %% image files V
60 fid = fopen([txtsavePath 'images_files_image_list.txt'], 'w');
61 for i=1:size(images_files, 1)
62 imgName = images_files(i).name;
63 fprintf(fid, '%s \n', imgName);
64 end
65 fclose(fid);
66 disp('==>> done V');
67
68
69
70
71 %% image files VI
72 fid = fopen([txtsavePath 'query_files_image_list.txt'], 'w');
73 for i=1:size(query_files, 1)
74 imgName = query_files(i).name;
75 fprintf(fid, '%s \n', imgName);
76 end
77 fclose(fid);
78 disp('==>> done VI');
16. load json files using matlab code.
this used package from: http://blog.csdn.net/sophia_xw/article/details/70141208
unzip this file and add path to matlab like this:
clear all; clc
addpath('/home/wangxiao/jsonlab-1.5/jsonlab-1.5');
fname='results.json';
jsonData=loadjson(fname);
17. 伪彩色图像的生成:
%% clc; close all; clear all; path = '\MotorRolling\'; files = dir([path, '*.png']); savePath = '\fakeColor_MotorRolling\'; ori_img_path = '\Benchmark\MotorRolling\img\'; ori_img = imread([ori_img_path, '0001.jpg']); oriFiles = dir([ori_img_path, '*.jpg']); height = size(ori_img, 1); width = size(ori_img, 2); for img_idex =1:length(files) im = imread([path, files(img_idex).name]); originalImage = imread([ori_img_path, oriFiles(img_idex).name]); % for idx =1:size(im, 1) % for jdx = 1:size(im, 2) % if im(idx, jdx) <= 30; % im(idx, jdx) = 0; % end % end % end I=double(im); [m,n]=size(I); L=256; for i=1:m for j=1:n if I(i,j)<=L/4 R(i,j)=0; G(i,j)=4*I(i,j); B(i,j)=L; else if I(i,j)<=L/2 R(i,j)=0; G(i,j)=L; B(i,j)=-4*I(i,j)+2*L; else if I(i,j)<=3*L/4 R(i,j)=4*I(i,j)-2*L; G(i,j)=L; B(i,j)=0; else R(i,j)=L; G(i,j)=-4*I(i,j)+4*L; B(i,j)=0; end end end end end for i=1:m for j=1:n rgbim(i,j,1)=R(i,j); rgbim(i,j,2)=G(i,j); rgbim(i,j,3)=B(i,j); end end rgbim=rgbim/256; % figure; % subplot(1,2,1); % imshow(gray); % subplot(1,2,2); fake_color_img = imresize(rgbim, [height, width]); imshow(fake_color_img); imwrite(fake_color_img, [savePath, oriFiles(img_idex).name]); img_idex end
18. frame to video transformation
%% clc; close all; clear all; videoPath='\demo_videos\demo_Skater2\'; salVideo = '\Benchmark\Skater2\img\'; resultsPath = '\saved_figure_tracking_results\Skater2\v\'; file=dir([videoPath,'*.jpg']); file_=dir([salVideo,'*.jpg']); resultsFiles = dir([resultsPath, '*.jpg']); writerObject = VideoWriter('OUR_Skater2.avi'); writerObject.FrameRate =25; %writerObject.Quality = 50; open( writerObject ); Num= length(file); for k =1 : Num trackingResults = imread([resultsPath, resultsFiles(k).name]); disp(['processing the ',num2str(k),'/',num2str(Num),' frames, please waiting....']); frame_1 = imread([videoPath,file(k).name]); frame_222 = imread([salVideo,file_(k).name]); frame_2 = zeros(size(frame_222, 1), size(frame_222, 2), 3); if size(frame_222, 3) == 1 frame_2(:,:,1) = frame_222; frame_2(:,:,1) = frame_222; frame_2(:,:,1) = frame_222; end frame_2 = uint8(frame_2); frame_3 = frame_1*0.5+frame_2*0.5; trackingResults = imresize(trackingResults, [size(frame_2, 1), size(frame_2, 2)]); fullframe = [ frame_2, frame_3, trackingResults ]; writeVideo( writerObject, fullframe ); end close( writerObject );
19. video to frame transformation:
clear all;close all; %% video videoPath='.\2\'; outPath='.\2\'; T=[1.72844 0.0692766 -293 -0.0692766 1.72844 -94 0 0 1]; tform = maketform('affine', T'); videoList=dir(fullfile(videoPath,'*.avi')); video_num=length(videoList); for j=1:video_num frames = VideoReader (strcat(videoPath,videoList(j).name)); numFrames =frames.NumberOfFrames; for k = 1 : numFrames disp(['processing the ',num2str(k),' frames, please waiting....']); frame = read(frames,k); transformFrame = imtransform(frame,tform,'XData',[1 320], 'YData',[1 240]); % frame=imresize(frame,[288,384]); imwrite(transformFrame,[outPath,sprintf('%08d.png',k)]); end end
20. Matlab load json files and save it into txt file:
%%
clc; close all; clear all; warning off;
addpath('E:\Matlab2018\jsonlab-1.5\jsonlab-1.5');
oriVideoPath = 'I:\Tracking-Dataset\Video_Labeled_set\';
savePath = 'E:\dataset_processsed_data\';
videoFiles = dir(oriVideoPath);
videoFiles = videoFiles(3:end);
for videoIDX =1:size(videoFiles, 1)
videoName = videoFiles(videoIDX).name;
videoPath = [oriVideoPath videoName '\'];
video_imgPath = [videoPath '*.png'];
video_imgFiles = dir(video_imgPath);
saveImage_path = [savePath videoName '\imgs\'];
mkdir(saveImage_path);
anno_files = dir(videoPath);
anno_name = anno_files( length(anno_files)).name;
anno_path = [videoPath anno_name '\'];
annoFiles = dir(anno_path);
annoFiles = annoFiles(3:end);
gt_txtFiles = fopen([savePath videoName '\groundtruth.txt'], 'w');
for imgIDX =1:size(video_imgFiles, 1)
disp(['==>> video ', num2str(videoIDX), '\', num2str(size(videoFiles, 1)), ' img ', num2str(imgIDX), '\', num2str(size(video_imgFiles, 1))]);
image = imread([videoPath video_imgFiles(imgIDX).name]);
imageIndex = sprintf('%05d', imgIDX);
newImgName = [num2str(imageIndex) '.png'] ;
imwrite(image, [saveImage_path newImgName]);
jsonFIle_path = [anno_path annoFiles(imgIDX).name];
json2data = loadjson(jsonFIle_path);
BBox = json2data.outputs.object{1, 1}.bndbox;
x1 = BBox.xmin;
y1 = BBox.ymin;
w = BBox.xmax - BBox.xmin;
h = BBox.ymax - BBox.ymin;
fprintf(gt_txtFiles, '%s', num2str(x1));
fprintf(gt_txtFiles, ',');
fprintf(gt_txtFiles, '%s', num2str(y1));
fprintf(gt_txtFiles, ',');
fprintf(gt_txtFiles, '%s', num2str(w));
fprintf(gt_txtFiles, ',');
fprintf(gt_txtFiles, '%s', num2str(h));
fprintf(gt_txtFiles, '\n');
end
fclose(gt_txtFiles);
end
21. Rename the txt files:
%% clc; clear all; close all; track_results_path = '/home/wangxiao/Downloads/tracking_results/dimp50/'; resultsFiles = dir(track_results_path); resultsFiles = resultsFiles(3:end); newSavePath = '/home/wangxiao/Downloads/tracking_results/renamed_trackingResults/'; for i=1:size(resultsFiles, 1) i videoName = resultsFiles(i).name; newVideoSavePath = [newSavePath videoName '/']; mkdir(newVideoSavePath); videoPath = [track_results_path videoName '/' videoName '.txt']; results = importdata(videoPath); videotimePath = [track_results_path videoName '/' videoName '_time.txt']; timeresults = importdata(videotimePath); new_txtName = [videoName '.txt']; fid = fopen([newVideoSavePath new_txtName], 'w'); new_timetxtName = [videoName '_time.txt']; fid2 = fopen([newVideoSavePath new_timetxtName], 'w'); for ii=1:size(results, 1) fprintf(fid, '%s', num2str(results(ii, 1))); fprintf(fid, '%s', ','); fprintf(fid, '%s', num2str(results(ii, 2))); fprintf(fid, '%s', ','); fprintf(fid, '%s', num2str(results(ii, 3))); fprintf(fid, '%s', ','); fprintf(fid, '%s\n', num2str(results(ii, 4))); fprintf(fid2, '%s\n', '1.0'); end fclose(fid); fclose(fid2); end
22. prcc reid dataset processing:
% %% % % +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % % Processing the train and val set. % % +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % clc; clear all; close all; warning off; % % path = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID\prcc_raw/rgb/train/'; % folders = dir(path); % folders = folders(3:end); % newSavePath = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID/prcc_processed/rgb/train/'; % mkdir(newSavePath); % % for index=1:size(folders, 1) % videoName = folders(index).name; % new_path = [path videoName '/']; % imgFiles = dir([new_path, '*.jpg']); % % personID = str2num(videoName); % % for imgIndex =1:size(imgFiles, 1) % imageName = imgFiles(imgIndex).name; % image = imread([new_path, imageName]); % % if(size(image, 3)==1) % image = repmat(image, [1 1 3]); % end % % if imageName(1) == 'A', camID = 0; end % if imageName(1) == 'B', camID = 1; end % if imageName(1) == 'C', camID = 2; end % % newImageName = [num2str(index) '_' num2str(imgIndex) '_' num2str(camID) '.jpg']; % % imwrite(image, [newSavePath, newImageName]); % % disp(['videoIndex: ', num2str(index), ' ', 'imageIndex: ', num2str(imgIndex)]); % % end % % end % % +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % % Processing the test set. % % +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % clc; clear all; close all; warning off; % % rgb sketch % path = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID\prcc_raw/rgb/test/'; % folders = dir(path); % folders = folders(3:end); % newSavePath = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID/prcc_processed/rgb/test/'; % mkdir(newSavePath); % % for index=1:size(folders, 1) % videoName = folders(index).name; % new_path = [path videoName '/']; % videoFiles = dir(new_path); % videoFiles = videoFiles(3:end); % % for videoIdx = 1:size(videoFiles, 1) % img_path = [path videoName '/' videoFiles(videoIdx).name '/']; % imgFiles = dir([img_path, '*.jpg']); % personID = str2num(videoFiles(videoIdx).name); % % for imgIndex =1:size(imgFiles, 1) % imageName = imgFiles(imgIndex).name; % image = imread([img_path, imageName]); % % if(size(image, 3)==1) % image = repmat(image, [1 1 3]); % end % % newImageName = [num2str(videoIdx) '_' num2str(imgIndex) '_' num2str(videoName) '.jpg']; % % imwrite(image, [newSavePath, newImageName]); % % disp(['videoIndex: ', num2str(index), ' ', 'imageIndex: ', num2str(imgIndex)]); % end % end % end %% % +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % Processing the query and gallery set % +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ clc; clear all; close all; warning off; sketch_path = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID\prcc_raw/sketch/test/'; sketchfolders = dir(sketch_path); sketchfolders = sketchfolders(3:end); new_sketchquery_SavePath = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID\prcc_processed/sketch/query/'; new_sketchgallery_SavePath = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID\prcc_processed/sketch/gallery/'; mkdir(new_sketchquery_SavePath); mkdir(new_sketchgallery_SavePath); rgb_path = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID\prcc_raw/rgb/test/'; rgbfolders = dir(rgb_path); rgbfolders = rgbfolders(3:end); new_rgbquery_SavePath = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID\prcc_processed/rgb/query/'; new_rgbgallery_SavePath = 'D:\My works\cvpr2021_video_based_Cloth_Change_reID\prcc_processed/rgb/gallery/'; mkdir(new_rgbquery_SavePath); mkdir(new_rgbgallery_SavePath); for index=1:size(sketchfolders, 1) videoName = sketchfolders(index).name; sketchnew_path = [sketch_path videoName '/']; sketchvideoFiles = dir(sketchnew_path); sketchvideoFiles = sketchvideoFiles(3:end); rgbnew_path = [rgb_path videoName '/']; rgbvideoFiles = dir(rgbnew_path); rgbvideoFiles = rgbvideoFiles(3:end); for videoIdx = 1:size(sketchvideoFiles, 1) sketchimg_path = [sketchnew_path sketchvideoFiles(videoIdx).name '/']; sketchimgFiles = dir([sketchimg_path, '*.jpg']); rgbimg_path = [rgbnew_path rgbvideoFiles(videoIdx).name '/']; rgbimgFiles = dir([rgbimg_path, '*.jpg']); personID = str2num(sketchvideoFiles(videoIdx).name); shuffleIDX = randperm(size(sketchimgFiles, 1)); queryNUM = floor(size(sketchimgFiles, 1) * 0.3); for imgIndex =1:size(sketchimgFiles, 1) %% for sketch image sketchimageName = sketchimgFiles(shuffleIDX(1, imgIndex)).name; sketchimage = imread([sketchimg_path, sketchimageName]); if size(sketchimage, 3) ==1 sketchimage = repmat(sketchimage, [1 1 3]); end newsketchimageImageName = [num2str(videoIdx) '_' num2str(imgIndex) '_' num2str(videoName) '.jpg']; if imgIndex <= queryNUM imwrite(sketchimage, [new_sketchquery_SavePath, newsketchimageImageName]); else imwrite(sketchimage, [new_sketchgallery_SavePath, newsketchimageImageName]); end %% for rgb image rgbimageName = rgbimgFiles(shuffleIDX(1, imgIndex)).name; rgbimage = imread([rgbimg_path, rgbimageName]); if size(rgbimage, 3)==1 rgbimage = repmat(rgbimage, [1 1 3]); end newrgbimageImageName = [num2str(videoIdx) '_' num2str(imgIndex) '_' num2str(videoName) '.jpg']; if imgIndex <= queryNUM imwrite(rgbimage, [new_rgbquery_SavePath, newrgbimageImageName]); else imwrite(rgbimage, [new_rgbgallery_SavePath, newrgbimageImageName]); end % imshow(rgbimage); imshow(sketchimage); disp(['videoIndex: ', num2str(index), ' ', 'imageIndex: ', num2str(imgIndex)]); end end end
Matlab 读取点我科技数据标注结果
%% clc; close all; clear all; warning off; addpath('C:\Users\rj-wang\Documents\jsonlab-1.5\jsonlab-1.5'); path = 'I:\dataset\vidar_dvs_dataset\'; vidarImg_path = [path 'Vidar_frames_Sparse\']; anno_path = [path 'Vidar_BBox_Annotations_processed\anno\']; annoFiles = dir(anno_path); annoFiles = annoFiles(3:end); count = 0; workCount = 0; for vidIDX =1:size(annoFiles, 1) vidIDX videoName = annoFiles(vidIDX).name; videoPath = [anno_path videoName '\']; jsonFiles = dir([videoPath '*.json']); imgPath = [vidarImg_path videoName '\']; imgFiles = dir([imgPath '*.png']); % fid = fopen([imgPath, 'groundtruth.txt'], 'w'); for jsonID =1:size(jsonFiles, 1) json2data=loadjson([videoPath jsonFiles(jsonID).name]); % image = imread([imgPath imgFiles(jsonID).name]); x1 = json2data.Public{1, 1}.Landmark{1, 1}.Points{1, 1}.X ; y1 = json2data.Public{1, 1}.Landmark{1, 1}.Points{1, 1}.Y ; x2 = json2data.Public{1, 1}.Landmark{1, 1}.Points{1, 2}.X ; y2 = json2data.Public{1, 1}.Landmark{1, 1}.Points{1, 2}.Y ; x3 = json2data.Public{1, 1}.Landmark{1, 1}.Points{1, 3}.X ; y3 = json2data.Public{1, 1}.Landmark{1, 1}.Points{1, 3}.Y ; x4 = json2data.Public{1, 1}.Landmark{1, 1}.Points{1, 4}.X ; y4 = json2data.Public{1, 1}.Landmark{1, 1}.Points{1, 4}.Y ; % figure; imshow(image); % hold on; % rect = [x1 y1 x3 y3 ]; % % plot(rect, 'linewidth', 2); % patch = imcrop(image, rect); % figure; imshow(patch); pointAll = [x2, y2]; windSize = [x4-x2, y4-y2]; % patch = imcrop(image, [pointAll windSize]); % figure; imshow(patch); % [state, results]=draw_rect(image, pointAll, windSize); BBox = [pointAll windSize]; if BBox(1) + BBox(2) + BBox(3) + BBox(4) > 0 workCount = workCount + 1; end count = count + 1; % fprintf(fid, '%s', num2str(BBox(1))); % fprintf(fid, ','); % fprintf(fid, '%s', num2str(BBox(2))); % fprintf(fid, ','); % fprintf(fid, '%s', num2str(BBox(3))); % fprintf(fid, ','); % fprintf(fid, '%s', num2str(BBox(4))); % fprintf(fid, '\n'); end % fclose(fid); end
matlab 读取奥鹏 json 文件:
%% 奥鹏数据标注结果转换; clc; close all; clear all; warning off; addpath('C:\Users\rj-wang\Documents\jsonlab-1.5\jsonlab-1.5'); path = 'I:\CRSOT-dataset\'; folderFiles = dir(path); folderFiles = folderFiles(3:end); anno_path = 'C:\Users\rj-wang\Desktop\transformed_p4_12\'; count = 0; workCount = 0; % for folderID = 1:size(folderFiles, 1) for folderID = 6 folderName = folderFiles(folderID).name; imagePath = [path folderName '\']; videoFiles = dir(imagePath); videoFiles = videoFiles(3:end); % for vidIDX =1:size(videoFiles, 1) for vidIDX = 221 % vidIDX videoName = videoFiles(vidIDX).name; disp(['==>> deal with folder', num2str(folderID), '|', num2str(size(folderFiles, 1)), ' videoID: ', num2str(vidIDX), '|', num2str(size(videoFiles, 1)), ' videoName: ', videoName]); videoPath = [anno_path videoName '\']; jsonFiles = dir([videoPath '*.json']); if size(jsonFiles, 1) ==0, jsonFiles = dir([[anno_path videoName '\RGB_formated\'] '*.json']); videoPath = [anno_path videoName '\RGB_formated\']; end imgPath = [imagePath videoName '\RGB_formated\']; imgFiles = dir([imgPath '*.png']); txtsavePath = [imagePath videoName '\']; mkdir(txtsavePath); fid = fopen([txtsavePath, 'absent_label.txt'], 'w'); fid2 = fopen([txtsavePath, 'groundtruth.txt'], 'w'); savepath = 'C:\Users\rj-wang\Desktop\transformed_p4_12\20210920a_00151_done\savedIMG\'; for jsonID =1:size(imgFiles, 1) % jsonID image = imread([imgPath imgFiles(jsonID).name]); [rows, cols, depth] = size(image); imgNAME = imgFiles(jsonID).name; imgNAME = imgNAME(1:end-4); jsonFIlename = [imgNAME '.json']; try json2data=loadjson([videoPath jsonFIlename]); x1 = json2data.json{1, 1}.objects{1, 1}.polygon{1, 1}.x ; y1= json2data.json{1, 1}.objects{1, 1}.polygon{1, 1}.y ; x2 = json2data.json{1, 1}.objects{1, 1}.polygon{1, 2}.x; %% 655.1363 y2 = json2data.json{1, 1}.objects{1, 1}.polygon{1, 2}.y; %% 44.2235 x3 = json2data.json{1, 1}.objects{1, 1}.polygon{1, 3}.x; y3 = json2data.json{1, 1}.objects{1, 1}.polygon{1, 3}.y; x4 = json2data.json{1, 1}.objects{1, 1}.polygon{1, 4}.x; %% 391.4923 y4 = json2data.json{1, 1}.objects{1, 1}.polygon{1, 4}.y; %% 1016.3 fprintf(fid2, '%s', num2str(json2data.json{1, 1}.objects{1, 1}.polygon{1, 1}.x)); fprintf(fid2, ','); fprintf(fid2, '%s', num2str(json2data.json{1, 1}.objects{1, 1}.polygon{1, 1}.y)); fprintf(fid2, ','); fprintf(fid2, '%s', num2str(json2data.json{1, 1}.objects{1, 1}.polygon{1, 3}.x - json2data.json{1, 1}.objects{1, 1}.polygon{1, 1}.x)); fprintf(fid2, ','); fprintf(fid2, '%s', num2str(json2data.json{1, 1}.objects{1, 1}.polygon{1, 3}.y - json2data.json{1, 1}.objects{1, 1}.polygon{1, 1}.y)); fprintf(fid2, '\n'); fprintf(fid, '%s', num2str(1)); fprintf(fid, '\n'); catch BBox = [0 0 0 0]; fprintf(fid2, '%s', num2str(0)); fprintf(fid2, ','); fprintf(fid2, '%s', num2str(0)); fprintf(fid2, ','); fprintf(fid2, '%s', num2str(0)); fprintf(fid2, ','); fprintf(fid2, '%s', num2str(0)); fprintf(fid2, '\n'); fprintf(fid, '%s', num2str(0)); fprintf(fid, '\n'); end % BBox figure; set(gcf, 'position', [x1 y1 x2 y4]); imshow(image); hold on; rectangle('position', [x1 y1 x2-x1 y4-y1], 'linewidth', 3, 'edgecolor', 'g'); end fclose(fid); fclose(fid2); end end
%% clc; close all; warning off; dataset_path = 'D:\CRSOT_dataset\DSEC_dataset\'; sequence= dir(dataset_path); sequence = sequence(3:end); for videoIndex = 1:size(sequence, 1) videoname = sequence(videoIndex).name; savingPath = [dataset_path videoname '\temp_visIMGs\']; mkdir(savingPath); % r Red, g Green, b Blue, y Yellow, k Black m Magenta, c Cyan, g b edgeColor={'r','g','b', 'y','k','m', 'c','g','b'}; lineStyle={'-','-','-', '-','-','-', '-','--',':',}; trackerResult = importdata([dataset_path videoname '\groundtruth.txt']); frames = dir([dataset_path videoname '/vis_imgs/*.png']); if(isempty(frames)==1) frames = dir([sequencePath videoname '/vis_imgs/*.jpg']); end frames={frames.name}; for frameIndex=1:length(frames) im = imread([dataset_path videoname '/vis_imgs/' frames{frameIndex}]); imshow(uint8(im)); rectangle('Position', trackerResult(frameIndex,:), 'LineWidth',3,'EdgeColor',edgeColor{2},'LineStyle',lineStyle{1}); hold on; text(5, 18, strcat('#',num2str(frameIndex)), 'Color','y', 'FontWeight','bold', 'FontSize',50); set(gca,'position',[0 0 1 1]); pause(0.00001); hold off; imwrite(frame2im(getframe(gcf)),[savingPath num2str(sprintf('%05d', frameIndex)) '.jpg']); end %% save image into videos. file = dir([dataset_path videoname '\temp_visIMGs\*.jpg']); file_ = dir([dataset_path videoname '\event_imgs\*.png']); writerObject = VideoWriter(['E:\CRSOT_dataset\DSEC_subset\' videoname '.avi']); writerObject.FrameRate = 30; %writerObject.Quality = 50; open( writerObject ); if length(file_) <= length(file) Num = length(file_); else Num = length(file); end for k =1 : Num disp(['processing the ',num2str(k),'/',num2str(Num),' frames, please waiting....']); frame_1 = imread([dataset_path videoname '\temp_visIMGs\', file(k).name]); try frame_222 = imread([dataset_path videoname '\event_imgs\', file_(k).name]); catch frame_222 = imread([dataset_path videoname '\event_imgs\', file_(k-1).name]); end frame_222 = imresize(frame_222, [size(frame_1, 1), size(frame_1, 2)]); fullframe = [ frame_1, frame_222 ]; writeVideo( writerObject, fullframe ); end close( writerObject ); %% delete the visualized images delFiles = dir(savingPath); for imgID =1:size(delFiles, 1) delete([savingPath delFiles(imgID).name]); end rmdir(savingPath); end
%%clc; close all; warning off;
dataset_path = 'D:\CRSOT_dataset\DSEC_dataset\';sequence= dir(dataset_path);sequence = sequence(3:end);
for videoIndex = 43:size(sequence, 1) videoname = sequence(videoIndex).name; savingPath = [dataset_path videoname '\temp_visIMGs\']; mkdir(savingPath); % r Red, g Green, b Blue, y Yellow, k Black m Magenta, c Cyan, g b edgeColor={'r','g','b', 'y','k','m', 'c','g','b'}; lineStyle={'-','-','-', '-','-','-', '-','--',':',};
trackerResult = importdata([dataset_path videoname '\groundtruth.txt']); frames = dir([dataset_path videoname '/vis_imgs/*.png']); if(isempty(frames)==1) frames = dir([sequencePath videoname '/vis_imgs/*.jpg']); end frames={frames.name}; for frameIndex=1:length(frames) im = imread([dataset_path videoname '/vis_imgs/' frames{frameIndex}]); imshow(uint8(im)); rectangle('Position', trackerResult(frameIndex,:), 'LineWidth',3,'EdgeColor',edgeColor{2},'LineStyle',lineStyle{1}); hold on; text(5, 18, strcat('#',num2str(frameIndex)), 'Color','y', 'FontWeight','bold', 'FontSize',50); set(gca,'position',[0 0 1 1]); pause(0.00001); hold off; imwrite(frame2im(getframe(gcf)),[savingPath num2str(sprintf('%05d', frameIndex)) '.jpg']); end %% save image into videos. file = dir([dataset_path videoname '\temp_visIMGs\*.jpg']); file_ = dir([dataset_path videoname '\event_imgs\*.png']); writerObject = VideoWriter(['E:\CRSOT_dataset\DSEC_subset\' videoname '.avi']); writerObject.FrameRate = 30; %writerObject.Quality = 50; open( writerObject ); if length(file_) <= length(file) Num = length(file_); else Num = length(file); end for k =1 : Num disp(['processing the ',num2str(k),'/',num2str(Num),' frames, please waiting....']); frame_1 = imread([dataset_path videoname '\temp_visIMGs\', file(k).name]); try frame_222 = imread([dataset_path videoname '\event_imgs\', file_(k).name]); catch frame_222 = imread([dataset_path videoname '\event_imgs\', file_(k-1).name]); end frame_222 = imresize(frame_222, [size(frame_1, 1), size(frame_1, 2)]); fullframe = [ frame_1, frame_222 ]; writeVideo( writerObject, fullframe ); end close( writerObject ); %% delete the visualized images delFiles = dir(savingPath); for imgID =1:size(delFiles, 1) delete([savingPath delFiles(imgID).name]); end rmdir(savingPath);end