get distnct blocks contained in a given matrix.

Hello. Lets say an image of size [M N] to blocks of size [i j] each. using the following piece of code:
A = im2col(im,[i j],'distinct');
% ....
% ....
% ....
% do some more operations on A
% derive submatrix B of size [m n] from A.
% QUESTION
% How can I retrieve the distnct blocks of size (i, j) contained in B
% and store them say in a different matrix.
% Thank you very much.
After a series of more operations, I derive a submatrix, say B of size [m n] from A.
My question is. How can I find how many blocks of size (i, j) are in B. Also how can I store these blocks in a different array? Thank you very much.

 Risposta accettata

In any one direction, if you need distinct blocks of size P and the available length along the dimension is Q, then the number of ways you can position the block along that dimension is (Q-P+1) . For example blocks of length 3 in a vector of length 5, [a b c d e f] can be [a b c], [b c d], [d e f] -- three different blocks, and (5-3+1) = 3 .
Now you have the same calculation along the other dimension.
Finally, you multiply the values for all of the dimensions.

9 Commenti

@Walter Roberson, if its not much trouble, can you please demonstrate this on any simple 2D double image with block sizes of say 8 ? I will really appreciate that.
a b c d e f
g h i j k l
m n o p q r
and let say we need blocks that are 2 rows by 3 columns.
Calculation: number of rows = 3, minus number of rows per block = 2, plus 1, gives 2.
Number of columns = 6, minus number or columns per block = 3, plus 1, gives 4.
Prediction: number of distinct blocks is 2*4 = 8.
The distinct blocks are:
a b c, b c d, c d e, d e f --> 4 distinct blocks
g h i, h i j, i j k, j k l
g h i, h i j, i j k, j k l --> 4 more distinct blocks
m n o, n o p, o p q, p q r
4+4 = 8. So by enumerating all of the blocks of the required size, we see we got 8 distinct blocks, which exactly matches the prediction of the number of blocks.
Thanks very much Walter. that part is now clear. In fact I have summarised it below in accordance to my description. Please let me know if i have some misunderstanding so far:
But I couldnt come up with the MATLAB code to extract and store the blocks. As you have shown in the last part of the demo. Can you please help me with that? I want to store the blocks separately say in an array or file.
% no. rows in my matrix, M = 3 - no or rows per block, i = 2, plus 1: (3-2) + 1 = 2
% i.e. r = (M-i)+1
% no. cols in matrix, N = 6 - no or columns per block, j = 3, plus 1: (6-3) + 1 = 4
% i.e. c = (N-j)+1
% prediction: Number of distinct blocks = 2 * 4
% i.e. r * c.
MatlabEnthusiast
MatlabEnthusiast il 23 Nov 2021
Modificato: MatlabEnthusiast il 23 Nov 2021
Especially from the result of 'im2col'. Sorry, I had not specified that in my former comment.
Why not use im2col() followed by num2cell()
img = imread('cameraman.tif');
ascol = im2col(img, [8 8], 'sliding');
separated = num2cell(ascol, 1);
size(separated), size(separated{1})
ans = 1×2
1 62001
ans = 1×2
64 1
Any time I have wanted to do anything like this, it has been in circumstances where using blockproc() or nlfilt() or conv2() has been a better choice.
MatlabEnthusiast
MatlabEnthusiast il 23 Nov 2021
Modificato: MatlabEnthusiast il 23 Nov 2021
Thank you Walter. Honestly I did not know about num2cell till you mentioned it. So is it safe to assume that each cell contains a single column block with contents in order so if i want my (i j). block I can just iterate in order to reconstruct the blocks and am good to go? Again thanks, for the suggestions as well. I am definitely gona look into them before deciding on the final implementation in my code.
When you pass an array into num2cell, and you pass in the optional argument 1 then the output will be a cell array in which each column of the original matrix is an individual cell array entry.
alright Walter. Finally, So how is this for a crude extraction of block starting at (1,1). If its right, how best can I generalize this to get say, the block at (r, c)?
b1_crude=[separated{1}(1:8) separated{2}(1:8) separated{3}(1:8) separated{4}(1:8) separated{5}(1:8) separated{6}(1:8) separated{7}(1:8) separated{8}(1:8)];

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by