Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
blkproc help needed...

Subject: blkproc help needed...

From: Jack Branning

Date: 6 Oct, 2007 08:55:09

Message: 1 of 12

Hello,

I am trying to block a 512x512 grayscale image into 8x8
blocks so that I can DCT each one. I am using the following
code to do this:

img = imread('lena.png');
img = blkproc(img, [8 8], @dct2);

The result is that a 2-d DCT has been performed on each of
the blocks but I now want to look at each of these 8x8
blocks individually and perhaps modify each one subtly...

Does anyone know of a loop that will allow me to be able to
call 'A' and receive the first 8x8 block, then call 'B' to
look at the second block and so on..? Or even better, is
there a function that will already do this when I use 'blkproc'?

Thanks in advance for any help you are able to give!

Subject: blkproc help needed...

From: Steven Lord

Date: 8 Oct, 2007 13:07:19

Message: 2 of 12


"Jack Branning" <philbateman.nospam@ntlworld.com> wrote in message
news:fe7ihd$bk1$1@fred.mathworks.com...
> Hello,
>
> I am trying to block a 512x512 grayscale image into 8x8
> blocks so that I can DCT each one. I am using the following
> code to do this:
>
> img = imread('lena.png');
> img = blkproc(img, [8 8], @dct2);
>
> The result is that a 2-d DCT has been performed on each of
> the blocks but I now want to look at each of these 8x8
> blocks individually and perhaps modify each one subtly...
>
> Does anyone know of a loop that will allow me to be able to
> call 'A' and receive the first 8x8 block, then call 'B' to
> look at the second block and so on..? Or even better, is
> there a function that will already do this when I use 'blkproc'?

You can use MAT2CELL to convert your matrix into 8-by-8 blocks, then process
each of the cells manually or using CELLFUN and convert back using CELL2MAT.

n = 512;
A = magic(n);
C = mat2cell(A, 8*ones(1, (n/8)), 8*ones(1, (n/8)));
C{1, 1} == A(1:8, 1:8)

--
Steve Lord
slord@mathworks.com

Subject: blkproc help needed...

From: Jack Branning

Date: 8 Oct, 2007 14:17:41

Message: 3 of 12

"Steven Lord" <slord@mathworks.com> wrote in message
<feda27$sv4$1@fred.mathworks.com>...
>
> "Jack Branning" <philbateman.nospam@ntlworld.com> wrote in
message
> news:fe7ihd$bk1$1@fred.mathworks.com...
> > Hello,
> >
> > I am trying to block a 512x512 grayscale image into 8x8
> > blocks so that I can DCT each one. I am using the following
> > code to do this:
> >
> > img = imread('lena.png');
> > img = blkproc(img, [8 8], @dct2);
> >
> > The result is that a 2-d DCT has been performed on each of
> > the blocks but I now want to look at each of these 8x8
> > blocks individually and perhaps modify each one subtly...
> >
> > Does anyone know of a loop that will allow me to be able to
> > call 'A' and receive the first 8x8 block, then call 'B' to
> > look at the second block and so on..? Or even better, is
> > there a function that will already do this when I use
'blkproc'?
>
> You can use MAT2CELL to convert your matrix into 8-by-8
blocks, then process
> each of the cells manually or using CELLFUN and convert
back using CELL2MAT.
>
> n = 512;
> A = magic(n);
> C = mat2cell(A, 8*ones(1, (n/8)), 8*ones(1, (n/8)));
> C{1, 1} == A(1:8, 1:8)
>
> --
> Steve Lord
> slord@mathworks.com
>
>


Thanks for your help!

Do you know how I would use that in keeping with a 512x512
image? I don't want to change any of the values until I am
in a position where if I call block A, i will get the first
8x8 block... Or do you know if cell2mat can be used with
blkproc?

Subject: blkproc help needed...

From: Jack Branning

Date: 9 Oct, 2007 09:26:01

Message: 4 of 12


> Thanks for your help!
>
> Do you know how I would use that in keeping with a 512x512
> image? I don't want to change any of the values until I am
> in a position where if I call block A, i will get the first
> 8x8 block... Or do you know if cell2mat can be used with
> blkproc?

Does anyone have any ideas? Sorry to keep harping on about
it, but I really am getting nowhere with this...

Subject: blkproc help needed...

From: us

Date: 9 Oct, 2007 09:53:23

Message: 5 of 12

