Pregunta

I am doing a basic image compression. 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. I have written this below code for this purpose. But i am stuck in the 1st step itself. it shows Error "Index exceeds matrix dimensions" in this line P=J(i:(i+7),j:(j+7)); Please help and i am unsure how to compare and assign 0 & 1 to the final matrix. I tried X=bsxfun(@ge,M,thr).. it is not working ..and my code seems to be big.. Am I doing it in write way? please suggest. I think it can be made simpler. I am new to matlab, please help me learn. Here is my code:

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

      % median values of the four sub windows

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

         % median values of the four sub windows

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

         % median values of the four sub windows

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

        % median 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];

     %X=bsxfun(@ge,M,thr)
    end
end
    imshow(M)
¿Fue útil?

Solución

  1. when i in your loop goes from 1 to the size of the image, i+7 will correspond to a value beyond the size of the image, eventually. Adjust the limits of the loop (to imageSize-7), or pad the image before running your code.

  2. If you want to create an array that has ones wherever it is greater or equal a threshold, and zeros otherwise, you write X = M>thr. It's a nice feature of Matlab.

  3. Note that mean takes the average, median takes the median.

Otros consejos

Finally, I did it using blockproc function in matlab.. Click on the below link to see the code.

How to store data out of the loop in MATLAB- Image Compression

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top