Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DIVIDING AN IMAGE INTO SUB BLOCKS

2,600 views
Skip to first unread message

sanyukta

unread,
Feb 10, 2012, 10:44:10 AM2/10/12
to
I have an image of size 500x500.I want to divide it into subblocks of size 100x100.Please help me with the code.

Steven_Lord

unread,
Feb 10, 2012, 11:15:55 AM2/10/12
to


"sanyukta " <sanyukta...@gmail.com> wrote in message
news:jh3e0a$143$1...@newscl01ah.mathworks.com...
> I have an image of size 500x500.I want to divide it into subblocks of size
> 100x100.Please help me with the code.

If you then want to apply some function to each block, use BLOCKPROC from
Image Processing Toolbox or simply use a FOR loop.

If you just need the individual blocks, look at MAT2CELL from MATLAB.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Matt J

unread,
Feb 10, 2012, 11:24:09 AM2/10/12
to
"sanyukta " <sanyukta...@gmail.com> wrote in message <jh3e0a$143$1...@newscl01ah.mathworks.com>...
>
> I have an image of size 500x500.I want to divide it into subblocks of size 100x100.Please help me with the code.

Since the blocks are all the same size, this might be useful

http://www.mathworks.com/matlabcentral/newsreader/view_thread/316327#865029

sanyukta

unread,
Feb 10, 2012, 12:44:13 PM2/10/12
to
"Steven_Lord" <sl...@mathworks.com> wrote in message <jh3frt$7od$1...@newscl01ah.mathworks.com>...
Thanks..I have used mat2cell function and got a total of p=25 blocks.So each block has q=100x100 elements.Next,I want to concatenate all the elements of the block to give a vector to represent one block resulting in a matrix x=[x1,x2,....xp]of size qxp.How to do?

Matt J

unread,
Feb 10, 2012, 2:11:09 PM2/10/12
to
"sanyukta " <sanyukta...@gmail.com> wrote in message <jh3l1d$phd$1...@newscl01ah.mathworks.com>...
================

Is this what you want?


>> A=rand(500);
>> B=mat2tiles(A,[100,100])

B =

Columns 1 through 4

[100x100 double] [100x100 double] [100x100 double] [100x100 double]
[100x100 double] [100x100 double] [100x100 double] [100x100 double]
[100x100 double] [100x100 double] [100x100 double] [100x100 double]
[100x100 double] [100x100 double] [100x100 double] [100x100 double]
[100x100 double] [100x100 double] [100x100 double] [100x100 double]

Column 5

[100x100 double]
[100x100 double]
[100x100 double]
[100x100 double]
[100x100 double]


