1 源代码
将显著性检测的 salient maps 转换为 salient edge maps
src_root = 'H:\matlab\Az_Docter_Project\MB';
dst_root = 'H:\matlab\Az_Docter_Project\MB_edge';
lst_set = 'H:\matlab\Az_Docter_Project\MB';
lst_set = [lst_set '.lst']; % lst_set -> 'H:\matlab\Az_Docter_Project\MB.lst'
index_file = fullfile(lst_set); % index_file -> H:\matlab\Az_Docter_Project\MB.lst.lst
%open
fileID = fopen(index_file); % disp(fileID) -> 3 表示有三个参数吗
im_ids = textscan(fileID, '%s');
% disp(im_ids) -> {31x1 cell} 31表示.lst文件有多少行
im_ids = im_ids{1};
% disp(im_ids)
%im_ids:
% 'COCO_train2014_000000004823'
% ...
% 'COCO_train2014_000000014502'
fclose(fileID);
% close
num_images = length(im_ids); % 31
for im_id = 1:num_images
id = im_ids{im_id}; % 从1到31, 1为例 'COCO_train2014_000000004823'
id = id(1:end-0); % 原版是end-4,这是因为要减去'.png', 因为我们的MB.lst已经去掉后缀了这里不需要
% img_path = fullfile(data_root, [id '.jpg']);
% image = imread(img_path);
gt = imread(fullfile(src_root, [id '.png']));
% fullfile加,会自动补充分割符号 -> 'H:\matlab\Az_Docter_Project\MB' + 'COCO_train2014_000000004823.png'
% 'H:\matlab\Az_Docter_Project\MB\COCO_train2014_000000004823.png'
%计算edge
gt = (gt > 128); % 只选取亮度到达一定程度的,在这里都一样
gt = double(gt);
[gy, gx] = gradient(gt); % 计算xy方向的梯度
temp_edge = gy.*gy + gx.*gx; % x^2 + y^2
temp_edge(temp_edge~=0)=1; % 只要不等于零,置1
bound = uint8(temp_edge*255); % x255
save_path = fullfile(dst_root, [id '_edge.png']);
% out_root -> 'H:\matlab\Az_Docter_Project\MB';
% id -> 'COCO_train2014_000000004823'
% '_edge.png'
imwrite(bound, save_path);
end
2 实用代码
特别注意:对于有些数据是需使用
gt = (gt > 0);才可以
data_root = '/home/nk/zjc/data/data-RGBD/train/NUK2K-NLPR-Train-2/X-edge/Mask';
out_root = '/home/nk/zjc/data/data-RGBD/train/NUK2K-NLPR-Train-2/X-edge/edge';
lst_set = '/home/nk/zjc/data/data-RGBD/train/NUK2K-NLPR-Train-2/X-edge/train';
index_file = fullfile([lst_set '.lst']); % ./test.lst
disp(index_file)
fileID = fopen(index_file);
im_ids = textscan(fileID, '%s');
im_ids = im_ids{1};
fclose(fileID);
num_images = length(im_ids);
for im_id = 1:250
id = im_ids{im_id};
id = id(1:end-0);
% img_path = fullfile(data_root, [id '.jpg']);
% image = imread(img_path);
disp('1')
disp(fullfile(data_root, [id '.png']));
gt = imread(fullfile(data_root, [id '.png']));
gt = (gt > 0);
gt = double(gt);
[gy, gx] = gradient(gt);
temp_edge = gy.*gy + gx.*gx;
temp_edge(temp_edge~=0)=1;
bound = uint8(temp_edge*255);
%bound = uint8(temp_edge);
save_path = fullfile(out_root, [id '.png']);
disp('2')
disp(save_path)
imwrite(bound, save_path);
%imwrite(gt, save_path);
end