MATLAB:批量對圖片進行裁剪
阿新 • • 發佈:2018-12-05
程式碼:
%% 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)
就是寫檔案的。