批量处理小工具- 批量resize 和 批量mask2edge(canny法, 求梯度法)

resize

import cv2
import sys
import numpy as np
import os
mySys = 'ubuntu'  # win ubuntu mac
# filePath1 = '/home/xxx/data/SOC-COD/COD-CCC/mask/'
filePath1 = '/home/xxx/COD-edge/mask/'
# filePath2 = '/home/xxx/COD-edge/CODmask/'
filePath2 = '/home/xxx/COD-edge/resizemask/'
list_png = os.listdir(filePath1)  # todo 1
list_png = sorted(list_png)  # todo 2
if mySys == 'mac':
    list_png = list_png[1:]
print(list_png)
if __name__ == '__main__':
    for i in list_png:  # todo 3
        # print(type(i))
        img = cv2.imread(filePath1 + i)  # todo 4
        print(img.shape)
        img = cv2.resize(img, (int(352), int(352)), interpolation=cv2.INTER_AREA)
        # img = 255. - img  # todo 5
        # output = filePath2 + i
        cv2.imwrite(filePath2 + i, img)
        # cv2.imwrite(output, img)

mask2edge

import cv2
import sys
import numpy as np
import os
mySys = 'ubuntu'  # win ubuntu mac
filePath1 = '/home/xxx/COD-edge/mask/'
filePath2 = '/home/xxx/COD-edge/EG-edge/'
list_png = os.listdir(filePath1)  # todo 1
list_png = sorted(list_png)  # todo 2
if mySys == 'mac':
    list_png = list_png[1:]
print(list_png)
if __name__ == '__main__':
    for i in list_png:  # todo 3
        # print(type(i))
        img = cv2.imread(filePath1 + i)  # todo 4
        print(img.shape)
        img = cv2.GaussianBlur(img,(3,3),0)
        canny = cv2.Canny(img, 0, 250)
        # img = cv2.resize(img, (int(352), int(352)), interpolation=cv2.INTER_AREA)
        # img = 255. - img  # todo 5
        # output = filePath2 + i
        cv2.imwrite(filePath2 + i, canny)
        # cv2.imwrite(output, img)

EGNet提供

mask2edge

src_root = '/home/xxx/COD-edge/CODmask';
dst_root = '/home/xxx/COD-edge/EG-CODedge';
lst_set = '/home/xxx/COD-edge/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已经去掉后缀了这里不需要
    disp(id)
%     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

猜你喜欢

转载自blog.csdn.net/zjc910997316/article/details/114032483