comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Problems with ERODE and DILATE functions
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Problems with ERODE and DILATE functions [message #73589] Thu, 18 November 2010 00:55
Oriol Güell Riera is currently offline  Oriol Güell Riera
Messages: 7
Registered: September 2010
Junior Member
On 17 nov, 21:05, Guillermo <guillermo.castilla.castell...@gmail.com>
wrote:
> Hola Oriol,
>
> I haven't read thoroughly your post or your code, but since you say
> that "erode" and "dilate" modify separate your ellipses sometimes, it
> may be the case that then you have too many individual regions and
> your code gets too slow. Instead of using WHERE and SEARCH_2D, you can
> use LABEL_REGION and HISTOGRAM to locate individual regions in a much
> more efficient way:
>
> hst= histogram(label_region(slice, min=1, omax=nreg, rev=r)
> for j=0L, nreg-1 do begin
>   region= r[r[j]:r[j+1]-1]
>   ...
>   numpts= hst[j]
>   ...
> endfor
>
> Also, instead of dilate and erode, you could simply use the MEDIAN
> function to remove small regions or holes within small regions
>
> Bona sort!
>
> Guillermo

Hola Guillermo

Thank you very much for your suggestions!
I have thought in another way to approach the problem, and it seems
that it works fine, fingers crossed!

Moltes gràcies per l'ajuda!

Oriol
Re: Problems with ERODE and DILATE functions [message #73600 is a reply to message #73589] Wed, 17 November 2010 12:05 Go to previous message
guillermo.castilla.ca is currently offline  guillermo.castilla.ca
Messages: 27
Registered: September 2008
Junior Member
Hola Oriol,

I haven't read thoroughly your post or your code, but since you say
that "erode" and "dilate" modify separate your ellipses sometimes, it
may be the case that then you have too many individual regions and
your code gets too slow. Instead of using WHERE and SEARCH_2D, you can
use LABEL_REGION and HISTOGRAM to locate individual regions in a much
more efficient way:

hst= histogram(label_region(slice, min=1, omax=nreg, rev=r)
for j=0L, nreg-1 do begin
region= r[r[j]:r[j+1]-1]
...
numpts= hst[j]
...
endfor

Also, instead of dilate and erode, you could simply use the MEDIAN
function to remove small regions or holes within small regions

Bona sort!

Guillermo
Re: Problems with ERODE and DILATE functions [message #73611 is a reply to message #73600] Tue, 16 November 2010 23:57 Go to previous message
Oriol Güell Riera is currently offline  Oriol Güell Riera
Messages: 7
Registered: September 2010
Junior Member
On 16 nov, 22:44, James <donje...@gmail.com> wrote:
> On Nov 16, 8:02 am, Oriol Güell Riera <oriolguellri...@gmail.com>
> wrote:
>
>
>
>> On 16 nov, 16:56, David Fanning <n...@dfanning.com> wrote:
>
>>> Oriol Güell Riera writes:
>>>> The problem is that it works fine for small stacks of images but not
>>>> for large ones, the pc crashes.
>
>>> Yes, and I presume you think it should work for large
>>> stacks of images. That's the assumption I think I would
>>> focus the investigation on. You assume large stacks
>>> are similar to small stacks in all ways except size.
>>> Are they? Does your program evolve over time to do
>>> something you don't expect it to? Larger stacks
>>> mean longer running times. These are just two of
>>> the hundred or so theories I could come up with if
>>> I had five minutes. You are going to have to play
>>> detective this morning. Don't you watch CSI? It's
>>> going to be FUN! :-)
>
>>> 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.")
>
>> It seems I will have to do become Sherlock Holmes, because when I use
>> erode and dilate for the large stack it works fine, the problem is
>> that it detects more particles because erode and dilate separate the
>> pixels. Due to this, I erase erode and dilate, so I think that the
>> only modification in the program is that the pixels are not going to
>> get modified, but the rest of the program is exactly the same. The
>> thing that makes me go mad is that the program works with erode and
>> dilate and it doesn't work when I remove them. It is strange, it
>> should work without modifying the image.
>> I'll keep investigating.
>> Thank you again David,
>> Oriol
>
> Can you post the code for the program you're working on?  As David
> says, it sounds like there is some unintended behavior in the program
> that you're not accounting for.
>
> One thing to consider: DILATE and ERODE are converting your data to
> Byte type unless you are using both the /GREY and /PRESERVE_TYPE
> keywords.  Perhaps your input data is in a larger type, and when you
> remove the DILATE/ERODE calls, the data is too large for later stages
> of processing.

Here's the routine. The main program calls it in some point to find
the particles and compute the center of mass position and the
orientation:

----------------------------------------------
function tracking,cube,num,result

result=cube

nx=n_elements(cube(*,0,0))
ny=n_elements(cube(0,*,0))
nz=n_elements(cube(0,0,*))
bloblist=fltarr(4)
blank=bytarr(nx,ny)
the=fltarr(num*nz)
k=-1
oa=0

for j=0,nz-1 do begin
o=0
if j eq oa*10 then begin
message,'slice '+string(j)+' of '+string(nz-1),/inf
oa=oa+1
endif
slice=result(*,*,j)

;The next 5 lines are the ones that I'm trying to erase, but
when I put them off the program crashes!
lilsquare=bytarr(3,3)+1b
bigsquare=bytarr(5,5)+1b
; next three lines do a good job removing tiny islands and
craters
slice=erode(slice,lilsquare)
slice=dilate(slice,bigsquare)
slice=erode(slice,lilsquare)


s=size(slice)
w=where(slice eq 1,nw)
if (nw gt 0) then begin
while (nw gt 0) do begin
ypos2=w(0)/(s[1])
xpos2=w(0)-(ypos2*s[1])
region=search2d(slice,xpos2,ypos2,1,1)
if (n_elements(region) gt 1) then begin
k=k+1
o=o+1
ypos=region/(s[1])
xpos=(region-(ypos*s[1]))
numpts=n_elements(region)
mass=numpts
massxy=slice(xpos,ypos)
xbar=total(xpos*massxy)/mass
ybar=total(ypos*massxy)/mass
rx=xpos-xbar
ry=ypos-ybar
i11=total(rx*rx)/mass
i22=total(ry*ry)/mass
i12=total(rx*ry)/mass
trace=i11+i22
det=i11*i22-i12*i12
eval1=trace/2+sqrt(trace^2/4-det)
xeix1=1./sqrt(1.+((eval1-i11)/i12)^2)
yeix1=(eval1-i11)/i12*1./sqrt(1.+
((eval1-i11)/i12)^2)
if i12 eq 0 then begin
xeix1=1.
yeix1=0.
endif
if (j eq 0) then begin
the(k)=!pi-atan2(yeix1,xeix1)
endif else begin
theta1=!pi-atan2(yeix1,xeix1)
theta2=!pi-atan2(-yeix1,-
xeix1)
if (abs(theta1-the(k-num)) gt
2) then begin
the(k)=theta2
endif else begin
the(k)=theta1
endelse
endelse
ybar=float(ny)-temporary(ybar)
bloblist = [[bloblist],
[xbar,ybar,the(k),j]]
if o gt num then begin
print,'Slice',j,' has a
problem'
endif
slice(region) = 255b
w=where(slice eq 1,nw)
endif
end
endif else begin
message,'WARNING: no pixels above threshold',/inf
endelse
result(*,*,j)=slice
endfor
; strips off first empty row
bloblist=bloblist(*,1:*)

return, bloblist

end
---------------------------------------

The input images are thresholded, they only have 0 and 1. Do you
suggest to write slice=byte(temporary(slice)) instead of erode and
dilate to transform it to byte type?

Thanks again David and James
Oriol
Re: Problems with ERODE and DILATE functions [message #73617 is a reply to message #73611] Tue, 16 November 2010 13:44 Go to previous message
James[2] is currently offline  James[2]
Messages: 44
Registered: November 2009
Member
On Nov 16, 8:02 am, Oriol Güell Riera <oriolguellri...@gmail.com>
wrote:
> On 16 nov, 16:56, David Fanning <n...@dfanning.com> wrote:
>
>
>
>> Oriol Güell Riera writes:
>>> The problem is that it works fine for small stacks of images but not
>>> for large ones, the pc crashes.
>
>> Yes, and I presume you think it should work for large
>> stacks of images. That's the assumption I think I would
>> focus the investigation on. You assume large stacks
>> are similar to small stacks in all ways except size.
>> Are they? Does your program evolve over time to do
>> something you don't expect it to? Larger stacks
>> mean longer running times. These are just two of
>> the hundred or so theories I could come up with if
>> I had five minutes. You are going to have to play
>> detective this morning. Don't you watch CSI? It's
>> going to be FUN! :-)
>
>> 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.")
>
> It seems I will have to do become Sherlock Holmes, because when I use
> erode and dilate for the large stack it works fine, the problem is
> that it detects more particles because erode and dilate separate the
> pixels. Due to this, I erase erode and dilate, so I think that the
> only modification in the program is that the pixels are not going to
> get modified, but the rest of the program is exactly the same. The
> thing that makes me go mad is that the program works with erode and
> dilate and it doesn't work when I remove them. It is strange, it
> should work without modifying the image.
> I'll keep investigating.
> Thank you again David,
> Oriol

Can you post the code for the program you're working on? As David
says, it sounds like there is some unintended behavior in the program
that you're not accounting for.

One thing to consider: DILATE and ERODE are converting your data to
Byte type unless you are using both the /GREY and /PRESERVE_TYPE
keywords. Perhaps your input data is in a larger type, and when you
remove the DILATE/ERODE calls, the data is too large for later stages
of processing.
Re: Problems with ERODE and DILATE functions [message #73618 is a reply to message #73617] Tue, 16 November 2010 08:02 Go to previous message
Oriol Güell Riera is currently offline  Oriol Güell Riera
Messages: 7
Registered: September 2010
Junior Member
On 16 nov, 16:56, David Fanning <n...@dfanning.com> wrote:
> Oriol Güell Riera writes:
>> The problem is that it works fine for small stacks of images but not
>> for large ones, the pc crashes.
>
> Yes, and I presume you think it should work for large
> stacks of images. That's the assumption I think I would
> focus the investigation on. You assume large stacks
> are similar to small stacks in all ways except size.
> Are they? Does your program evolve over time to do
> something you don't expect it to? Larger stacks
> mean longer running times. These are just two of
> the hundred or so theories I could come up with if
> I had five minutes. You are going to have to play
> detective this morning. Don't you watch CSI? It's
> going to be FUN! :-)
>
> 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.")

It seems I will have to do become Sherlock Holmes, because when I use
erode and dilate for the large stack it works fine, the problem is
that it detects more particles because erode and dilate separate the
pixels. Due to this, I erase erode and dilate, so I think that the
only modification in the program is that the pixels are not going to
get modified, but the rest of the program is exactly the same. The
thing that makes me go mad is that the program works with erode and
dilate and it doesn't work when I remove them. It is strange, it
should work without modifying the image.
I'll keep investigating.
Thank you again David,
Oriol
Re: Problems with ERODE and DILATE functions [message #73619 is a reply to message #73618] Tue, 16 November 2010 07:56 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Oriol Güell Riera writes:

> The problem is that it works fine for small stacks of images but not
> for large ones, the pc crashes.

Yes, and I presume you think it should work for large
stacks of images. That's the assumption I think I would
focus the investigation on. You assume large stacks
are similar to small stacks in all ways except size.
Are they? Does your program evolve over time to do
something you don't expect it to? Larger stacks
mean longer running times. These are just two of
the hundred or so theories I could come up with if
I had five minutes. You are going to have to play
detective this morning. Don't you watch CSI? It's
going to be FUN! :-)

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.")
Re: Problems with ERODE and DILATE functions [message #73620 is a reply to message #73619] Tue, 16 November 2010 07:38 Go to previous message
Oriol Güell Riera is currently offline  Oriol Güell Riera
Messages: 7
Registered: September 2010
Junior Member
On 16 nov, 16:27, David Fanning <n...@dfanning.com> wrote:
> Oriol Güell Riera writes:
>> I have a problem with the "erode" and "dilate" functions. I have a
>> program which tracks ellipses, and it uses these functions to improve
>> the images. However, if I try to run the program without using "erode"
>> and "dilate", the program crashes, it can't keep tracking the ellipses
>> (I have got a stack of images and the program stops tracking at some
>> point of the tracking). I don't know why it happens because if I
>> rewrite the functions to the routine, the program finishes the
>> tracking. The main problem is that "erode" and "dilate" modify
>> sometimes my images, they separate my ellipses sometimes, so I have to
>> erase the "erode" and "dilate" functions from the prorgam. In
>> principle, these functions should not interfere if I put them out of
>> the routine, but actually they do.
>
> Your program is not doing what you *think* it is doing
> quite yet. I would spend some time reviewing all your
> assumptions and making sure they are true. About 99
> percent of the time when you "don't know why it happens",
> it is because of programmer error. :-)
>
> 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.")

The problem is that it works fine for small stacks of images but not
for large ones, the pc crashes.
Thanks,
Oriol
Re: Problems with ERODE and DILATE functions [message #73621 is a reply to message #73620] Tue, 16 November 2010 07:27 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Oriol Güell Riera writes:

> I have a problem with the "erode" and "dilate" functions. I have a
> program which tracks ellipses, and it uses these functions to improve
> the images. However, if I try to run the program without using "erode"
> and "dilate", the program crashes, it can't keep tracking the ellipses
> (I have got a stack of images and the program stops tracking at some
> point of the tracking). I don't know why it happens because if I
> rewrite the functions to the routine, the program finishes the
> tracking. The main problem is that "erode" and "dilate" modify
> sometimes my images, they separate my ellipses sometimes, so I have to
> erase the "erode" and "dilate" functions from the prorgam. In
> principle, these functions should not interfere if I put them out of
> the routine, but actually they do.

Your program is not doing what you *think* it is doing
quite yet. I would spend some time reviewing all your
assumptions and making sure they are true. About 99
percent of the time when you "don't know why it happens",
it is because of programmer error. :-)

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.")
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDLPong, a Pong game in IDL.
Next Topic: Extracting info from the ASCII file with IDL

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:39:30 PDT 2025

Total time taken to generate the page: 0.00789 seconds