You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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().
importmatplotlib.pyplotaspltimportpandasaspdfrommatplotlibimportpatchesMatrixOrientation2D=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)
defprocess_crops(crops, gray_levels: int, dist: int) ->pd.DataFrame:
glcm_mean_results= []
forkey, valueincrops.items():
glcm_angle_results= []
image=ij.py.to_dataset(value) # convert the view to a net.imagej.Datasetglcm_angle_results.append(run_glcm(image, gray_levels, dist, angle))
corr_mean=sum(x[0] forxinglcm_angle_results) /len(glcm_angle_results)
diff_mean=sum(x[1] forxinglcm_angle_results) /len(glcm_angle_results)
glcm_mean_results.append((corr_mean, diff_mean))
returnpd.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 2dist=7# distance in pixels# set up GLCM parametersgray_levels=128# a value lower than the bit depth of the image and typically a power of 2dist=7# distance in pixelsdata=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'])
foriinrange(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()
withtheerror:
---------------------------------------------------------------------------TypeErrorTraceback (mostrecentcalllast)
CellIn[10], line52plt.style.use('ggplot')
4# process the dict of crops and add crop names to the output dataframe---->5df=process_crops(crops, gray_levels, dist)
6df["name"] = ["cyto1", "cyto2", "cyto3", "cyto4", "bkg1", "bkg2"]
8# plot the data in a matplotlib scatter plotCellIn[5], line36, inprocess_crops(crops, gray_levels, dist)
34# compute the correlation and difference variance textures at all orientations35forangleinorientations:
--->36glcm_angle_results.append(run_glcm(image, gray_levels, dist, angle))
37# calculate the mean of the angle results38corr_mean=sum(x[0] forxinglcm_angle_results) /len(glcm_angle_results)
CellIn[5], line16, inrun_glcm(img, gray_levels, dist, angle)
13diff=ij.op().haralick().differenceVariance(img, gray_levels, dist, angle)
15# convert to Python float--->16corr=ij.py.from_java(corr)
17diff=ij.py.from_java(diff)
19return (corr.value, diff.value)
File~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/imagej/__init__.py:288, inImageJPython.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-->288returnsj.to_python(data)
File~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/scyjava/__init__.py:700, into_python(data, gentle)
698exceptTypeErrorasexc:
699ifgentle: returndata-->700raiseexcFile~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/scyjava/__init__.py:697, into_python(data, gentle)
695start_jvm()
696try:
-->697return_convert(data, py_converters)
698exceptTypeErrorasexc:
699ifgentle: returndataFile~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/scyjava/__init__.py:266, in_convert(obj, converters)
264suitable_converters=filter(lambdac: c.predicate(obj), converters)
265prioritized=max(suitable_converters, key=lambdac: c.priority)
-->266returnprioritized.converter(obj)
File~/.pyenv/versions/3.8.19/lib/python3.8/site-packages/scyjava/__init__.py:345, in_raise_type_exception(obj)
344def_raise_type_exception(obj: Any):
-->345raiseTypeError('Unsupported type: 'str(type(obj)))
TypeError: Unsupportedtype: <javaclass'net.imglib2.type.numeric.real.DoubleType'>``
The text was updated successfully, but these errors were encountered:
Hi @YunxiaoWangwww, what version of PyImageJ are you using? I just ran the notebook without errors with the latest release version 1.5.0 and openjdk 11.0.23. Also what version of scyjava do you have installed in your environment?
Hi, I am running the Gray level co-occurrence matrices example in pyimagej, but it seems
ij.op().haralick().correlation()
andij.op().haralick().differenceVariance()
output java classnet.imglib2.type.numeric.real.DoubleType
, which cannot be converted into Python usingij.py.from_java()
.The text was updated successfully, but these errors were encountered: