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:
deviding an image with dwt

Subject: deviding an image with dwt

From: Twilighter

Date: 11 Dec, 2010 07:44:05

Message: 1 of 8

i have a 512x512 Lena.bmp image

i need to divide it into 8x8 block & perform a DWT on each block, there is an example in the Help using blkproc with dct2

>> I = imread('cameraman.tif');
>> fun = @dct2;
>> J = blkproc(I,[8 8],fun);
>> imagesc(J), colormap(hot)

but i couldnt apply it with dwt2 function
is there other way to perform this, and at the same time be able know the position & coordinate of each block

Subject: deviding an image with dwt

From: Wayne King

Date: 11 Dec, 2010 13:08:04

Message: 2 of 8

"Twilighter " <mscmatlab@gmail.com> wrote in message <idva45$a85$1@fred.mathworks.com>...
> i have a 512x512 Lena.bmp image
>
> i need to divide it into 8x8 block & perform a DWT on each block, there is an example in the Help using blkproc with dct2
>
> >> I = imread('cameraman.tif');
> >> fun = @dct2;
> >> J = blkproc(I,[8 8],fun);
> >> imagesc(J), colormap(hot)
>
> but i couldnt apply it with dwt2 function
> is there other way to perform this, and at the same time be able know the position & coordinate of each block

Hi, the problem is that dwt2, unlike dct2, requires more than one input argument.

I = imread('cameraman.tif');
fun = @(block_dwt2) dwt2(double(I),'haar');
J = blkproc(I,[8,8],fun);

Wayne

Subject: deviding an image with dwt

From: Twilighter

Date: 11 Dec, 2010 16:02:04

Message: 3 of 8


> Hi, the problem is that dwt2, unlike dct2, requires more than one input argument.
>
> I = imread('cameraman.tif');
> fun = @(block_dwt2) dwt2(double(I),'haar');
> J = blkproc(I,[8,8],fun);
>
> Wayne

thanx it helped me a lot, i found another function

C = mat2cell(I, [64 64 64 64 64 64 64 64],[64 64 64 64 64 64 64 64]);
this one can show me clearly all my block so i can know the position and location

but i again have problem with function assignment

this gave me error
J = cellfun(@(block_dwt2) dwt2(double(C),'haar'), C,'UniformOutput',false);

Subject: deviding an image with dwt

From: Brendan Hannigan

Date: 13 Dec, 2010 19:06:06

Message: 4 of 8

"Twilighter " <mscmatlab@gmail.com> wrote in message <ie079s$3bs$1@fred.mathworks.com>...
>
> > Hi, the problem is that dwt2, unlike dct2, requires more than one input argument.
> >
> > I = imread('cameraman.tif');
> > fun = @(block_dwt2) dwt2(double(I),'haar');
> > J = blkproc(I,[8,8],fun);
> >
> > Wayne
>
> thanx it helped me a lot, i found another function
>
> C = mat2cell(I, [64 64 64 64 64 64 64 64],[64 64 64 64 64 64 64 64]);
> this one can show me clearly all my block so i can know the position and location
>
> but i again have problem with function assignment
>
> this gave me error
> J = cellfun(@(block_dwt2) dwt2(double(C),'haar'), C,'UniformOutput',false);

Hi Wayne and Twilighter,

I'm not sure if they are typos in your code or if you guys are not quite understanding how to use these block processing routines.

When defining an anonymous function that will take data and run another function, you "inner" function usually operates on the input argument of the anonymous function, like this:

fun = @(arg1) otherFun(arg1);

In both of your examples, you are defining an anonymous function that takes an input argument, "block_dwt2", but then you are not doing anything with it. Instead you are calling the inner function, DWT2, and passing the the full data set.

The point of the anonymous function is to take a small block of data and only run the user function on that small block. Here is how you would do that, first with BLKPROC and then with CELLFUN:

===============
I = double(imread('cameraman.tif'));
block_size = 8;

% with blkproc
fun = @(block) dwt2(block,'haar');
J1 = blkproc(I,[block_size block_size],fun);

% with cellfun
C = mat2cell(I, repmat(block_size,[1 size(I,1)/block_size]),...
    repmat(block_size,[1 size(I,2)/block_size]));
J2_mat = cellfun(@(block) dwt2(block,'haar'),C,'UniformOutput',false);
J2 = cell2mat(J2_mat);

isequal(J1,J2)

ans =
     1
====================
You can see both methods produce the same results.

Recently I've seen a few people suggesting this mat2cell/cellfun/cell2mat workflow as a recommended way to do block processing. It is true that in some cases you can see a slight performance improvement vs. BLKPROC (or vs. BLOCKPROC), but I'm not crazy about it. BLKPROC/BLOCKPROC both have a lot of nice features build in like edge padding, over lapping blocks, etc.

Plus, just look at the code. There's something to be said for code readability and self-documentation. Anyway, just my 2 cents :).

hope this helps,
-brendan

Subject: deviding an image with dwt

From: Twilighter

Date: 19 Dec, 2010 13:43:06

Message: 5 of 8

thank you Brendan Hannigan for your answer it helped me as i'm just beginner in Matlab in general

here i need to ask one more question

when i apply my dwt2 function i get the HH HL LH LL coefficients matrix, and every matrix is half the size of the trating matrix

but the final result J2 is just the same size as the starting matrix ( original image) where is my HH HL LH LL coefficients in this matrix ? what is the output

Subject: deviding an image with dwt

From: Brendan Hannigan

Date: 20 Dec, 2010 15:40:26

Message: 6 of 8

"Twilighter " <mscmatlab@gmail.com> wrote in message <iel25a$pj5$1@fred.mathworks.com>...
> thank you Brendan Hannigan for your answer it helped me as i'm just beginner in Matlab in general
>
> here i need to ask one more question
>
> when i apply my dwt2 function i get the HH HL LH LL coefficients matrix, and every matrix is half the size of the trating matrix
>
> but the final result J2 is just the same size as the starting matrix ( original image) where is my HH HL LH LL coefficients in this matrix ? what is the output

aha... Ok I'm going to have to eat my words a bit here. I didn't really look much into your function DWT2 and notice that it has multiple output arguments. BLKPROC is really only appropriate for functions which return 1 output argument.

What's happening is that these functions are collecting the first output argument, and discarding the other 3. In order to block process your image and collect all of the output arguments you should use the CELLFUN solution above. You just need to modify it such that it gets all 4 output args like this:

[cA,cH,cV,cD] = cellfun(@(block) dwt2(block,'haar'),C,'UniformOutput',false);
cA_final = cell2mat(cA);
cH_final = cell2mat(cH);
cV_final = cell2mat(cV);
cD_final = cell2mat(cD);

Now CELLFUN will return all of the 1st output arguments, blockwise, in cA, all of the 2nd output arguments in cH, etc, etc. You need to call CELL2MAT on each of the return args to convert them from cell arrays back into matrices.

hope this helps,
-brendan

Subject: deviding an image with dwt

From: Twilighter

Date: 21 Dec, 2010 11:28:11

Message: 7 of 8

Thank you Brendan it worked :)

Subject: deviding an image with dwt

From: Brendan Hannigan

Date: 21 Dec, 2010 16:39:05

Message: 8 of 8

"Twilighter " <mscmatlab@gmail.com> wrote in message <ieq30b$1v3$1@fred.mathworks.com>...
> Thank you Brendan it worked :)

no prob, good luck!

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