>> C=cellfun(@(c) c(:).', B,'uni',0)

C =

Columns 1 through 4

[1x10000 double] [1x10000 double] [1x10000 double] [1x10000 double]
[1x10000 double] [1x10000 double] [1x10000 double] [1x10000 double]
[1x10000 double] [1x10000 double] [1x10000 double] [1x10000 double]
[1x10000 double] [1x10000 double] [1x10000 double] [1x10000 double]
[1x10000 double] [1x10000 double] [1x10000 double] [1x10000 double]

Column 5

[1x10000 double]
[1x10000 double]
[1x10000 double]
[1x10000 double]
[1x10000 double]

ImageAnalyst

unread,
Feb 10, 2012, 7:43:28 PM2/10/12
to
On Feb 10, 12:44 pm, "sanyukta " <sanyuktacheti...@gmail.com> wrote:
> Thanks..I have used mat2cell function and got a total of p=25 blocks.So each block has q=100x100 elements.Next,I want to concatenate all the elements of the block to give a vector to represent one block resulting in a matrix x=[x1,x2,....xp]of size qxp.How to do?

----------------------------------------------------------------------------------------------
If I understand you correctly you want to take one particular block
and concatenate all the elements of that block go give a 1D vector.
To do that you just do:

m = rand(500,500); % Sample data 500x500
divisions = [100 100 100 100 100];
% Chop up into 100x100 cells.
mCell = mat2cell(m, divisions, divisions)
% Pull out one particular cell - it will be a 100x100 array.
thisBlock = mCell{2,4}; % Or whatever indexes you want.
% Concatenate into vector 10000 elements long.
columnVector = thisBlock(:);

sanyukta

unread,
Feb 11, 2012, 11:55:24 AM2/11/12
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <0bc34147-dd8c-44bb...@g27g2000yqa.googlegroups.com>...
Thanks..I used the following code:
clc;
clear all;
close all;
a=imread('glyoxylase.jpg');
a1=rgb2gray(a);
a2=mat2cell(a1,[100 100 100 100 100],[100 100 100 100 100]);
disp(a2);

I got the results of a2 as-

Columns 1 through 4

[100x100 uint8] [100x100 uint8] [100x100 uint8] [100x100 uint8]
[100x100 uint8] [100x100 uint8] [100x100 uint8] [100x100 uint8]
[100x100 uint8] [100x100 uint8] [100x100 uint8] [100x100 uint8]
[100x100 uint8] [100x100 uint8] [100x100 uint8] [100x100 uint8]
[100x100 uint8] [100x100 uint8] [100x100 uint8] [100x100 uint8]

Column 5

[100x100 uint8]
[100x100 uint8]
[100x100 uint8]
[100x100 uint8]
[100x100 uint8]
Means I got a total of 25 blocks.Next I want to concatenate all the elements of the 25 blocks to get a single vector.

Matt J

unread,
Feb 11, 2012, 1:33:27 PM2/11/12
to
"sanyukta " <sanyukta...@gmail.com> wrote in message <jh66hs$rnc$1...@newscl01ah.mathworks.com>...
=================

But what we can't understand is how the vector elements are supposed to be ordered. For example, one way you could have obtained a vector is simply to
do a2=a1(:). No need to convert to a cell at all.

sanyukta

unread,
Feb 17, 2012, 11:45:36 PM2/17/12
to
"Matt J" wrote in message <jh6c9n$dtn$1...@newscl01ah.mathworks.com>...
Well,i am going to do local processing on the image and feed the single vector in the self organizing map as input.As such I have converted the matrix into cell.Can you tell me how to create a 2-D self organizing map?I want to create a self organizing map of size 5x5.But i am not able to do it.Please help me if you have any idea.

Matt J

unread,
Feb 18, 2012, 9:21:18 AM2/18/12
to
"sanyukta " <sanyukta...@gmail.com> wrote in message <jhnadg$3u2$1...@newscl01ah.mathworks.com>...
Maybe if you explained what a "self-organizing map" is.

sanyukta

unread,
Feb 18, 2012, 10:22:12 AM2/18/12
to
"Matt J" wrote in message <jhoc4u$9fv$1...@newscl01ah.mathworks.com>...
It is a technique in artificial neural network to convert a high dimensional data to a low dimensional data.This technique is called an unsupervised technique as the learning is based only upon the input data and is independent of the desired output data.Like in feedforward network,which is a supervised technique,we provide both the input and the target and then train the network but in case of self organizing map(SOM),we provide only the input but the output is decided by the network itself and hence it is called a unsupervised technique.

Matt J

unread,
Feb 18, 2012, 1:26:22 PM2/18/12
to
"sanyukta " <sanyukta...@gmail.com> wrote in message <jhofn4$jcq$1...@newscl01ah.mathworks.com>...
>
> > Maybe if you explained what a "self-organizing map" is.
> It is a technique in artificial neural network to convert a high dimensional data to a low dimensional data.This technique is called an unsupervised technique as the learning is based only upon the input data and is independent of the desired output data.Like in feedforward network,which is a supervised technique,we provide both the input and the target and then train the network but in case of self organizing map(SOM),we provide only the input but the output is decided by the network itself and hence it is called a unsupervised technique.
==================

Alright, I take it back. Explaining what a self-organizing map is isn't going to help at all.

Look, you've been shown how to take a 500x500 numeric array and split it up into a 5x5 cell array of 100x100 blocks. Explain to us what you want next in two steps:

(1) How do you want each of the 100x100 blocks processed? Do you want them straightened out into row vectors? Into column vectors? You can do either one using cellfun


EachCellColumn = cellfun(@(c) c(:), a2,'uni',0);
EachCellRow = cellfun(@(c) c(:).', a2,'uni',0);




(2) Once you've transformed each of the 100x100 blocks, how do you want them consolidated back into a numeric array?. Do you want them stitched together as is, e.g.

cell2mat(EachCellRow)
cell2mat(EachCellColumn)

or do you want them stacked in some way,e.g.,

cell2mat(EachCellRow(:))
cell2mat(EachCellColumn(:))

Note that the final option is equivalent simply to a2(:).

sanyukta

unread,
Feb 20, 2012, 10:11:11 AM2/20/12
to
"Matt J" wrote in message <jhoqge$k4e$1...@newscl01ah.mathworks.com>...
Well,let me explain you the algorithm of my work.The steps are
(1) Divide the image into small blocks
(2)Concatenate pixels of small blocks
(3)Form a matrix with as many numbers of columns as the number of blocks
(4)Choose a 2D self organizing map
(5)Train the self organizing map
(6)Retain weight matrix corresponding to each training image
(7)Compute covariance matrix of each weight matrix
(8)Sort the eigen values and eigen vectors of the covariance matrix
(9)Retain eigen vectors corresponding to highest eigen values and retain KL coefficients
(10)Reconstruct the image at the time of recognition
(11)Match the reconstructed image with the test image using Euclidean norm

Steven_Lord

unread,
Feb 21, 2012, 11:06:48 AM2/21/12
to


"sanyukta " <sanyukta...@gmail.com> wrote in message
news:jhtnqf$gsa$1...@newscl01ah.mathworks.com...
> "Matt J" wrote in message <jhoqge$k4e$1...@newscl01ah.mathworks.com>...
>> "sanyukta " <sanyukta...@gmail.com> wrote in message
>> <jhofn4$jcq$1...@newscl01ah.mathworks.com>...

*snip*

> Well,let me explain you the algorithm of my work.The steps are
> (1) Divide the image into small blocks
> (2)Concatenate pixels of small blocks
> (3)Form a matrix with as many numbers of columns as the number of blocks
> (4)Choose a 2D self organizing map
> (5)Train the self organizing map

For steps 4 and 5, look here:

http://www.mathworks.com/help/toolbox/nnet/ug/bss4b2e.html

*more snip*

sanyukta

unread,
Feb 21, 2012, 11:23:11 AM2/21/12
to
"Matt J" wrote in message <jhoqge$k4e$1...@newscl01ah.mathworks.com>...
I have used
EachCellColumn = cellfun(@(c) c(:), a2,'uni',0);
and I got the result as

<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>

<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>

<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>

<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>

<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>
<10000x1 uint8>

but I want it to be 10000x25.

Matt J

unread,
Feb 21, 2012, 1:36:11 PM2/21/12
to
"sanyukta " <sanyukta...@gmail.com> wrote in message <ji0gdf$92o$1...@newscl01ah.mathworks.com>...
>
>
> but I want it to be 10000x25.


result=[EachCellColumn{:}];

sanyukta

unread,
Feb 22, 2012, 2:13:10 AM2/22/12
to
"Matt J" wrote in message <ji0o6r$6vq$1...@newscl01ah.mathworks.com>...
> "sanyukta " <sanyukta...@gmail.com> wrote in message <ji0gdf$92o$1...@newscl01ah.mathworks.com>...
> >
> >
> > but I want it to be 10000x25.
>
>
> result=[EachCellColumn{:}];
Does it mean that I have concatenated all the pixels of small block?
My original image size was 500x500 and I have divided the image into sub blocks of size 100x100.Thus,I got total no.of blocks=25 and each block has 100x100=10000 no.of elements.

Matt J

unread,
Feb 22, 2012, 7:19:17 PM2/22/12
to
"sanyukta " <sanyukta...@gmail.com> wrote in message <ji24i6$oi3$1...@newscl01ah.mathworks.com>...
>
>each block has 100x100=10000 no.of elements.
=============

???????? But in your last mail, you reshaped them from 100x100 to 10000x1.


Below is the code that I think you want, but I'm now kind of confused. In any case, the result is 10000x25.


a2=rand(500);%Fake

EachCellRow = cellfun(@(c) c(:), mat2tiles(a2,[100,100]),'uni',0);

TheResultYouWant =[EachCellRow{:}];

sanyukta

unread,
Feb 24, 2012, 11:09:19 AM2/24/12
to
"Matt J" wrote in message <ji40m5$c0l$1...@newscl01ah.mathworks.com>...
your suggestions helped me quite a lot.
In my image of size 500x500,the maximum value of the element is 255 and the minimum value is 0.
I want to make all the elements of the image with value 255 to zero.

Asal

unread,
May 2, 2012, 3:02:07 AM5/2/12
to
-------------------------------------------------------------------------------------------
how it is possible to find out in the image which blocks are similar to each other? if one block is copied and pasted in another place in same image.

ujwala.a...@gmail.com

unread,
Apr 3, 2014, 1:29:35 AM4/3/14
to
can u give the matlab code for "retrieval images acn be displayed in grid.."
actually iam getting the results as one image in one window,bt i want to display all retrieval images in one window using GRID..
can u do help me??

0 new messages