in Revit Dynamo, can i invert real image into numpy array? - python

these day, i've done some research that change real masonry wall image into architectural drawing(like CAD,Blueprinting).
so.. my solution is change real masonry image into grayscale image, then convert 0 and 1 numpy array. is this process possible?
and also if i can make image numpy array, then can i import this data array into Dynamo(Revit plugin)?

Answering your questions one by one:
yes, you can change a real masonry image into a grayscale image
yes, you can convert an image into the binary matrix stored in a numpy array
yes, you can import this data to Dynamo
What's more, you can actually do all of those in Dynamo using Python Script node starting from importing the image file. If you are working in Revit 2022 you can benefit from CPython and simply import numpy, if older versions, you would have to point to location where numpy is installed.
To do it, first, you need to have numpy for ironpython2.7 (the version used in Revit 2021 and older). Instructions can be found here: How to install numpy and scipy for Ironpython27? Old method doesn't work
Once you have it, simply point in your Dynamo Python Script node to that library location:
import sys
import clr
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
import numpy

Related

Medical imaging (PyRadiomics) with .nii.gz files

I am trying to implement the package:
https://pyradiomics.readthedocs.io/en/latest/usage.html
It looks super simple, but they expect .nrrd files.
My files are .nii.gz. How do I solve this?
Also, have anyone tried to apply PyRadiomics on TCIA data? if so, can I see your github or Jupyter Notebook?
Thanks a lot.
You could turn NII into numpy array firstly and then turn it into NRRD with using:
nrrd and nibabel
import numpy as np
import nibabel as nib
import nrrd
# Download NII
example_filename = "image.nii.gz"
image = nib.load(example_filename)
# Turn into numpy array
array = np.array(img.dataobj)
# Save NRRD
nrrd_path_to = "image.nrrd"
nrrd.write(image_path_to, array)
Although the examples are in .nrrd, PyRadiomics uses SimpleITK for image operations. This allows PyRadiomics to support a whole range of image formats, including .nii.gz. You don't have to convert them.
The DWIConverter converts diffusion-weighted MR images in DICOM series into nrrd format for analysis in Slicer. It parses the DICOM header to extract necessary information about measurement frame, diffusion weighting directions, b-values, etc, and write out a nrrd image. For non-diffusion weighted DICOM images, it loads in an entire DICOM series and writes out a single DICOM volume in a .nhdr/.raw pair.
So that trying to convert your .nii.gz inside DICOM files for the nrrd format is a possibility by using this tools. Also, you can look at the SlicerDMRI that is a similar module.

Opening Images as Arrays

I have a script that should open an image as a 2D array but I can't seem to make it work. I have tried using the numpy an PIL libraries. I tried this on different computers. The issue is that it opens the image as a 2D array on one computer but opens them ups as objects on a different compute running the same version of python.
The code that should open the image and store it as an array can be seen below:
img = np.array(Image.open(imagePath))
On one computer I get an array but on another one I get an image object like this:
array(<PIL.TiffImagePlugin.TiffImageFile image mode=I;16B size=320x240 at 0x2289FA8>, dtype=object)
Have you tried the imread function from matplotlib?
from matplotlib.image import imread
image = imread(image_path)
Returns a numpy array and works fine for me (python 3.4).

Data types in OpenCV

I'm working with OpenCV for the first time and I'm a little bit confused with data types. I am working with Python.
I see that I can store an image as:
Numpy array
CvMat
Iplimage
The problem I'm having is that different parts of OpenCV seem to require different types and I keep having to try and convert back and forth- it's very confusing, and I'm certain it can't have been designed this way on purpose. I'm also a bit confused about when something should be in the cv module vs cv2.cv:
import cv
import cv2.cv
Can someone explain the logic? It would really help.
Thanks!
cv (or, cv2.cv)
is the old opencv python api, using IplImage and CvMat.
you should not use that anymore. it's being phased out, and won't be available in the next version.
cv2 is the new python api, using numpy arrays for almost anything instead,
so easy to combine with scipy, matplotlib and what not. (and , btw, much closer the the current c++ api)

Image Conversion between cv2, cv, mahotas, and SimpleCV

I am having to do a lot of vision related work in Python lately, and I am facing a lot of difficulties switching between formats. When I read an image using Mahotas, I cannot seem to get it to cv2, though they are both using numpy.ndarray. SimpleCV can take OpenCV images easily, but getting SimpleCV image out for legacy cv or mahotas seems to be quite a task.
Some format conversion syntaxes would be really appreciated. For example, if I open a greyscale image using mahotas, it is treated to be in floating point colour space by default, as I gather. Even when I assign the type as numpy.uint8, cv2 cannot seem to recognise it as an array. I do not know how to solve this problem. I am not having much luck with colour images either. I am using Python 2.7 32bit on Ubuntu Oneiric Ocelot.
Thanks in advance!
I have never used mahotas. But I'm currently working on SimpleCV. I have just sent a pull request for making SimpleCV numpy array compatible with cv2.
So, basically,
Image.getNumpy() -> numpy.ndarray for cv2
Image.getBitmap() -> cv2.cv.iplimage
Image.getMatrix() -> cv2.cv.cvmat
To convert cv2 numpy array to SimpleCV Image object,
Image(cv2_image) -> SimpleCV.ImageClass.Image
With only experience in cv2 and SimpleCV, to convert from SimpleCV to cv2:
cv2_image = simplecv_image.getNumpyCv2()
To convert from cv2 to SimpleCV:
simplecv_image = Image(cv2_image.transpose(1, 0, 2)[:, :, ::-1])

Why does this PIL call crash python?

import Image
from numpy import zeros, asarray
YUV = zeros((240, 320, 3), dtype='uint8')
im = Image.fromarray(YUV, mode="YCbCr")
blah = asarray(im)
When I run this (IPython 0.10.1 on Py 2.7.1) it seems to make python read some memory which it shouldn't be reading. Sometimes it crashes, sometimes it doesn't but I can surely make it crash by increasing the 320x240 zeros to, say, 3200x2400 and/or calling blah.copy(). Also, if I do:
from matplotlib import pyplot as p
p.subplot(221); p.imshow(blah[:,:,0])
p.subplot(222); p.imshow(blah[:,:,1])
p.subplot(223); p.imshow(blah[:,:,2])
p.subplot(224); p.imshow(blah[:,:,3])
p.gray()
p.show()
I start to see junk memory appearing in blah at around about row 180. What's going on here? Am I converting from PIL Image to numpy array in a bad way? What is the difference between using array(im) instead of asarray(im), and what is preferred? (note in the former case it does still crash sometimes, but it seems to be more stable and less junk)
(Here's a related question)
I noticed that your image is YCbCr 3-channel, but you're displaying 4 channels. Turns out the "junk data" problem is caused by a bug in PIL's array interface, and a fix was committed in Nov 2010. PIL's array interface is returning a 4th channel.
I ran your test case under PIL 1.1.7 and see the noise. I commented out the 224 subplot and re-ran your test using the latest PIL trunk code and a proper 3-channel array is produced, with no noise. The crashing may also be related, but I couldn't reproduce that in my environment.

Categories