一,不重叠分块
效果:
clear;
clc;
img = imread('/media/P1.jpg');
%% resize the image into the new size with 500x*500y
r_img = img(:, :, 1);
g_img = img(:, :, 2);
b_img = img(:, :, 3);
[row, col] = size(r_img);
new_row = ceil(row/500) * 500;
new_col = ceil(col/500) * 500;
new_r_img = imresize(r_img, [new_row new_col], 'bilinear');
new_g_img = imresize(g_img, [new_row new_col], 'bilinear');
new_b_img = imresize(b_img, [new_row new_col], 'bilinear');
new_img(:, :, 1) = new_r_img;
new_img(:, :, 2) = new_g_img;
new_img(:, :, 3) = new_b_img;
[y_row y_col dim] = size(new_img);
row_blk_num = y_row/500; % 3
col_blk_num = y_col/500; % 6
no overlap
blocks = 1;
for i = 1:row_blk_num
for j = 1:col_blk_num
disp(blocks);
block = new_img((i - 1) * 500 + 1 : i * 500, (j - 1) * 500 + 1 : j * 500, :);
imwrite(block, ['./' num2str(blocks) '.jpg']);
blocks = blocks + 1;
end
end
二,重叠(重叠大小为块的一半)
效果(部分)
代码:
clear;
clc;
img = imread('/media/P1.jpg');
%% resize the image into the new size with 16x*16y
r_img = img(:, :, 1);
g_img = img(:, :, 2);
b_img = img(:, :, 3);
[row, col] = size(r_img);
new_row = ceil(row/500) * 500;
new_col = ceil(col/500) * 500;
new_r_img = imresize(r_img, [new_row new_col], 'bilinear');
new_g_img = imresize(g_img, [new_row new_col], 'bilinear');
new_b_img = imresize(b_img, [new_row new_col], 'bilinear');
new_img(:, :, 1) = new_r_img;
new_img(:, :, 2) = new_g_img;
new_img(:, :, 3) = new_b_img;
[y_row y_col dim] = size(new_img);
row_blk_num = (y_row-250)/250; % 5
col_blk_num = (y_col-250)/250; % 11
blocks = 1;
for i = 1:row_blk_num
for j = 1:col_blk_num
disp(blocks);
block = new_img((i - 1) * 250 + 1 : (i+1) * 250, (j - 1) * 250 + 1 : (j+1) * 250, :);
imwrite(block, ['./' num2str(blocks) '.jpg']);
blocks = blocks + 1;
end
end