I have some self-developed image segmentation software written in python (for details see link below). Now I want to implement it as a plugin in Fiji/ImageJ to make it publicly available as open source project.
One important step during the algorithm is to compare the distances between two contours.
I decided to take a kd-tree approach.
The following images illustrate the problem:
Fig1. Ground Truth:
Fig2. Bad Image Segmentation realization:
Fig3. Good Image Segmentation realization:
In the original Python implementation the distance between the contours is defined as the cumulative distance for every point on the longer contour (by numbers of pixels contributing to the contour) towards the nearest neighbour on the shorter contour.- It has to be mentioned that a contour is defined as the concatenated contour of all contours in one image.
Example Code from Python:
import numpy as np import cv2 from scipy.spatial import cKDTree [...] # get the contours contours1,hierarchy1 = cv2.findContours(img1,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE) contours2,hierarchy2 = cv2.findContours(img2,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE) # concatenate all contours in one big (non-continous) line ccc1=np.concatenate(contours1,axis=0) ccc2=np.concatenate(contours2,axis=0) # check which contour is longer (made from more pixels): if len(ccc1) <= len(ccc2): ar1=ccc1 ar2=ccc2 elif len(ccc1) > len(ccc2): ar1=ccc2 ar2=ccc1 # query for all distances (from the longer to the shorter contour). tree1=cKDTree(ar1) distances=tree1.query(ar2)
This would result in a smaller distance for the image shown in Figure 3 towards the one shown in Fig 1 compared to Fig 2 towards Fig1.
Here are my open questions:
How do I obtain a list of all “contour pixels” in Java ImageJ?
So far I use
IJ.run(img, "Outline", "=");to obtain the contour as an image, but I would need a
list-object. So this would be a list of pixels in
255- I assume that there is an easier way than iterating over all pixels via
In other words: I am looking for the equivalent of
How can I realize the nearest neighbour approach in Java ImageJ?
net.imglib2.neighborsearch- however, I do not find good docs or examples.
Sometimes the java code is a bit difficult for me as a python programmer .
I am happy about any kind of suggestions. Thanks in advance.
For anyone who is interested, HERE is a link about the theoretical background.