-1
I=imread('ft.jpg'); 
 [a b]=size(I);
 figure;imshow(I);
j=rgb2ycbcr(I);
[m n]=size(j);
figure;
imshow(j);
ca=mat2cell(j,8*ones(1,size(j,1)/8),8*ones(1,size(j,2)/8),3);
p = 1;
figure;
figure;
X=ones(m,n,8,8);
for c=1:size(ca,1)
    for r=1:size(ca,2)


 temp=zeros(8,8,'uint8');
   temp(:,:)=X(c,r,:,:);
   temp=temp-128;
   temp=dct2(temp);

   subplot(size(ca,1),size(ca,2),temp(:,:)); %// Change
   imshow(ca{c,r});
   p=p+1;
    end
end

the error is: Error using ==> subplot at 309 Illegal plot number.

Error in ==> project at 22 subplot(size(ca,1),size(ca,2),temp(:,:)); %// Change

matlabuser
  • 101
  • 1
  • 11

1 Answers1

2

That's because you're not calling subplot properly. It needs p as the parameter for the third argument, not temp. p determines which slot you want to place your figure in. You putting in a doublevector as the third parameter makes no sense. Also, ca contains 8 x 8 pixel blocks, and you'd like to show the DCT of each block. Your current code does not do this. Actually, X is what you're trying to find the DCT of, and it's all ones.... doesn't make much sense.

You probably want to show the DCT of each block inside the figure as well, not ca.... and so you need to do this:

for c=1:size(ca,1)
    for r=1:size(ca,2)

        temp = double(ca{c,r}); %// Change - cast to double for precision
        temp=temp-128;
        temp=dct2(temp);

        subplot(size(ca,1),size(ca,2),p); %// Change
        imshow(temp,[]); %// Change here too
        p=p+1;
     end
end

Note that I did imshow(temp,[]); so that we can contrast stretch the block so that the minimum value gets mapped to black while the maximum value gets mapped to white. Everything else is a shade of gray in between. Be advised that this doesn't change the image itself. It only changes it for display.

Suggestion

You should read up on my post on how subplot works here: How does subplot work and what is the difference between subplot(121) and subplot(1,2,1) in MATLAB?

Once you read through this post, you'll have a better understanding of how each parameter works for subplot, and how you can display graphs / images in a figure.

Minor Note

Looks like you took that code from this post but you didn't copy and paste properly :) That %// Change comment, as well as the variable name ca looked oddly familiar.

Community
  • 1
  • 1
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • but i need to see the dct2 applied on the blocks.how can i do this? – matlabuser Nov 26 '14 at 16:30
  • 1
    @matlabuser , if you find the answers (like in the other post) useful for you, don't forget to accept them. Its the small tick next to the number next to the answer itself. – Ander Biguri Nov 26 '14 at 16:32
  • @matlabuser - You weren't showing the DCT of each block properly. I've modified my post. – rayryeng Nov 26 '14 at 16:36
  • @matlabuser - Made a small typo in the `for` loops with getting a block. Please check the new edits. – rayryeng Nov 26 '14 at 16:44