MATLAB:批量对图片进行裁剪

版权声明:Copyright (c) strongnine https://blog.csdn.net/weixin_39679367/article/details/84475901

代码:

%% crop the im into 256*256

clear;clc;
file_path = 'path\to\your\images\'; % 设定你存放图片的目录
img_path_list = dir(strcat(file_path, '*.jpg')); % 选后缀为 .jpg 的图片
img_num = length(img_path_list); %获得图片数量

for j = 1:img_num 
    image_name = img_path_list(j).name;
    image = imread(strcat(file_path, image_name));
    crop_image = imcrop(image, [118, 85, 255, 255]); % 使用 imcrop() 函数来裁剪图片,第二个参数的格式为 [XMIN YMIN WIDTH HEIGHT]
    imwrite(crop_image, strcat('path\to\save\', image_name)); % 保存文件
end

代码中使用的函数:

dir() 列出符合字符串 strcat(file_path, '*.jpg') 的所有文件;

strcat() 函数是用来把两个字符串合起来的;

imcrop(image, [XMIN YMIN WIDTH HEIGHT]) 指定了图片和需要裁剪的地方,指定的方式是,指定左上角,和需要裁剪的宽和高;

imwrite(image, path) 就是写文件的。

猜你喜欢

转载自blog.csdn.net/weixin_39679367/article/details/84475901
今日推荐