This is a follow up to the question: Overlapping sliding window over an image using blockproc or im2col?
So by using the code :
B = blockproc(A, [1 1], @block_fun, 'BorderSize', [2 2], 'TrimBorder', false, 'PadPartialBlocks', true)
I was able to create an overlapping sliding window over my image and calculate the dct2
for each window. But the problem is that blockproc
concatenates the output in a way that I cannot use. The output greatly depends on the block size and the size of the output matrix is different because of it every time.
My dct2
function creates a 1 x 200
vector for every block or window. So I assumed that if there are 64 blocks I should get something like 64 x 200
or 200 x 64
output, but I get something like 64 x 1600
or in case of larger blocks I get 15 x 400
.
Looking into the blockproc
function the problem is caused by
% write 4 corner blocks
b(1:ul_output_size(1),1:ul_output_size(2),:) = ul_output;
if ll_processed
last_row_start = final_rows - size(ll_output,1) + 1;
last_row_width = size(ll_output,2);
b(last_row_start:end,1:last_row_width,:) = ll_output;
end
if ur_processed
last_col_start = final_cols - size(ur_output,2) + 1;
last_col_height = size(ur_output,1);
b(1:last_col_height,last_col_start:end,:) = ur_output;
end
if lr_processed
last_row_start = final_rows - size(ll_output,1) + 1;
last_col_start = final_cols - size(ur_output,2) + 1;
b(last_row_start:end,last_col_start:end,:) = lr_output;
end
Apparently, blockproc further divides the blocks into upper left, upper right, lower left and lower right and concatenates that result. And that is why I am getting all this mixed outputs.
I need the output of each block in its each row, for each window. Each window should just give me a 1x200
output, that I can feed into my classifier.
Can I force the output of blockproc
in the way that I want it, just give the output of each block.
If not, I would really appreciate an alternative solution to have an overlapping sliding window over the image.
edit: would it be possible to save the blocks data using block_struct.data
for every block into a cell array inside the function block_fun
and then use that array to extract my features?
Thank you
edit:
B = blockproc(images_m{1}, [64 64], @(x)reshape(x.data(:),[1 1 numel(x.data)]), 'BorderSize', [10 10], 'TrimBorder', false, 'PadPartialBlocks', true, 'PadMethod', 'replicate');
imgs = {};
for i = 1:size(B,1)
for j = 1:size(B,2)
tempy = squeeze(B(i,j,:));
tempy2 = reshape(tempy, [84 84]);
feats{end+1} = block_dct2(tempy2); %calculates dct2 for the block and returns a 1*200 vector
end
end
Maybe reshape you data in the third dimension?
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> B = blockproc(A, [1 1], @(x)reshape(x.data(:),[1 1 numel(x.data)]), 'BorderSize', [1 1], 'TrimBorder', false, 'PadPartialBlocks', true);
>> whos B
Name Size Bytes Class Attributes
B 3x3x9 648 double
>> squeeze(B(1,1,:))
ans =
0
0
0
0
8
3
0
1
5
>>
An alternate using MAT2CELL:
function extractFeatures
images_m{1} = rand(128);
B = blockproc(images_m{1}, [64 64], @processBlock,...
'BorderSize', [10 10], 'TrimBorder', false,...
'PadPartialBlocks', true, 'PadMethod', 'replicate');
%B is 2x400 i.e 2x2 blocks of each block being a 1x200 feature vector
m = ones(1,size(B,1));
n = 200*ones(1,size(B,2)/200);
% The MAT2CELL help does a good job, just read it carefully and run the
% examples
feats = mat2cell(B,m,n);
feats = feats(:);
end
function feature = processBlock(bstruct)
% I dont know what block_dct2 does:
%feature = block_dct2(bstruct.data);
% So I'll put in a place holder which returns a 1x200 'feature'
% for each overlapping image block
feature = repmat(mean(bstruct.data(:)), [1 200]);
end
来源:https://stackoverflow.com/questions/29109241/is-there-any-way-to-control-the-concatenation-of-the-blockproc-output