0

I want to segment image of size M x N into overlapped blocks with size k x k to generate (M-k+1) x (N-k+1) blocks. I have to find mean and variance of all the extracted/segmented blocks.

its not only segmentation, it include operations(mean and variance) on blocks within loop, my primary concern is to improve computational efficiency.

I have done the following coding in MATLAB:

A=imread('lena.jpg');
[M N]=size(A)
N=N/3
n=16;
m=16;
bl_mean=[];
bl_variance=[];
for i=1:M-m
    for j=1:N-n
        %[ii,jj]=ndgrid(n,m);
        bl=A(i:(i+m-1),j:(j+n-1));
        [bl_mean]=[bl_mean mean(mean(bl))];
        [bl_variance]=[bl_variance var(var(double(bl)))];
        B=sort(bl_mean);
        B1=sort(bl_variance);
    end
end

It was working fine for smaller image but for bigger image it keeps on processing for a long long time.

Any alternate or right way to find blocks in quick manner?

N. Kanwal
  • 11
  • 2
  • you are recomputing `B` and `B1` each time per iteration but doing nothing with it ever. – Ander Biguri Jun 20 '17 at 17:56
  • that i will use in next steps of algorithm – N. Kanwal Jun 20 '17 at 17:57
  • You obtained the wrong information from my comment. In the end of this code, you will only have `B` and `B1` from `i==M-m` and `j==N-n` – Ander Biguri Jun 20 '17 at 18:01
  • 2
    Perform an `im2col` procedure such that each block gets transformed into a separate column and so you will have a 2D matrix of columns - one column per block. Then compute the mean and standard deviation of each column individually. Refer to the marked duplicate to perform the transformation. After, use `mean` and `std` and ensure you operate along each column individually. – rayryeng Jun 20 '17 at 18:01
  • thanks for the help. i have used `im2col` function and it worked fine for the small image blocks but for an image of size _512X512_ and block size like _16X16_ it generate an error of **out of memory** its working fine with smaller block size like _3X3_ etc. any clue @rayryeng – N. Kanwal Jun 26 '17 at 17:50

0 Answers0