quickly reassign numbers in large array? [message #50102] |
Wed, 13 September 2006 16:41 |
vlk.astro
Messages: 11 Registered: March 2006
|
Junior Member |
|
|
Hello,
I am faced with a need to speed up a program which takes parts of an
image and reassigns values to them. I have so far gotten away with a
for-loop. Here is an example to illustrate what I am trying to do:
; make up an image
img=shift(dist(512),255,255)
image = 10*(img lt 10) + 20*(img gt 20 and img lt 30) + 50*(img gt 50
and img lt 100)
; this image has these labels and this many distinct regions
labels = image[uniq(image,sort(image))] ; =[0,10,20,50]
nregions=n_elements(labels) ; =4
; now I want to make a new image with new labels
newimage=0*image
for i=0,nregions-1 do
idx=where(image eq label[i])
newimage[idx]=i ; now newimage will have labels [0,1,2,3]
endfor
There has to be a faster, if not cleverer, way to do this reassignment.
Those multiple where()'s and the for-loop are fine for readability,
but are a major drain on the cpu and user patience when you have large
images and thousands of segments.
Any ideas?
Thanks,
Vinay
|
|
|