Open
Description
Hi, I am running the Gray level co-occurrence matrices example in pyimagej, but it seems ij.op().haralick().correlation()
and ij.op().haralick().differenceVariance()
output java class net.imglib2.type.numeric.real.DoubleType
, which cannot be converted into Python using ij.py.from_java()
.
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib import patches
MatrixOrientation2D = imagej.sj.jimport('net.imagej.ops.image.cooccurrenceMatrix.MatrixOrientation2D')
orientations = [
MatrixOrientation2D.ANTIDIAGONAL,
MatrixOrientation2D.DIAGONAL,
MatrixOrientation2D.HORIZONTAL,
MatrixOrientation2D.VERTICAL
]
corr = ij.op().haralick().correlation(img, gray_levels, dist, angle)
diff = ij.op().haralick().differenceVariance(img, gray_levels, dist, angle)
corr = ij.py.from_java(corr)
diff = ij.py.from_java(diff)
return (corr.value, diff.value)
def process_crops(crops, gray_levels: int, dist: int) -> pd.DataFrame:
glcm_mean_results = []
for key, value in crops.items():
glcm_angle_results = []
image = ij.py.to_dataset(value) # convert the view to a net.imagej.Dataset
glcm_angle_results.append(run_glcm(image, gray_levels, dist, angle))
corr_mean = sum(x[0] for x in glcm_angle_results) / len(glcm_angle_results)
diff_mean = sum(x[1] for x in glcm_angle_results) / len(glcm_angle_results)
glcm_mean_results.append((corr_mean, diff_mean))
return pd.DataFrame(glcm_mean_results, columns=['corr', 'diff'])`
gray_levels = 128 # a value lower than the bit depth of the image and typically a power of 2
dist = 7 # distance in pixels# set up GLCM parameters
gray_levels = 128 # a value lower than the bit depth of the image and typically a power of 2
dist = 7 # distance in pixels
data = ij.io().open('https://media.imagej.net/workshops/data/2d/hela_hiv_gag-yfp.tif')
data_xarr = ij.py.from_java(data)
ij.py.show(data_xarr * 12, cmap='Greys_r') # multiply by 12 to better visualize the data (doesn't change source)
crops = {
"cyto1": data[318: 368, 369: 419], # cell 1 cytoplasm crop
"cyto2": data[130: 180, 355: 405], # cell 2 cytoplasm crop
"cyto3": data[87: 137, 194: 244], # cell 3 cytoplasm crop
"cyto4": data[256: 306, 43: 93], # cell 4 cytoplasm crop
"bkg1": data[19: 69, 57: 107], # background 1 crop
"bkg2": data[263: 313, 221: 271] # background 2 crop
}
crop_coords = {
"cyto1": (318, 369),
"cyto2": (130, 355),
"cyto3": (87, 194),
"cyto4": (256, 43),
"bkg1": (19, 57),
"bkg2": (263, 221)
}
plt.style.use('ggplot')
df = process_crops(crops, gray_levels, dist)
df["name"] = ["cyto1", "cyto2", "cyto3", "cyto4", "bkg1", "bkg2"]
plt.scatter(df['corr'], df['diff'])
for i in range(len(df)):
plt.annotate(f"{df['name'][i]}", (df['corr'][i], df['diff'][i]))
plt.xlabel('corr')
plt.ylabel('diff')
plt.title('GLCM texutre plot')
plt.show()
with the error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[10], line 5
2 plt.style.use('ggplot')
4 # process the dict of crops and add crop names to the output dataframe
----> 5 df = process_crops(crops, gray_levels, dist)
6 df["name"] = ["cyto1", "cyto2", "cyto3", "cyto4", "bkg1", "bkg2"]
8 # plot the data in a matplotlib scatter plot
Cell In[5], line 36, in process_crops(crops, gray_levels, dist)
34 # compute the correlation and difference variance textures at all orientations
35 for angle in orientations:
---> 36 glcm_angle_results.append(run_glcm(image, gray_levels, dist, angle))
37 # calculate the mean of the angle results
38 corr_mean = sum(x[0] for x in glcm_angle_results) / len(glcm_angle_results)
Cell In[5], line 16, in run_glcm(img, gray_levels, dist, angle)
13 diff = ij.op().haralick().differenceVariance(img, gray_levels, dist, angle)
15 # convert to Python float
---> 16 corr = ij.py.from_java(corr)
17 diff = ij.py.from_java(diff)
19 return (corr.value, diff.value)
File ~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/imagej/__init__.py:288, in ImageJPython.from_java(self, data)
279 """Convert supported Java data into Python equivalents.
280
281 Converts Java objects (e.g. 'net.imagej.Dataset') into the Python
(...)
285 :return: A Python object converted from Java.
286 """
287 # todo: convert a dataset to xarray
--> 288 return sj.to_python(data)
File ~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/scyjava/__init__.py:700, in to_python(data, gentle)
698 except TypeError as exc:
699 if gentle: return data
--> 700 raise exc
File ~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/scyjava/__init__.py:697, in to_python(data, gentle)
695 start_jvm()
696 try:
--> 697 return _convert(data, py_converters)
698 except TypeError as exc:
699 if gentle: return data
File ~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/scyjava/__init__.py:266, in _convert(obj, converters)
264 suitable_converters = filter(lambda c: c.predicate(obj), converters)
265 prioritized = max(suitable_converters, key = lambda c: c.priority)
--> 266 return prioritized.converter(obj)
File ~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/scyjava/__init__.py:345, in _raise_type_exception(obj)
344 def _raise_type_exception(obj: Any):
--> 345 raise TypeError('Unsupported type: ' + str(type(obj)))
TypeError: Unsupported type: <java class 'net.imglib2.type.numeric.real.DoubleType'>
``
Metadata
Metadata
Assignees
Labels
No labels