2

I need to create a 32x32 sliding window on an image Z. I then need to check the mean intensity of each window on the image.

Can I use: n=[32,32] h = fspecial('average', n); filter2(h, img)

N = 32;
info = repmat(struct, ceil(size(Z, 1) / N), ceil(size(Z, 2) / N)); 
for row = 1:N:size(Z, 1)%loop through each pixel in the image matrix
   for col = 1:N:size(Z, 2)
     r = (row - 1) / N + 1;
     c = (col - 1) / N + 1;
     imgWindow = Z(row:min(end,row+N-1), col:min(end,col+N-1));
    average = mean(imgWindow(:)) %calculate the mean intensity of pixels within each window
   end
end

However this only creates 12x30. Can anyone spot where I have gone wrong?

user1853871
  • 249
  • 5
  • 21
  • What's that magic word `struct` doing alongside `repmat`? It may not sort out the issue at hand, but got curious about it. – Divakar Apr 17 '14 at 17:00
  • 1
    If you type `size(Z)`, what do you get? – tashuhka Apr 17 '14 at 17:06
  • In the event that you have the image processing toolbox, you might want to use the ready made sliding filter functions it has. – Cape Code Apr 17 '14 at 17:09
  • size(Z) gives <364x350> – user1853871 Apr 17 '14 at 17:12
  • @Jigg I do have the image processing toolbox, do you mean the blocproc function? – user1853871 Apr 17 '14 at 17:15
  • @user1853871 no, `blockproc` is for block processing, look more into `nfilter` or the specific median or standard deviation filter `medfilt2`, `stdfilt`, etc. – Cape Code Apr 17 '14 at 17:25
  • Pick something appropriate in this list: http://www.mathworks.com/help/images/linear-filtering.html – Cape Code Apr 17 '14 at 17:25
  • 1
    You can also get inspiration from some of my implementations, `nfilter` here: http://stackoverflow.com/a/22987604/2777181, `filter2` here: http://stackoverflow.com/a/22914519/2777181 – Cape Code Apr 17 '14 at 17:30
  • Thanks :) ! I need to calculate the mean intensity of each window so should I use imgWindow = nlfilter(YourImage,[3 3],fun); and fun would be a function that finds the mean intensity? – user1853871 Apr 17 '14 at 17:36
  • 1
    If it's the mean you want, look at this: http://stackoverflow.com/a/1738103/2777181 – Cape Code Apr 17 '14 at 17:48
  • I dont understand how to use this. Do this h = fspecial('average', n); filter2(h, img); return the mean intensity for each window ? – user1853871 Apr 17 '14 at 18:01
  • Yes, exactly. Replace `n` by 32 and the output of `filter2(h, Z)` is what you want. – Cape Code Apr 17 '14 at 18:02
  • @Jigg I have tried this h = fspecial('average', 32); newimg=filter2(h, Z); figure(2); imshow(newimg); but when ever I do imshow(newimg) it does not show the window it shows the whole image. Am i doing something wrong? – user1853871 Apr 18 '14 at 12:27
  • Yes, it is intended to filter the whole image. That's the point of a moving filter. If you want a subset of the image just use indexing. – Cape Code Apr 18 '14 at 13:21
  • Oh I do not need to filter the whole image. I just need to find an ROI in the image by placing windows over it and finding the window that contains the ROI by extracting features from it, starting with obtaining the average pixel value for each single window in the image – user1853871 Apr 18 '14 at 13:35
  • Just pick a pixel at the right location in the filtered image and it will give you the mean in the respective neighborhood. You could make your question clearer with an example image and desired output. – Cape Code Apr 18 '14 at 20:53

2 Answers2

2

Your image is 364x350, and the window size is 32x32. What happens is this:

enter image description here

Notice in the last column the windows are 32x30, in the last row the windows are 12x32, and the last window (bottom-right) is 12x30. That's the last to be calculated, and why you're getting that value when the code stops running.

I see three options here:

  1. Fill the last column with two columns of pixels and the last row with 20 rows of pixels (fill with zeros, perhaps?)
  2. Discard the last column and the last row.
  3. Change the window size to MxN, where M is a divisor of 364 and N is a divisor of 350.
Rafael Monteiro
  • 4,509
  • 1
  • 16
  • 28
  • I think one of the first two options would be best as I will be using this code on different images of different sizes. However if I use one of these 2 options will this affect the mean intensity value of the window? – user1853871 Apr 17 '14 at 18:59
  • On option one, yes. Couldn't you use the mean intensity of those windows which are not 32x32? – Rafael Monteiro Apr 17 '14 at 20:05
  • 1
    I would stick to the second option, but it really depends on your application. – Rafael Monteiro Apr 17 '14 at 22:32
1

Initialize before the for loop

e=1;

and then start counter in for loop and put this code

imgWindow (:,:,e)= Z(row:min(end,row+N-1), col:min(end,col+N-1));

instead of

imgWindow = Z(row:min(end,row+N-1), col:min(end,col+N-1));

Hope it will help you. Regards

old-ufo
  • 2,799
  • 2
  • 28
  • 40
Rehan
  • 11
  • 1