DIRPARAM.ORIGIN_IMAGE_DIR = 'E:\background\images';
DIRPARAM.RESULT_IMAGE_DIR = 'E:\background\cropimage';
fprintf('make a new dir for every image named by imagename\n');
im_dir = dir([DIRPARAM.ORIGIN_IMAGE_DIR,'\*.jpg']);
for i=1:length(im_dir)
imagename = im_dir(i).name;
img = imread([DIRPARAM.ORIGIN_IMAGE_DIR,'\',imagename]);
[h,w,c] = size(img);
filename = imagename(1:end-4);
mkdir([DIRPARAM.RESULT_IMAGE_DIR,'\',filename]);
cd ([DIRPARAM.RESULT_IMAGE_DIR,'\',filename]);
fprintf('process the %s\n',imagename);
for j=1:h
for k=1:w
if (j<16&&k<w-31)
temp = imcrop(img,[k,j,31,31]);
end
if j<16&&k>=w-31
temp = imcrop(img,[k-31,j,31,31]);
end
%向下取区域
if j>=16&&j<h-16&&k<16
temp = imcrop(img,[k,j-15,31,31]);
%以像素为中心取区域
elseif j>=16&&j<h-16&&k>=16&&k<w-31
temp = imcrop(img,[k-15,j-15,31,31]);
elseif j>=16&&j<h-16&&k>=w-31
temp = imcrop(img,[k-31,j-15,31,31]);
end
if j>=h-16&&k<w-31
temp = imcrop(img,[k,j-31,31,31]);
elseif j>=h-16&&k>=w-31
temp = imcrop(img,[k-31,j-31,31,31]);
end
pachname = num2str((j-1)*w+k);
[h1,w1,c1] = size(temp);
if h1<32||w1<32
fprintf('bu fu he yao qiu %d,%d \n',j,k);
end
imwrite(temp,[DIRPARAM.RESULT_IMAGE_DIR,'\',filename,'\',pachname,'.jpg']);
end
end
end
原图:
分割小区域图