regionprops Very slow on centroid identification

jeff witz witzjean at gmail.com
Fri Nov 14 10:27:42 EST 2014


Hello.

I'm developing a video extensometer based on the identification of center 
of mass of circular white mark on  a black rubber specimen.

In order to deal with data in real time I have to be fast (over 100 fps). 
So I first identify the Zones Of Interests using this example :
http://scikit-image.org/docs/dev/auto_examples/plot_label.html

Then I compute the center of mass on each ZOI.

As I have a fast camera the ZOI between two successive images doesn't  
change much. So if I extend the bounding box of my current ZOI I could be 
pretty confident in the fact that given circular mark in the next picture 
will be in the extended ZOI and the recompute an updated extened ZOI for 
the next image.

So this is the big picture.

You will find bellow the function I use in order to get it  :
def barycenter(image_,minx_,miny_,maxx_,maxy_,thresh_,border_):
  bw_=image_[minx_:maxx_+1,miny_:maxy_+1]>thresh_
  [Y,X]=np.meshgrid(range(miny_,maxy_+1),range(minx_,maxx_+1))
  region=regionprops(bw_)
  minx,miny,maxx,maxy=region[0].bbox
  Px_=(X*bw_).sum().astype(float)/bw_.sum()
  Py_=(Y*bw_).sum().astype(float)/bw_.sum()
  minx_=X[minx,miny]-border_
  miny_=Y[minx,miny]-border_
  maxx_=X[maxx,maxy]+border_
  maxy_=Y[maxx,maxy]+border_
  return Px_,Py_,minx_,miny_,maxx_,maxy_
 
As you can see I don't use region[0].centroid. I compute the moment myself 

if I time my function on a 141x108 ZOI I get 504 µs

If I time this function :

def barycenter2(image_,minx_,miny_,maxx_,maxy_,thresh_,border_):
  bw_=image_[minx_:maxx_+1,miny_:maxy_+1]>thresh_
  [Y,X]=np.meshgrid(range(miny_,maxy_+1),range(minx_,maxx_+1))
  region=regionprops(bw_)
  Px_,Py_=region[0].centroid
  Px_+=minx_
  Py_+=miny_  
  minx,miny,maxx,maxy=region[0].bbox
  minx_=X[minx,miny]-border_
  miny_=Y[minx,miny]-border_
  maxx_=X[maxx,maxy]+border_
  maxy_=Y[maxx,maxy]+border_
  return Px_,Py_,minx_,miny_,maxx_,maxy_


I get 10ms per loop !

What is really strange is if I time : 

%timeit region[0].centroid
 I get 58.6 ns per loop !

So I don't really understand why this time explose when I use it in a 
function ?

If someone have some insight it will be very helpfull. Even If I can use my 
first function, it's a pity to have to use less optimized functions.

Best regards.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20141114/73413bcb/attachment.html>


More information about the scikit-image mailing list