I would like to apply DCT to each element in a matrix

I have a code where i take every 8*8 block of a 512*512 image and find its average, and subtract it away, storing the result into a new matrix(64*64).
n = 1;
for i = 1:bs:row;
m = 1;
for j = 1:bs:col;
ave = mean(mean(I(i:i+bs-1, j:j+bs-1)));
A(i:i+bs-1, j:j+bs-1) = I(i:i+bs-1, j:j+bs-1) - ave;
DC(n,m) = ave;
m = m + 1;
end
n = n + 1;
end
But i would like to apply DCT transform to each of the 8*8 blocks, without applying it to the entire image at once, and then store my results in a new matrix of the same size as the original image matrix. I am not sure how to continue from here. Can someone please guide me through this?

 Accepted Answer

I'll assume you have a dct function of some kind and also MAT2TILES from the File Exchange
Icell = mat2tiles(I,[8,8]);
ave=Icell;
for i=1:numel(Icell)
ave{i}=mean(Icell{i}(:));
Icell{i}=dct(Icell{i}-ave{i});
end
DC=cell2mat(ave);
result=cell2mat(Icell);

11 Comments

Thank you very much Matt. I actually dont intend to include the average(ave) value but rather just do the DCT-iDCT on each block image. so i did something like this.
n = 1;
for i = 1:bs:row;
m = 1;
for j = 1:bs:col;
dct = mydct(I(i:i+bs-1, j:j+bs-1));
A(i:i+bs-1, j:j+bs-1) = I(i:i+bs-1, j:j+bs-1) - dct;
DC(n,m) = dct;
m = m + 1;
end
n = n + 1;
end
But i am getting an error of "too many output arguments" PS: Yes, i have a DCT code(mydct)
The error message should tell you the lines of code and functions responsible for the error.
yeah the line is dct = mydct(I(i:i+bs-1, j:j+bs-1));
Then your code for mydct is probably mis-written to return no outputs.
The dct code actually works well when run it independently. i have it here http://ge.tt/3pzrdkY just in case. Maybe i am not defining something properly.
Copy/paste the exact error messages
Your first line is
function[]=mydct(filename,n,m)
which means that function "mydct" does not return any outputs.
I would advise that when you write such a function, it is better to not have the function do its own image display (unless for debugging purposes) and to instead return values to the caller, letting the caller display if it wants.
??? Error using mydct
Too many out put arguments
Error in applydct at 22
dct = mydct(I(i:i+bs-1, j:j+bs-1));
Hi Matt, the above error messages are what i get.
Then as Walter indicated, i changed the first line to function I_rec = mydct(filename,n,m) and i got the following error messages ??? Error using strfind The filename or url must be a string
Error in imread at 282
if (strfind(filename, '://'))
Error in mydct at 12
I=imread(filename); % Reading the input image file and storing intensity
values in 2-D matrix I.
Error in applydct at 22
dct = mydct(I(i:i+bs-1, j:j+bs-1));
As the error message says, in imread(filename), the 'filename' variable must be a string. Did you check that it is? If so, how?
I am not sure how to do that.
Read the function you are using! It starts
%%LOSSY COMPRESSION-DECOMPRESSION USNIG DISCRETE COSINE TRANSFORM TECHNIQUE.
function[]=mydct(filename,n,m)
% "filename" is the string of characters including Image name and its
% extension.
% "n" denotes the number of bits per pixel.
% "m" denotes the number of most significant bits (MSB) of DCT Coefficients.
This documents that you must pass exactly 3 parameters to mydct(), the first of which must be a file name, and the other two must be scalars. But instead your code is passing in an array of data values as the first parameter, and you are not passing in the second or third parameter at all.

Sign in to comment.

More Answers (0)

Asked:

on 17 Feb 2013

Community Treasure Hunt

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

Start Hunting!