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 239931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:37:30+00:00 2026-05-11T20:37:30+00:00

I have a small python VTK function that calculates the volume and surface area

  • 0

I have a small python VTK function that calculates the volume and surface area of an object embedded in a stack of TIFF images. To read the TIFF's into VTK, I have used vtkTIFFReader and processed the result using vtkImageThreshold. I then use vtkMassProperties to extract the volume and surface area of the object identified after thresholding.

With VTK-5.04, this function returns the correct value for a test stack (3902 pixels). However, using VTK-5.4.2 the same function returns a different value (422 pixels). Can someone explain this?


Code

def testvtk():
    # read 36 TIFF images. Each TIFF is 27x27 pixels
    v16=vtk.vtkTIFFReader()
    v16.SetFilePrefix("d:/test/slice")
    v16.SetDataExtent(0,27,0,27,1,36)
    v16.SetFilePattern("%s%04d.tif")
    v16.SetDataSpacing (1,1,1)
    v16.Update()

    # Threshold level for seperating background/foreground pixels
    maxthres=81

    # Threshold the image stack
    thres=vtk.vtkImageThreshold()
    thres.SetInputConnection(v16.GetOutputPort())
    thres.ThresholdByLower(0)
    thres.ThresholdByUpper(maxthres)

    # create ISO surface from thresholded images
    iso=vtk.vtkImageMarchingCubes()
    iso.SetInputConnection(thres.GetOutputPort())

    # Have VTK calculate the Mass (volume) and surface area
    Mass = vtk.vtkMassProperties()
    Mass.SetInputConnection(iso.GetOutputPort())
    Mass.Update() 

    # just print the results
    print "Volume = ", Mass.GetVolume() 
    print "Surface = ", Mass.GetSurfaceArea()

Note

By testing both VTK-5.4.2 and VTK-5.2.1, I narrowed things down a bit and believe this behaviour was introduced between versions 5.0.4 and 5.2.1.

Update

It seems that in VTK-5.4.2, vtkTIFFReader ignores the x and y values set in the SetDataSpacing method. Instead vtkTIFFReader is calculating the x and y dataspacing from the resolution reported by the TIFF files.

  • 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-11T20:37:30+00:00Added an answer on May 11, 2026 at 8:37 pm

    I have never heard of VTK before, but here it goes.

    Good thing about opensource software is that you can check the source code directly. Better yet, if there’s web-based version control browser, we can talk about it online like this.

    Let’s see vtkMassProperties in question. 5.0.4 uses r1.28 and 5.4.2 uses r1.30. Here’s the diff between r1.28 and r.30. The part that may affect volume calculations are

    vol[2] += (area * (double)u[2] * (double)zavg); // 5.0.4
    vol[2] += (area * u[2] * zavg); // 5.4.2
    

    and

    kxyz[0] = (munc[0] + (wxyz/3.0) + ((wxy+wxz)/2.0)) /(double)(numCells); // 5.0.4
    kxyz[0] = (munc[0] + (wxyz/3.0) + ((wxy+wxz)/2.0)) /numCells; // 5.4.2
    

    but all changes look ok to me.

    Next suspicious are the vtkMarchingCubes. Diff between r1.1.6.1 and 1.5.

    self->UpdateProgress ((double) k / ((double) dims[2] - 1)); // 5.0.4
    self->UpdateProgress (k / static_cast<double>(dims[2] - 1)); // 5.4.2
    

    and

    estimatedSize = (int) pow ((double) (dims[0] * dims[1] * dims[2]), .75); // 5.0.4
    estimatedSize = static_cast<int>(
                 pow(static_cast<double>(dims[0]*dims[1]*dims[2]),0.75)); // 5.4.2
    

    Again, they are fixing stuff on casting, but it looks ok.

    Might as well see vtkImageThreshold too. Diff between r1.50 and r1.52.

    lowerThreshold = (IT) inData->GetScalarTypeMin(); // 5.0.4
    lowerThreshold = static_cast<IT>(inData->GetScalarTypeMin()); // 5.4.2
    

    There are bunch more, but they are all casting stuff.

    It gets more interesting with vtkTIFFReader. Diff between 1.51 and 1.63. As you can see by the difference in the revision numbers, there has been some development in this class compared to others. Here are the checkin comments:

    • ENH: Add an name for scalars. Visible in Paraview.
    • ENH: vtkDataArray now has a new superclass-vtkAbstractArray…
    • ENH: Set default number of sampels per pixel for files that are missing this meta data.
    • ENH: Read only what you need.
    • ENH: add multipage TIFF file support
    • ENH: print ivars
    • BUG: TIFF Reader did not properly account for RLE encoded data. Also, ExecuteInformation
      overwrote user specified spacing and origin.
    • BUG: when reading beach.tif (from current CVS VTKData), the image would be loaded upside down.
    • STYLE: s/OrientationTypeSpecifiedFlag/OriginSpecifiedFlag/g and s/OrientationTypeSpecifiedFlag/SpacingSpecifiedFlag/g
    • BUG: Reader was not handling extents properly.
    • COMP: Fixing a warning.
    • COMP: Getting rid of a warning.

    From the amount of changes that was made in vtkTIFFReader, I would guess that the difference in behavior is coming from there. For example, it may have started to recognize your Tiff as different format and changed the internal pixel values. Try printing out pixel values and see if there is any difference. If the pixel values have changed maxthres=81 may be too high.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The only way to do this is by actually using… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer With CSS: selector { cursor: none; } An example: <div… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer The bin/Debug folder is not where the unit tests run.… May 11, 2026 at 11:49 pm

Related Questions

I have a small python script which i use everyday......it basically reads a file
I have a small python script to make an installation. I need some information
Background : I have a small Python application that makes life for developers releasing
I have a web based email application that logs me out after 10 minutes

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.