调整大小后替换 Matlab 中的像素

replacing pixels in Matlab after resizing

我有一个 Matlab 脚本(见下文)可以将装满图片的文件夹 (490 x 500) 调整为新大小 (512 x 512)。现在,因为原始图片不是正方形,调整大小后发生的是每张图片的左侧和右侧出现白色条带。这大概是一个自动填充,保持图片的比例,同时达到512x512的尺寸。

我需要怎么做才能去掉图片两边的那些白条?也许用黑色像素替换白色像素,但如何实现这一战略明智?

clear all
close all

dir = 'D:\matlab_picture_edit\pics'; %location of images
outdir = 'D:\matlab_picture_edit\pics_new'; %location of directory for output images

cd (dir); % go to source dir
fs = filesep
dln = '.'

num = 1 % iterations of transform
images = cellstr(ls(dir)); % list all files in source dir
images = images(3:end); % remove first 2 lines (.., .)

 for h = 1:length(images) % all images
     flnmdel(h,:) = strsplit(images{h},dln) % split up file name
 end

for pic = 1:numel(images)
   image = images{pic};

   img = imread(image);

   img2 = imresize(img, [512, 512]);

   imwrite(img2,[outdir fs flnmdel{pic} '_s.jpg'] )   

end

你的代码没有问题。

我试过在不是你在问题中提到的正方形图像上这样做!您的实际图像可能有空白,当您增加其大小时,这些空白会变大。