Create a scan window in MATLAB

Asked

Viewed 237 times

0

I’m trying to create in Matlab a script that makes a sub-matrix (search window 3 x 3) go through a larger matrix looking for minimum values for each 3 x 3 cell chunk analyzed. When the script finds this minimum value cell, it places it as the center of the sub-matrix and searches again for the minimum value and so on until a cell indicated in the larger matrix. I tried to work with loops, but as I’m beginner I’m not getting.

clc
clear
alt= imread('foto_o1.tif')
[l c]= size(alt)
a = 1
b = 7
d= 1
while a < 416
  a= a+1 
  while b < 416 
    b= b+1 
    d= d+1 
    d= alt(1:7, a:b)
  end
end
  • Hugo, welcome to Stack Overflow! What have you tried so far? Questions should show a minimum of research effort. Look for a [tour] and read the guide [Ask]

  • Thanks for the gmsantos tips, there is an initial attempt to move a submatriz over a larger matrix, more specifically a 3 x 3 matrix over a 244 x 416.clc clear alt= imread('foto_o1.tif') [l c]= size(alt) a= 1 b= 7 d= 1 while a < 416 a= a+1 while b < 416 b= b+1 d= d+1 d= alt(1:7, a:b) end end

1 answer

1

It wasn’t very clear, but I’ll try to help, let’s go for a simple example, let’s create a matrix 6x6 to exemplify:

A = [1  2  3  4 5  6;  7 8 9 10 11 12; 13 14 15 16 17 18; 19 20 21 22 23 24; 25 26 27 28 29 30; 31 32 33 34 35 36]

OK This will give you the following matrix:

A =

    1    2    3    4    5    6
    7    8    9   10   11   12
   13   14   15   16   17   18
   19   20   21   22   23   24
   25   26   27   28   29   30
   31   32   33   34   35   36

Perfect following your logic (if I understand correctly) you need to walk applying a window 3x3, this means that you would need to extract the following sub-matrices from the main matrix:

Its first sub-matrix would be:

    1    2    3
    7    8    9
   13   14   15

Second:

    4    5    6
   10   11   12
   16   17   18

Third:

   19   20   21
   25   26   27
   31   32   33

Fourth:

   22   23   24
   28   29   30
   34   35   36

The logic to do this is to actually walk through a loop and go incrementing the row and column by 3:

A = [1  2  3  4 5  6;  7 8 9 10 11 12; 13 14 15 16 17 18; 19 20 21 22 23 24; 25 26 27 28 29 30; 31 32 33 34 35 36]

[l, c] = size(A)

c1=1;
c2=3;

while c1 <= c

    l1 = 1;
    l2 = 3;

    while l1 <= l

       D = A(c1:c2,  l1:l2)
       l1 = l1 +3;
       l2 = l2+3;

    end
    c1 = c1+3;
    c2 = c2 +3;
end

The above code prints the sub-matrices shown in this example.

To extract the minimum value within each sub-matrix you can use the function min of Matlab, in this case add after D = A(c1:c2, l1:l2) the following line minimo =min(D(:))

  • Thank you so much for the help ederwander, this is exactly what I need. I made a mess when using the while and why I was not getting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.