using discrete cosine transform to break 16x16 to 8x8 blocks

I have to break a image-pixel array down to 8x8 blocks. Here is my code
function imcompress(fig,fmt,mask)
p = imread(fig,fmt);
p = im2double(p);
siz = size(p)-rem(size(p),8);
for c = 1:3
i = j(1:siz(1),1:siz(2),c)
end
Q = dctmtx(8)
fund = @(R) Q*R*Q'
B = blockproc(i, [8 8],fund);
B = blockproc(B, [8 8],@(block) mask.*block.data);
fundid = @(R) Q'*R*Q
I2 = blockproc(B, [8 8],fundid)
I2 = p2(1:siz(1),1:siz(2),c)
imshow(i), figure, imshow(I2)
And I also got my mask function
function mask = mymask(n)
mask=zeros(8);
matr= fliplr(triu(ones(n)));
mask(1:n,1:n)=matr;
end
When I test the picture,which called picture.jpg, I typed imcompress('picture','jpg',mymask(8)) it said Error in imcompress (line 6) ,i = j(1:size(1),1:size(2),c) I am not sure what's wrong with my code. Can someone help me up, and point out some others error codes?

3 Comments

Jarvan - what is j in your line of code
i = j(1:siz(1),1:siz(2),c)
In fact, what are you trying to do with i as it gets updated at each iteration of the for loop and so overwrites the values from previous iterations?
As an aside, you should avoid naming variables i and j as these also correspond to the representation of the imaginary number.
there are a new size, which is siz = size(p)-rem(size(p),8); i have use the new size to break down to 8x8 blocks. I know the low-frequency of the picture is left-up corner, like
1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
i am using a for-loop to trim the input array down to the nearest multiple of 8

Sign in to comment.

 Accepted Answer

Jarvan - you mention that the error message is
Error in imcompress (line 6) ,i = j(1:size(1),1:size(2),c)
but you haven't included all of the error message...but it seems clear what the error is. You have not defined j nor is it an input parameter. You haven't described what you intend to do with this for loop and so you need to clear up the following problems with it
  1. What is j?
  2. Why is the code calculating size(1) and size(2), or should this be siz(1) and siz(2) instead? And if so, you may want to consider renaming this siz variable.
  3. What is the purpose of i?

5 Comments

I need to process each RGB color plane separately,so I tried to use a for-loop, and to trim the input array down to the nearest multiple of 8, therefore I got
siz = size(p)-rem(size(p),8);
for c = 1:3
i = j(1:siz(1),1:siz(2),c)
end
I think it should be p and put the rest code into loop like
function imcompress(fig,fmt,mask)
p = imread(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*Q'
B = blockproc(I, [8 8],fund);
B = blockproc(B, [8 8],@(block) mask.*block.data);
fundid = @(R) Q'*R*Q
I2 = blockproc(B, [8 8],fundid)
I2 = p2(1:siz(1),1:siz(2),c)
end
imshow(i), figure, imshow(I2)
end
I try to run it on command and display with lots of number, but end up with lots error with my code
Unfortuantely, I don't have the Image Processing Toolbox, so can't run your code. You need to solve this problem by stepping through it with the debugger so that you can see where the errors are and convince yourself that each line of code is necessary and is doing what you intend. For example, what is p2 that is referenced at the line
I2 = p2(1:siz(1),1:siz(2),c)
And note how you reference i at the next to last line even though this local variable no longer exists.
To save the result of each RGB channel, just do something like the following prior to entering the for loop
outImg = [];
for c = 1:3
% do stuff from above
% save I2 to outing
outImg(:,:,c) = I2;
end
imshow(p);
figure;
imshow(outImg);
And see also Image Analyst's answer as that will point you in the right direction.
thank you let me try it again

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 10 Dec 2014

Commented:

on 16 Dec 2014

Community Treasure Hunt

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

Start Hunting!