IDL beginner writes:
> I am new IDL user and I need your help in the following question:
>
> I have an image with values between 1 and 8 and I need to create a 5x5
> window that will move step by step over each pixel of the image. The
> window over each pixel will count how many pixels around the center
> pixel have the value 1, 2, 3 ..., or 8.
>
> I do not know how to do that and how can I deal with the pixels on the
> boarders of the image because some of the window elements will fall
> outside the image.
How about something like this. First, create an appropriate
data set. (Small enough to check results.)
array = Fix(Scale_Vector(Randomu(-3L, 10, 10), 1, 9))
Now, the answers will be in a 10x10x8 array, as there will
be a 2D answer array for each value, 1 through 8.
s = Size(array, /DIMENSIONS)
answers = LonArr(s[0], s[1], 8)
Partition the array into the 8 values, and find the indices
representing these 8 values:
cutoff = [1,2,3,4,5,6,7,8]
h = Histogram(Value_Locate(cutoff, array) + 2, REVERSE_INDICES=ri)
We will need a mask to select each of the 8 values, in turn.
mask = BytArr(s[0], s[1])
Now, get the answers and put them in the answers array. Note
that ONLY the pixels surrounding the center pixel (as the
requirements stated) are counted. The center pixel is not
counted.
FOR j=0L,7 DO BEGIN
mask = mask * 0B
kernel = Replicate(1.0/(j+1), 5, 5)
kernel[2,2] = 0 ; Center pixel not counted.
mask[ri[ri[j]:ri[j+1]-1]] = 1
answers[0,0,j] = Convol(Float(array) * mask, kernel, /EDGE_ZERO)
ENDFOR
The entire program (as a main level program) is here:
;*********************************************************** ********
array = Fix(Scale_Vector(Randomu(-3L, 10, 10), 1, 9))
s = Size(array, /DIMENSIONS)
answers = LonArr(s[0], s[1], 8)
cutoff = [1,2,3,4,5,6,7,8]
h = Histogram(Value_Locate(cutoff, array) + 2, REVERSE_INDICES=ri)
mask = BytArr(s[0], s[1])
FOR j=0L,7 DO BEGIN
mask = mask * 0B
kernel = Replicate(1.0/(j+1), 5, 5)
kernel[2,2] = 0 ; Center pixel not counted.
mask[ri[ri[j]:ri[j+1]-1]] = 1
answers[0,0,j] = Convol(Float(array) * mask, kernel, /EDGE_ZERO)
ENDFOR
END
;*********************************************************** ********
How does it work? Save it as "example.pro".
IDL> .run example
IDL> Print, array, FORMAT='(10I3)'
8 5 7 5 1 8 1 2 2 8
8 8 7 2 2 7 1 6 1 1
6 1 2 1 3 3 6 6 3 4
5 2 8 6 5 7 7 4 5 8
6 7 2 3 7 7 1 6 2 4
4 4 8 4 6 5 3 5 2 2
7 8 4 3 2 5 7 4 1 2
7 2 6 2 1 8 5 2 7 4
7 3 4 3 5 8 2 2 6 3
5 4 5 6 9 3 1 5 1 5
How many ones are there?
IDL> Print, answers[*,*,0], FORMAT='(10I3)'
1 2 3 3 3 4 3 4 4 2
1 2 3 3 4 4 3 4 3 1
1 1 3 2 5 5 5 5 5 2
1 2 2 2 3 3 3 4 4 2
1 2 2 2 2 2 1 2 2 1
0 0 1 1 2 2 3 2 2 1
0 0 1 1 2 2 3 2 1 1
0 0 1 1 1 2 4 3 3 2
0 0 1 1 2 2 4 3 3 2
0 0 1 1 2 2 2 2 1 1
How many fives are there:
IDL> Print, answers[*,*,4], FORMAT='(10I3)'
1 1 2 1 1 1 0 0 0 0
2 3 4 3 2 2 2 1 1 1
2 3 4 3 2 2 2 1 1 1
0 1 2 2 1 3 4 3 1 2
1 1 2 3 3 4 5 4 2 2
1 1 2 3 4 4 6 4 3 2
0 0 1 3 4 4 5 4 2 1
2 2 3 4 5 6 5 6 4 3
2 2 3 3 3 4 4 4 3 2
1 2 2 2 3 3 3 2 3 1
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|