1

It is basic but I don't know how to do it.. I want to take 8x8 window in an image and further subdivide that window into 2x2 and then have to find their mean values. After doing this, I have to compare this mean value matrix with a threshold mean(mean value of whole image or mean value of that 8x8 window). If the elements greater than or equal to the threshold value, it should to be assigned 1 if not 0(converting it to a binary image). After all the operations a 256x256 image would become 128x128. Compression will be achieved. I have written this below code for this purpose.

After doing the all the operations, I don't know how to store the result (after comparing with the threshold value) after each loop. I need to do this to construct the compressed image. Please help. My codes take sometime to run. I think this can be coded more simply. Please explain me how to slide a window in an image and store how can I store the processed values to construct the output image.. so I can have a clear understanding ..Pls provide any materials to learn image processing basics like sliding a window and others things in MATLAB

My Code is here:

%In this code only the final window values are stored.. values of previous windows are not stored. I need to store the values of all the windows to get the compressed image. Help me.

I=imread('C:\Users\Prem\Documents\MATLAB\mandrill.jpg');
G=rgb2gray(I);
J=imresize(G,[256 256]);
thr=mean(J(:));
[m,n]=size(J); % Reading the size of the image
for i=1:m 
    for j=1:n

      P=J(i:(i+7),j:(j+7)); % Reading 8x8 window

        % Sub dividing the 8 x 8 window into four 4x4 sub windows 

    tl = P(1:4, 1:4); % top left sub-window
    tr = P(1:4, 5:8); % top right sub-window
    bl = P(5:8, 1:4); % bottom left sub-window
    br = P(5:8, 5:8); % bottom right sub-window

     % Sub dividing the 4 x 4 window into four 2x2 sub windows 

    newtl_1 = tl(1:2, 1:2); % top left sub-window
    newtr_1 = tl(1:2, 3:4); % top right sub-window
    newbl_1 = tl(3:4, 1:2); % bottom left sub-window
    newbr_1 = tl(3:4, 3:4); % bottom right sub-window

     % Sub dividing the 4 x 4 window into four 2x2 sub windows 

    newtl_2 = tr(1:2, 1:2); % top left sub-window
    newtr_2 = tr(1:2, 3:4); % top right sub-window
    newbl_2 = tr(3:4, 1:2); % bottom left sub-window
    newbr_2 = tr(3:4, 3:4); % bottom right sub-window

     % Sub dividing the 4 x 4 window into four 2x2 sub windows 

    newtl_3 = bl(1:2, 1:2); % top left sub-window
    newtr_3 = bl(1:2, 3:4); % top right sub-window
    newbl_3 = bl(3:4, 1:2); % bottom left sub-window
    newbr_3 = bl(3:4, 3:4); % bottom right sub-window

     % Sub dividing the 4 x 4 window into four 2x2 sub windows 

    newtl_4 = br(1:2, 1:2); % top left sub-window
    newtr_4 = br(1:2, 3:4); % top right sub-window
    newbl_4 = br(3:4, 1:2); % bottom left sub-window
    newbr_4 = br(3:4, 3:4); % bottom right sub-window

      % mean values of the four sub windows

        m1=mean(newtl_1(:));
        m2=mean(newtr_1(:));
        m3=mean(newbl_1(:));
        m4=mean(newbr_1(:));

         % mean values of the four sub windows

        m5=mean(newtl_2(:));
        m6=mean(newtr_2(:));
        m7=mean(newbl_2(:));
        m8=mean(newbr_2(:));

         % mean values of the four sub windows

        m9=mean(newtl_3(:));
        m10=mean(newtr_3(:));
        m11=mean(newbl_3(:));
        m12=mean(newbr_3(:));

        % mean values of the four sub windows

        m13=mean(newtl_4(:));
        m14=mean(newtr_4(:));
        m15=mean(newbl_4(:));
        m16=mean(newbr_4(:));

     M=[m1 m2 m3 m4; m5 m6 m7 m8; m9 m10 m11 m12; m13 m14 m15 m16];

OutputCompressedImage=M>thr;

    end
end
    imshow(OutputCompressedImage)

%In this code only the final window values are stored.. values of previous windows are not stored. I need to store the values of all the windows to get the compressed image. Help me.

1 Answer 1

0

you are overwriting your OutputCompressedImage every loop iteration, that's why you only get the the final window value. One way to do it is to use a three dimensional array to save your data, each slice representing one window.

your loop can be modified the following way:

s=1;
for i=1:m 
    for j=1:n
      ****keep original code****
      OutputCompressedImage(:,:,s)=M>thr;
      s=s+1;
    end
end

The dimension of OutputCompressedImage shoud have m*n slices.

P.S. for moving a window across you can consider using convolution. http://www.mathworks.com/help/matlab/ref/conv2.html

4
  • I did as you have said.. I get following error.. Error using imageDisplayValidateParams>validateCData (line 117) Unsupported dimension. Error in imageDisplayValidateParams (line 31) common_args.CData = validateCData(common_args.CData,image_type); Error in imageDisplayParseInputs (line 79) common_args = imageDisplayValidateParams(common_args); Error in imshow (line 220) [common_args,specific_args] = ... Error in meanthr (line 81) imshow(OutputCompressedImage);
    – Premnath D
    Oct 2, 2013 at 18:04
  • yeah, that's saying you can not imshow a 3D matrix, cause images are 2 dimensional. if you do imshow(OutputCompressedImage(:,:,i)), it will show you the compressed image of the i th window. so it's upto you to figure out how to put all the windows back together and form a complete image.
    – Cici
    Oct 2, 2013 at 18:29
  • you need to tile those together, maybe with a loop.
    – Cici
    Oct 2, 2013 at 18:45
  • Please help me do that.. I am very new to matlab.. Hope you will save my day. I am trying reshape function but getting error..
    – Premnath D
    Oct 2, 2013 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.