how to specify positions of the blocks in an image

4 views (last 30 days)
i have divided my 64x64 image into overlapping blocks of 2x2. and the block elements are stored in another array. the array contains the first block elements in first row. second block elements in the second row and so on. now can anyone please help me in finding the the starting row and col position of that block?
i used the function im2col to divide my image into blocks. thank you!

Answers (2)

David Young
David Young on 11 Jan 2012
Suppose you have
B = im2col(A, [2 2]);
and the size of A is [64 64].
Then B(1,k) comes from A(r,c) where
c = floor((k-1)/63) + 1
r = k - (c-1)*63
More generally, if the size of A is [mm nn], and you start from
B = im2col(A, [m n]);
then B(1,k) comes from A(r,c) where
c = floor((k-1)/(mm-m+1)) + 1
r = k - (c-1)*(mm-m+1)
  2 Comments
Prajakta Shende
Prajakta Shende on 13 Jan 2012
p=imread('D:\image forgery\db\1aa.jpg');
% imshow(p);
% p=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16];
p=imresize(p,[64 64]);
[m,n]=size(p);
in=input('Enter block size');
disp('Beginning block division using im2col');
c=im2col(p,[in in],'sliding');
c=c';
disp('Block division complete')
[row col]=size(c);
% y=0;
% for i=1:row-in
% for j=1:col-in
% x=0;
% y=y+1;
% pos(y,1)=i;
% pos(y,2)=j;
% end
% end
xyz=0;
for i=1:row
max(c(i,:));
min(c(i,:));
mean(c(i,:));
var(c(i,:));
xyz=xyz+1;
% output(xyz,1)=pos(i);
% output(xyz,2)=pos(j);
%
output(xyz,1)=max(c(i,:));
output(xyz,2)=min(c(i,:));
output(xyz,3)=mean(c(i,:));
output(xyz,4)=var(c(i,:));
end
toc
there is some big problem in the min, max part of the code itself. even if i run it for 16x16 block size it gives toooo many values. which is wrong.
for 16x16 with a blck size of 2 (say) the no of rows would be 9 each containing 4 columns.
and the mean,max,min,var of these rows should be only 9.
but it gives many many values.
can u please tell me the necessary change to be made?
David Young
David Young on 13 Jan 2012
I am sorry, but I don't understand what you're trying to do. It looks like a new question. The answer I gave is to your original question as I understood it, in the simplest form I could.
Perhaps you need to explain what you are trying to achieve as your final result.

Sign in to comment.


Walter Roberson
Walter Roberson on 13 Jan 2012
Why should the min or max of the rows be only 9? You are applying the statistics to c(i,:) where c is the output of im2col() applied to p, where p is the resize of the image that was read in. im2col does not return indices, it returns array contents -- so your c is going to have whatever content the image had. Unless the image itself was confined to values in the range 0 to 9, the max() could easily exceed 9.
  3 Comments
Walter Roberson
Walter Roberson on 13 Jan 2012
Seems to me that there should be 15 blocks in each direction when you using overlapping 2 x 2 blocks on a 16x16 image. For any top-left image index (I,J) then (I+1,J) and (I,J+1) and (I+1,J+1) are also valid top-left indices (overlapping by one pixel of the 2x2 block); by induction, top left indices can range from 1 to 16-1 on each side, which gives 15x15 = 225 blocks.
David Young
David Young on 13 Jan 2012
Walter is correct:
>> size(im2col(zeros(16), [2 2]))
ans =
4 225

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!