Jack Branning:
<SNIP sub-mat extraction evergreen...

you may want to look at this fex contribution: gblk

http://www.mathworks.com/matlabcentral/fileexchange/loadFile
.do?objectId=14801&objectType=FILE#

us

Subject: blkproc help needed...

From: Steven Lord

Date: 9 Oct, 2007 14:02:10

Message: 6 of 12


"Jack Branning" <philbateman.nospam@ntlworld.com> wrote in message
news:fefhf9$jcd$1@fred.mathworks.com...
>
>> Thanks for your help!
>>
>> Do you know how I would use that in keeping with a 512x512
>> image?

What do you mean with this statement? If you have a 512-by-512 image matrix
A, you can convert it into a 64-by-64 cell array C, each element of which is
an 8-by-8 submatrix from A, using MAT2CELL and you can reverse the process
using CELL2MAT.

>> I don't want to change any of the values until I am
>> in a position where if I call block A, i will get the first
>> 8x8 block..

Then use MAT2CELL on your matrix and ask for the first cell C{1, 1}
(assuming C is the output of MAT2CELL.)

>>. Or do you know if cell2mat can be used with
>> blkproc?

The way I think about these two functions, you'd use BLKPROC if you had a
function that you want to apply to the blocks programmatically and MAT2CELL
if you want to create the blocks and then interactively manipulate them.

--
Steve Lord
slord@mathworks.com

Subject: blkproc help needed...

From: Jack Branning

Date: 10 Oct, 2007 14:02:54

Message: 7 of 12

"Steven Lord" <slord@mathworks.com> wrote in message
<feg1l3$sr1$1@fred.mathworks.com>...
>
> "Jack Branning" <philbateman.nospam@ntlworld.com> wrote in
message
> news:fefhf9$jcd$1@fred.mathworks.com...
> >
> >> Thanks for your help!
> >>
> >> Do you know how I would use that in keeping with a 512x512
> >> image?
>
> What do you mean with this statement? If you have a
512-by-512 image matrix
> A, you can convert it into a 64-by-64 cell array C, each
element of which is
> an 8-by-8 submatrix from A, using MAT2CELL and you can
reverse the process
> using CELL2MAT.
>
> >> I don't want to change any of the values until I am
> >> in a position where if I call block A, i will get the first
> >> 8x8 block..
>
> Then use MAT2CELL on your matrix and ask for the first
cell C{1, 1}
> (assuming C is the output of MAT2CELL.)
>
> >>. Or do you know if cell2mat can be used with
> >> blkproc?
>
> The way I think about these two functions, you'd use
BLKPROC if you had a
> function that you want to apply to the blocks
programmatically and MAT2CELL
> if you want to create the blocks and then interactively
manipulate them.
>
> --
> Steve Lord
> slord@mathworks.com
>
>

Oh, ok I think I know what you are saying. I think I need to
use blkproc though, as I need to DCT the image ("@dct2") so
how would I then use mat2cell to section the result into 8x8
blocks?

Subject: blkproc help needed...

From: Steven Lord

Date: 10 Oct, 2007 15:20:32

Message: 8 of 12


"Jack Branning" <philbateman.nospam@ntlworld.com> wrote in message
news:feim2e$lno$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <feg1l3$sr1$1@fred.mathworks.com>...
>>
>> "Jack Branning" <philbateman.nospam@ntlworld.com> wrote in
> message
>> news:fefhf9$jcd$1@fred.mathworks.com...
>> >
>> >> Thanks for your help!
>> >>
>> >> Do you know how I would use that in keeping with a 512x512
>> >> image?
>>
>> What do you mean with this statement? If you have a
> 512-by-512 image matrix
>> A, you can convert it into a 64-by-64 cell array C, each
> element of which is
>> an 8-by-8 submatrix from A, using MAT2CELL and you can
> reverse the process
>> using CELL2MAT.
>>
>> >> I don't want to change any of the values until I am
>> >> in a position where if I call block A, i will get the first
>> >> 8x8 block..
>>
>> Then use MAT2CELL on your matrix and ask for the first
> cell C{1, 1}
>> (assuming C is the output of MAT2CELL.)
>>
>> >>. Or do you know if cell2mat can be used with
>> >> blkproc?
>>
>> The way I think about these two functions, you'd use
> BLKPROC if you had a
>> function that you want to apply to the blocks
> programmatically and MAT2CELL
>> if you want to create the blocks and then interactively
> manipulate them.
>>
>> --
>> Steve Lord
>> slord@mathworks.com
>>
>>
>
> Oh, ok I think I know what you are saying. I think I need to
> use blkproc though, as I need to DCT the image ("@dct2") so
> how would I then use mat2cell to section the result into 8x8
> blocks?

