Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7128369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:07:53+00:00 2026-05-28T11:07:53+00:00

I am trying to compute the volume (or surface area) of a 3D numpy

  • 0

I am trying to compute the volume (or surface area) of a 3D numpy array. The voxels are anisotropic in a lot of cases, and I have the pixel to cm conversion factor in each direction.

Does anyone know a good place to find a toolkit or package to do the above??

Right now, I have some in-house code, but I am looking to upgrade to something more industrial strength in terms of accuracy.

Edit1: Here is some (poor) sample data. This is much smaller than the typical sphere. I will add better data when I can generate it! It is in (self.)tumorBrain.tumor.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:07:54+00:00Added an answer on May 28, 2026 at 11:07 am

    One option is to use VTK. (I’m going to use the tvtk python bindings for it here…)

    At least in some circumstances, getting the area within the isosurface will be a bit more accurate.

    Also, as far as surface area goes, tvtk.MassProperties calculates surface area as well. It’s mass.surface_area (with the mass object in the code below).

    import numpy as np
    from tvtk.api import tvtk
    
    def main():
        # Generate some data with anisotropic cells...
        # x,y,and z will range from -2 to 2, but with a 
        # different (20, 15, and 5 for x, y, and z) number of steps
        x,y,z = np.mgrid[-2:2:20j, -2:2:15j, -2:2:5j]
        r = np.sqrt(x**2 + y**2 + z**2)
    
        dx, dy, dz = [np.diff(it, axis=a)[0,0,0] for it, a in zip((x,y,z),(0,1,2))]
    
        # Your actual data is a binary (logical) array
        max_radius = 1.5
        data = (r <= max_radius).astype(np.int8)
    
        ideal_volume = 4.0 / 3 * max_radius**3 * np.pi
        coarse_volume = data.sum() * dx * dy * dz
        est_volume = vtk_volume(data, (dx, dy, dz), (x.min(), y.min(), z.min()))
    
        coarse_error = 100 * (coarse_volume - ideal_volume) / ideal_volume
        vtk_error = 100 * (est_volume - ideal_volume) / ideal_volume
    
        print 'Ideal volume', ideal_volume
        print 'Coarse approximation', coarse_volume, 'Error', coarse_error, '%'
        print 'VTK approximation', est_volume, 'Error', vtk_error, '%'
    
    def vtk_volume(data, spacing=(1,1,1), origin=(0,0,0)):
        data[data == 0] = -1
        grid = tvtk.ImageData(spacing=spacing, origin=origin)
        grid.point_data.scalars = data.T.ravel() # It wants fortran order???
        grid.point_data.scalars.name = 'scalars'
        grid.dimensions = data.shape
    
        iso = tvtk.ImageMarchingCubes(input=grid)
        mass = tvtk.MassProperties(input=iso.output)
        return mass.volume
    
    main()
    

    This yields:

    Ideal volume 14.1371669412
    Coarse approximation 14.7969924812 Error 4.66731094565 %
    VTK approximation 14.1954890878 Error 0.412544796894 %
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to compute the hash of a byte array in Java. To
I have been trying to compute the total sum of all these ord s
I have a sum that I'm trying to compute, and I'm having difficulty parallelizing
I have a Pig program where I am trying to compute the minimum center
I'm trying to compute de Bruijn sequences for alphabets which have a number of
I am trying to compute area of a latitude-longitude rectangle using following formula: A
Im trying to compute a 24 hour rms (root mean squared) from a dataset
I'm trying to compute item-to-item similarity along the lines of Amazon's Customers who viewed/purchased
I'm trying to compute the average of a field over various subsets of a
I am trying to compute (360 / 24) / 60 I keep getting the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.