有些图像可能太大,处理不方便,所以我写了一个程序用来图像分块。
分块后图像处理后,再将处理后的图像进行拼接,实现最后的结果。
源图像----->分块----->处理------>处理后的小片结果图像-------->合并。
以上处理各个图像之间没有重叠。
原始图像:
分割后:
分割的代码:
clear;
clc;
%%图片进行无重叠分块
img = imread('C:\Users\A39.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);
%ceil 进行四舍五入计算
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
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, ['C:\Users\MenahemLi\Desktop\结果\A\' num2str(blocks) '.jpg']);
blocks = blocks + 1;
end
end
图像恢复:
恢复是从文件名入手,所以文件名对于恢复很重要。不可乱改
合并的代码:
clear;
clc;
%%进行无重叠分块后的图片复原
img = imread('C:\Users\A\A39.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);
%ceil 进行四舍五入计算
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; % 行数
col_blk_num = y_col/500; % 列数
blocks = 1;
temp = zeros(500,col_blk_num*500);
for i = 1:row_blk_num
img = imread(['C:\Users\A\' num2str((i-1)*col_blk_num+1) '.jpg']);
for j = 1:col_blk_num
img2 = imread(['C:\Users\A\' num2str(blocks) '.jpg']);
img = [img,img2];
blocks = blocks + 1;
end
if(i==1)
temp = img;
end
if(i~=1)
temp=[temp;img];
end
end
imshow(temp);
关注公众号获取更多有趣分享