I believe the workflow you want to use, if I understand your previous posts,
is this:

1) Use MAT2CELL to convert your image into a cell array, where each cell
contains a block
2) Interactively manipulate the contents of the cells in the cell array
3) Use CELL2MAT to convert the cell array back into a matrix
4) Use BLKPROC to call DCT2 on each of the submatrices.

Alternately, A(8*(r-1)+(1:8), 8*(c-1)+(1:8)) is the submatrix for block (r,
c) in the matrix, with block (1,1) being the upper-left block. If you want
to manipulate the blocks without breaking the matrix into cells, use those
row and column indices.

--
Steve Lord
slord@mathworks.com

Subject: blkproc help needed...

From: Jack Branning

Date: 10 Oct, 2007 18:37:37

Message: 9 of 12


>
> I believe the workflow you want to use, if I understand
your previous posts,
> is this:
>
> 1) Use MAT2CELL to convert your image into a cell array,
where each cell
> contains a block
> 2) Interactively manipulate the contents of the cells in
the cell array
> 3) Use CELL2MAT to convert the cell array back into a matrix
> 4) Use BLKPROC to call DCT2 on each of the submatrices.
>
> Alternately, A(8*(r-1)+(1:8), 8*(c-1)+(1:8)) is the
submatrix for block (r,
> c) in the matrix, with block (1,1) being the upper-left
block. If you want
> to manipulate the blocks without breaking the matrix into
cells, use those
> row and column indices.
>
> --
> Steve Lord
> slord@mathworks.com
>
>

That's so nearly what I need to do. I can now use MAT2CELL
to block my image into submatrices so that if I call B{1,1}
it shows the first 8x8 block and so on.

However, I need to change the DCT coefficients produced
after the blkproc which means that I need some way of
accessing a particular coefficient within 1 cell... Can this
be done?

To make things clearer, the code I have so far is:

A = imread('lena.png');
A = blkproc(A, [8 8], @dct2);
B = mat2cell(A, 8*ones(1,(512/8)),8*ones(1,(512/8)));

Calling B{1,1) brings up the first 8 by 8 block, but
obviously the result is that this is a cell... Is there any
way of accessing one specific value within this cell?

Subject: blkproc help needed...

From: us

Date: 10 Oct, 2007 18:53:53

Message: 10 of 12

Jack Branning:
<SNIP cell-syntax...

> B = mat2cell(A, 8*ones(1,(512/8)),8*ones(1,(512/8)));
> Is there any way of accessing one specific value
> within this cell...

one of the solutions

% eg, row 2/col 4
     B{1,1}(2,4)
% -or- recover a mat
     b=B{1,1};
     b(2,4)

us

Subject: help needed in making blocks in an image...

From: hari prasad

Date: 18 Feb, 2010 16:26:05

Message: 11 of 12

Hi,
Iam doing a project on image compression and to use DCT on an image i need to divide the image into 8*8blocks.
blkproc is the command which only functions on the blocks which are generated.Is there any other command in matlab which allows formation of blocks .
Else how can we write a nested for-loop to select the blocks so that it iterates 8 times and then stops and continues from the ninth time.
Thanks in advance

Subject: help needed in making blocks in an image...

From: Brendan Hannigan

Date: 19 Feb, 2010 13:47:05

Message: 12 of 12

"hari prasad" <hariprasadm.89@gmail.com> wrote in message <hljpmt$p2l$1@fred.mathworks.com>...
> Hi,
> Iam doing a project on image compression and to use DCT on an image i need to divide the image into 8*8blocks.
> blkproc is the command which only functions on the blocks which are generated.Is there any other command in matlab which allows formation of blocks .
> Else how can we write a nested for-loop to select the blocks so that it iterates 8 times and then stops and continues from the ninth time.
> Thanks in advance

I don't understand what you are trying to do. What BLKPROC does is:

1) divide an input image into [M x N] blocks
2) run the user supplied function on each block
3) assemble all the results into a single output image and return

What do you mean by "formation" of blocks? Why do you want to "iterate 8 times and then stop and continue" ? Can you explain more clearly what problem you are trying to solve?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us