I need to use dct to compress an image.

2 views (last 30 days)
Bob
Bob on 15 Dec 2014
Commented: Image Analyst on 15 Dec 2014
The instructions are as follows: A MATLAB script for accomplishing this is to be found under “DCT and Image Compression" at: http://www.mathworks.com/help/images/discrete-cosine-transform.html?refresh=true The basic procedure is as follows: 1.) Break an image-pixel array down into smaller blocks, e.g. 8 8 (or 16 16) pixels, 2.) perform a discrete cosine transform (dct) on these smaller blocks, 3.) apply a mask, consisting of an 88 array of zeros and ones, using array multiplication on the above dct block in order to eliminate certain high-frequency components, 4.) use the inverse discrete cosine transform (idct) to reconstruct 8 8 image blocks, and 5.) reassemble the blocks into a new image. This HW problem involves the following steps: a.) Create your own function imcompress.m based on this script that will use 88 blocks, and will accept the name and type of an image together with a matrix representing the mask as input arguments2. b.) Use comments before each major step in the program, explaining exactly what it accomplishes, identifying user-defined functions and distinguishing these from built-in MATLAB functions.
Note: 2The given script works only for 2D image arrays whose size is a multiple of the basic block size [8 8]. Hence, you will need to process each RGB color plane separately, e.g. using a for-loop, and to trim the input array down to the nearest multiple of 8, e.g. using a command such as new = old-rem(old, 8).
Here is what I have so far.
function imcompress(fig, fmt, mask ) P = imread(strcat(fig, '.', fmt)); P = im2double(P); siz = size(P) - rem(size(P), 8); for c = 1:3 I = P(1:siz(1), 1:siz(2), c); Q = dctmtx(8); fund = @(R) Q * R.data * Q'; z = blockproc(I, [8 8], fund); %funm = mask .* R z2 = blockproc(z, [8 8], @(R) mask .* R.data); funid = @(R) Q' * R.data * Q; I2 = blockproc(z2, [8 8], funid); P2 = I2(1:siz(1), 1:siz(2), c); end imshow(P2), figure, imshow(P) end
I am getting the error
>> imcompress2('maid','tiff',mymask(8)) Index exceeds matrix dimensions.
Error in imcompress2 (line 14) P2 = I2(1:siz(1), 1:siz(2), c);
and I'm not sure why. Any help would be amazing. the program runs if i comment that line, but then i don't think it's assembled into a new image, because it is a grayscale image that is created.
  2 Comments
Bob
Bob on 15 Dec 2014
Sorry, just realized the code was all bunched together
function imcompress(fig, fmt, mask )
P = imread(strcat(fig, '.', fmt));
P = im2double(P);
siz = size(P) - rem(size(P), 8);
for c = 1:3
I = P(1:siz(1), 1:siz(2), c);
Q = dctmtx(8);
fund = @(R) Q * R.data * Q';
z = blockproc(I, [8 8], fund);
%funm = mask .* R
z2 = blockproc(z, [8 8], @(R) mask .* R.data);
funid = @(R) Q' * R.data * Q;
I2 = blockproc(z2, [8 8], funid);
P2 = I2(1:siz(1), 1:siz(2), c);
end
imshow(P2),
figure, imshow(P)
end

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!