[ITK] [ITK-users] CropImageFilter, multithreading

Jonas Teuwen jonasteuwen at gmail.com
Mon May 22 11:05:56 EDT 2017


Hello Brad,

That is a very nice example indeed. I will try it and see if that is
sufficient for my purposes.
As far as I understood the python multithreading, spawning new threads can
create quite a bit over overhead compared to say, OpenMP.

If I wanted to translate your example to ITK/C++ would a direct translation
be the most sensible thing to do, or does ITK have smarter methods for that?

Jonas

On May 22, 2017 16:46, "Lowekamp, Bradley (NIH/NLM/LHC) [C]" <
blowekamp at mail.nih.gov> wrote:

Hello Jonas,

So ITK ( and therefore SimpleITK ), by default multi-thread each filter by
default. This enables efficient processing of larger images.

However, for your task you can run a large number of filters concurrently.
Fortunately, SimpleITK does support concurrent execution of ITK filters
with light weight python Threads! This is an uncommon feature for Python
libraries and is a distinguishing feature of SimpleITK.

I have been planning on writing an example or notebook on this.  Here is an
efficient and compact code to accomplish your task as I understand it:

import SimpleITK as sitk
from multiprocessing.pool import ThreadPool

p = ThreadPool()

# https://github.com/blowekamp/itkOBBLabelMap/tree/master/test/data
img = sitk.ReadImage(“~/Downloads/jelly_beans.png")
seg = sitk.ReadImage(“~/Downloads/jelly_beans_seg.png”)

shapeStats = sitk.LabelShapeStatisticsImageFilter()
shapeStats.Execute(seg)

def extract_bb(img, shape_stats_filter, label):
    [x,y,xsize,ysize]=shape_stats_filter.GetBoundingBox(label)
    return sitk.RegionOfInterest(img,size=[xsize,ysize],index=[x,y])

bbimg_list = p.map(lambda label: extract_bb(img, shapeStats, label),
shapeStats.GetLabels())


This uses advance concepts of multi-threading, closures, mapping, and
thread pools. I think it is the integration of SimpleITK any Python at its
bests!

One tweak which could be made to this code is to create a
RegionOfInterestImageFilter object, and explicitly set it’s number of
threads to 1, so that it is not multi-threaded.

A related not is the recently Oriented Bounding Box computation has been
added to ITK’s LabelShape objects and filters, this is starting to get
propagated into SimpleITK now. This can be used for a similar purpose but
with a resample image filter to change the orientation of the separate
object.

Enjoy!
Brad

On May 22, 2017, at 5:59 AM, Jonas Teuwen <jonasteuwen at gmail.com> wrote:

Dear all,

Currently I have SimpleITK code to extract patches from a 3D medical image
to train a neural network with. I do this with CropImageFilter and check if
they are on the edge or not, and pad if necessary.

Currently this is done offline, so speed is not really an issue, however, I
would like to do this online now, so load the image and mask, and return
the patches. If I want to extract many small patches, about 1000 out of a
large image (~3000x3000x50 or so) is there any reason why I would not use
OpenMP instead of ITK's possibilities? I do not have a good understanding
of the multithreading capabilities yet, so any pointers would be great.

Best,
Jonas Teuwen

_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20170522/059088c7/attachment-0001.html>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users


More information about the Community mailing list