Created
July 20, 2021 16:13
-
-
Save oeway/56bc262caa6040504aff92958f03c6a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<docs lang="markdown"> | |
## PyImageJ demo | |
A PyImageJ demo ported from https://github.com/imagej/pyimagej | |
</docs> | |
<config lang="json"> | |
{ | |
"name": "N2V-Fiji-Demo", | |
"type": "native-python", | |
"version": "0.1.4", | |
"api_version": "0.1.2", | |
"description": "This plugin process image with Noise2Void plugin in Fiji", | |
"tags": [], | |
"ui": "", | |
"inputs": null, | |
"outputs": null, | |
"flags": [], | |
"icon": "extension", | |
"env": ["conda create -n imagej", {"type": "binder", "spec": "oeway/pyimagej-binder-image/master", "skip_requirements": true}], | |
"requirements": ["conda: openjdk=8", "conda: -c conda-forge pyimagej", "pip: scikit-image pillow"], | |
"dependencies": [] | |
} | |
</config> | |
<script lang="python"> | |
from imjoy import api | |
import imagej | |
import numpy as np | |
import skimage | |
from skimage import io | |
import numpy as np | |
from PIL import Image | |
import base64 | |
from io import BytesIO | |
import urllib.request | |
import os | |
ij = imagej.init(os.path.expanduser("~") + '/Fiji.app',headless=True) | |
def array2base64(img): | |
img = img/(img.max())*255.0 | |
img = Image.fromarray(img.astype('uint8')) | |
byte_io = BytesIO() | |
img.save(byte_io, 'PNG') | |
result = base64.b64encode(byte_io.getvalue()).decode('ascii') | |
imgurl = 'data:image/png;base64,' + result | |
return imgurl | |
class ImJoyPlugin(): | |
def setup(self): | |
api.log(ij.getVersion().toString()) | |
urllib.request.urlretrieve('https://github.com/juglab/N2V_fiji/blob/master/src/test/resources/format-0.2.0-csbdeep.bioimage.io.zip?raw=true', '/home/jovyan/format-0.2.0-csbdeep.bioimage.io.zip') | |
async def run(self, my): | |
# Import an image with scikit-image. | |
# NB: Blood vessel image from: https://www.fi.edu/heart/blood-vessels | |
await api.showStatus('Loading example image...') | |
urllib.request.urlretrieve('https://raw.githubusercontent.com/juglab/N2V_fiji/master/src/test/resources/blobs.tif', '/home/jovyan/input.tif') | |
api.showStatus('Processing image with N2V...') | |
modelPath = "/home/jovyan/format-0.2.0-csbdeep.bioimage.io.zip" | |
inputPath = "/home/jovyan/input.tif" | |
outputPath = "/home/jovyan/output.tif" | |
if os.path.exists(outputPath): | |
os.remove(outputPath) | |
args = { | |
'training': '/home/jovyan/input.tif', | |
'prediction': '/home/jovyan/input.tif', | |
'output': outputPath, | |
'axes': 'XY', | |
'mode3d': False, | |
'numepochs': 2, | |
'numstepsperepoch': 4, | |
'batchsize': 8, | |
'patchshape': 16, | |
'neighborhoodradius': 5 | |
} | |
modelzoo_service = ij.context().getService('net.imagej.modelzoo.ModelZooService') | |
model = modelzoo_service.io().open(modelPath) | |
img = ij.io().open(inputPath) | |
output = modelzoo_service.predict(model, img, args['axes']) | |
dataset = ij.dataset().create(output.get('output')) | |
ij.io().save(dataset, outputPath) | |
await api.showStatus('Image processed with ImageJ.') | |
img = io.imread('/home/jovyan/input.tif') | |
output_img = io.imread('/home/jovyan/output.tif') | |
# Display image | |
imgurl1 = array2base64(img) | |
imgurl2 = array2base64(output_img) | |
viewer = await api.createWindow(src="https://kaibu.org") | |
await viewer.view_image(imgurl1, name="input image") | |
await viewer.view_image(imgurl2, name="output image") | |
# api.showDialog({'type': 'imjoy/image-compare', 'name': 'ImageJ Demo', 'data': {"first": imgurl1, 'second': imgurl2, 'name': 'Process with frangiVesselness in ImageJ'}}) | |
api.export(ImJoyPlugin()) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment