我将图像分成16x16块,其中每个块都像一个数组。如何将每个块转换为图像?
我的代码是:
I=imread(image);
img=rgb2gray(I);
[col, row] = find(img<250);
imout = I(min(col):max(col), min(row):max(row));
imshow(imout);
[rows columns numberOfBands]=size(imout);
blockSizeR = 16;
blockSizeC = 16;
wholeBlockRows = floor(rows / blockSizeR);
wholeBlockCols = floor(columns / blockSizeC);
blockNumber=1;
for row = 1 : blockSizeR : rows
for col = 1 : blockSizeC : columns
row1 = row;
row2 = row1 + blockSizeR - 1;
row2 = min(rows, row2);
col1 = col;
col2 = col1 + blockSizeC - 1;
col2 = min(columns, col2);
block=imout(row1:row2, col1:col2);
subplot(16,16,blockNumber);
imshow(block);
blockNumber = blockNumber + 1;
end
end
答案 0 :(得分:0)
要使用数组创建图像文件,您应该使用内置的imwrite
函数。
这是一段与您已经完成的代码完全相同的代码,并将其保存到文件中:
% ...
% imout contains the image to split
n = 16;
% Divide into nxn images
s1 = [n*ones(1,floor(size(imout,1)/n)) mod(size(imout,1),n)];
s2 = [n*ones(1,floor(size(imout,2)/n)) mod(size(imout,2),n)];
C = mat2cell(imout, s1, s2);
% Save into files
outpref = '/your/path/img_';
outsuff = '.png';
[I, J] = meshgrid(1:numel(s1), 1:numel(s2));
cellfun(@(x,i,j) imwrite(x, [outpref num2str(i) '_' num2str(j) outsuff]), C, num2cell(I'), num2cell(J'));