Skip to main content

How to convert between voxel and mm coordinates using Python

I'm often asked how to go from voxel and mm coordinates using Python. This can be easily achieved using nibabel package with only few lines of code. The following tutorial is based on +Matthew Brett answer on the nipy mailing list.

Going from voxel to mm coordinates

import os
import nibabel as nib

Load the NIFTI file defining the space you are interested in. For the purpose of this tutorial we will use a test dataset shipped with nibabel.

data_fname = os.path.join(os.path.dirname(nib.__file__), 'tests', 'data', 'example4d.nii.gz')
img = nib.load(data_fname)

Get the affine matrix and convert the coordinates.


aff = img.get_affine()
real_pt = nib.affines.apply_affine(aff, [22, 34, 12])
real_pt
array([ 73.85510254, 27.1169095 , 29.79324198])

Going from mm to voxel coordinates

Going the other direction is even easier.

import numpy.linalg as npl
nib.affines.apply_affine(npl.inv(aff), real_pt)
array([ 22., 34., 12.])

Comments

  1. This is ansuperior writing service point that doesn't always sink in within the context of the classroom. In the first superior writing service paragraph you either hook the reader's interest or lose it. Of course your teacher, who's getting paid to teach you how to write an good essay, 
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  2. Thnx a lot
    But I hv tried tht but still i hv errors :(

    ReplyDelete
  3. Attend The Python Training in Hyderabad From ExcelR. Practical Python Training Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Hyderabad.
    python training in bangalore

    ReplyDelete
  4. Business Analytics or Data Analytics or data science training in hyderabad is an extremely popular, in-demand profession which requires a professional to possess sound knowledge of analysing data in all dimensions and uncover the unseen truth coupled with logic and domain knowledge to impact the top-line (increase business) and bottom-line (increase revenue). ExcelR’s Data Science curriculum is meticulously designed and delivered matching the industry needs and considered to be the best in the industry.

    ReplyDelete
  5. Attend The Course in Data Analytics From ExcelR. Practical Course in Data Analytics Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Course in Data Analytics.
    ExcelR Course in Data Analytics

    ReplyDelete
  6. At Regulus, all of our employees, whether full-time, leased, temporary or internal are just as important. Best IT Consulting Company In USA We’ll take the time to get to know you and understand your needs and requirements on an individual basis, and we’ll match your skills to our clients’ requirements. Our understanding and commitment to each individual employee and their goals is what you will get when you work with us.

    ReplyDelete
  7. Nice Post...I have learn some new information.thanks for sharing. Artificial Intelligence Course

    ReplyDelete
  8. I have read your blog and I gathered some needful information from your blog.  Machine Learning Training In Bangalore

    ReplyDelete
  9. can i get the code for transforming world to voxel co-ordinates of a mhd file

    ReplyDelete
  10. Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work!
    ExcelR Data Analytics Course

    ReplyDelete
  11. I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work ExcelR Data Analytics Courses

    ReplyDelete
  12. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck... data science course

    ReplyDelete
  13. I have been searching to find a comfort or effective procedure to complete this process and I think this is the most suitable way to do it effectively.
    Please check ExcelR Data Science Courses in Pune

    ReplyDelete
  14. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    data analytics course mumbai

    ReplyDelete
  15. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    data analytics courses
    Data analytics Interview Questions

    ReplyDelete
  16. Attend The Digital Marketing Courses in Bangalore From ExcelR. Practical Digital Marketing Courses in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Digital Marketing Courses in Bangalore.
    ExcelR Digital Marketing Courses in Bangalore

    ReplyDelete
  17. very good post .. I gathered some needful information from your blog.
    digital marketing courses in hyderabad

    ReplyDelete
  18. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

    data analytics courses

    data science interview questions

    business analytics courses

    data science course in mumbai

    ReplyDelete
  19. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
    360DigiTMG business analytics certification
    data science courses
    business analytics course

    ReplyDelete
  20. Really awesome blog!!! I finally found a great post here.I really enjoyed reading this article. Nice article on data science . Thanks for sharing your innovative ideas to our vision. Your writing style is simply awesome with useful information. Very informative, Excellent work! I will get back here.
    Data Science Course
    Data Science Course in Marathahalli

    ReplyDelete
  21. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science training in Hyderabad

    ReplyDelete
  22. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science training

    ReplyDelete
  23. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data science Interview Questions

    ReplyDelete
  24. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data science Interview Questions
    Data Science Course

    ReplyDelete
  25. Attend The Business Analytics Courses From ExcelR. Practical Business Analytics Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses.
    Business Analytics Courses

    ReplyDelete
  26. I have found it extremely helpful.Content of the Articles are fabulous, Thanks for all your Advice keep.updating more and more...
    python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

    ReplyDelete
  27. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
    data analytics course in Bangalore

    ReplyDelete
  28. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
    data analytics course in Bangalore

    ReplyDelete
  29. So luck to come across your excellent blog. Your blog brings me a great deal of fun.. Good luck with the site. ExcelR Machine Learning Course

    ReplyDelete
  30. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data Science Course

    ReplyDelete
  31. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspried me to read more. keep it up.
    Correlation vs Covariance

    ReplyDelete

  32. This was really one of my favorite website. ExcelR Machine Learning Course Pune Please keep on posting.

    ReplyDelete
  33. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression

    ReplyDelete
  34. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.
    Data Science Certification in Bangalore

    ReplyDelete
  35. Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post.
    Data Science Course in Bangalore

    ReplyDelete
  36. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.
    Data Science Training in Bangalore

    ReplyDelete
  37. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
    Data Science Certification in Bangalore

    ReplyDelete
  38. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.
    Data Science Course in Bangalore

    ReplyDelete
  39. It has fully emerged to crown Singapore's southern shores and undoubtedly placed her on the global map of residential landmarks. I still scored the more points than I ever have in a season for GS. I think you would be hard pressed to find somebody with the same consistency I have had over the years so I am happy with that.
    Data Science Training in Bangalore

    ReplyDelete
  40. Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us.

    Data Science Course

    ReplyDelete
  41. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter. Here is deep description about the article matter which helped me more.

    Data Science Training

    ReplyDelete
  42. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    Data Science Course in Pune
    Data Science Training in Pune

    ReplyDelete
  43. Nice blog. I finally found great post here Very interesting to read this article and very pleased to find this site. Great work!
    Data Science Training in Pune
    Data Science Course in Pune

    ReplyDelete
  44. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

    ReplyDelete
  45. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression

    ReplyDelete
  46. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    Data Analytics Course in Pune
    Data Analytics Training in Pune

    ReplyDelete
  47. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    Business Analytics Course in Pune
    Business Analytics Training in Pune

    ReplyDelete
  48. Just the way I have expected. Your website really is interesting.
    learn360digitmg artificial intelligence course

    ReplyDelete
  49. While reading this wonderful article, I came across many aspects on which I coincide with you. It made me head bound to ponder over the topic and read it over again.
    SAP training in Kolkata
    SAP course in kolkata
    SAP training institute in Kolkata

    ReplyDelete
  50. Great! This article is packed full of constructive information. The valuable points made here are apparent, brief, clear and poignant. I am sincerely fond of your writing style.
    SAP training in Mumbai
    SAP course in Mumbai
    SAP training institute Mumbai

    ReplyDelete
  51. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete
  52. This comment has been removed by the author.

    ReplyDelete
  53. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    Data Science Institute in Bangalore

    ReplyDelete

Post a Comment