Java Code Examples for ij.process.ImageProcessor#getf()

The following examples show how to use ij.process.ImageProcessor#getf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageCalculator.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Example that computes the maximum over two images:
 * 
 * <pre>
 * <code>
 * ImageProcessor image1 = ...
 * ImageProcessor image2 = ...
 * ImageProcessor result = ImageCalculator.combineImages(
 * 		image1, image2, ImageCalculator.Operation.MAX);  
 * </code>
 * </pre>
 * 
 * @param image1
 *            the first input image
 * @param image2
 *            the second input image
 * @param op
 *            the operation to apply
 * @return the result of operation applied to the pair of input images
 */
public static final ImageProcessor combineImages(ImageProcessor image1, ImageProcessor image2,
        Operation op)
{
    int width = image1.getWidth();
    int height = image1.getHeight();
    
    ImageProcessor result = image1.duplicate();
    
    float v1, v2, vr;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            v1 = image1.getf(x, y);
            v2 = image2.getf(x, y);
            vr = op.applyTo(v1, v2);
            result.setf(x, y, vr);
        }
    }
    
    return result;
}
 
Example 2
Source File: Threshold.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new binary image with value 255 when input image has value
 * between <code>lower</code> and <code>upper</code> values (inclusive).
 * 
 * @param image
 *            the input grayscale image
 * @param lower
 *            the lower threshold bound (inclusive)
 * @param upper
 *            the upper threshold bound (inclusive)
 * @return a binary image
 */
public static final ImageProcessor threshold(ImageProcessor image, double lower, double upper)
{
	if (image instanceof ColorProcessor) 
	{
		throw new IllegalArgumentException("Requires a gray scale image");
	}
	
	int width = image.getWidth();
	int height = image.getHeight();

	ImageProcessor result = new ByteProcessor(width, height);
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			double value = image.getf(x, y);
			if (value >= lower && value <= upper)
				result.set(x, y, 255);
		}

	}

	return result;
}
 
Example 3
Source File: DistanceTransform5x5Short.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
private ShortProcessor initializeResult(ImageProcessor labelImage)
{
	this.fireStatusChanged(new AlgoEvent(this, "Initialization"));

	// size of image
	int sizeX = labelImage.getWidth();
	int sizeY = labelImage.getHeight();

	// create new empty image, and fill it with black
	ShortProcessor distMap = new ShortProcessor(sizeX, sizeY);
	distMap.setValue(0);
	distMap.fill();

	// initialize empty image with either 0 (background) or Inf (foreground)
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++)
		{
			int label = (int) labelImage.getf(x, y);
			distMap.set(x, y, label == 0 ? 0 : Short.MAX_VALUE);
		}
	}
	
	return distMap;
}
 
Example 4
Source File: DistanceTransform5x5Float.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
private FloatProcessor initializeResult(ImageProcessor labelImage)
{
	this.fireStatusChanged(new AlgoEvent(this, "Initialization"));

	// size of image
	int sizeX = labelImage.getWidth();
	int sizeY = labelImage.getHeight();

	// create new empty image, and fill it with black
	FloatProcessor distMap = new FloatProcessor(sizeX, sizeY);
	distMap.setValue(0);
	distMap.fill();

	// initialize empty image with either 0 (background) or Inf (foreground)
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++)
		{
			int label = (int) labelImage.getf(x, y);
			distMap.setf(x, y, label == 0 ? 0 : Float.POSITIVE_INFINITY);
		}
	}
	
	return distMap;
}
 
Example 5
Source File: DistanceTransform3x3Float.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
private FloatProcessor initializeResult(ImageProcessor labelImage)
{
	this.fireStatusChanged(new AlgoEvent(this, "Initialization"));

	// size of image
	int sizeX = labelImage.getWidth();
	int sizeY = labelImage.getHeight();

	// create new empty image, and fill it with black
	FloatProcessor distMap = new FloatProcessor(sizeX, sizeY);
	distMap.setValue(0);
	distMap.fill();

	// initialize empty image with either 0 (background) or Inf (foreground)
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++)
		{
			int label = (int) labelImage.getf(x, y);
			distMap.setf(x, y, label == 0 ? 0 : Float.POSITIVE_INFINITY);
		}
	}
	
	return distMap;
}
 
Example 6
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new Color image from a label planar image, a LUT, and a 
 * color for background.
 * 
 * @param image an ImageProcessor with label values and 0 for background
 * @param lut the array of color components for each label 
 * @param bgColor the background color
 * @return a new instance of ColorProcessor
 */
public static final ColorProcessor labelToRgb(ImageProcessor image, byte[][] lut, Color bgColor) 
{
	int width = image.getWidth();
	int height = image.getHeight();
	
	int bgColorCode = bgColor.getRGB();
	
	ColorProcessor result = new ColorProcessor(width, height);
	for (int y = 0; y < height; y++) 
	{
		for (int x = 0; x < width; x++) 
		{
			int index = (int) image.getf(x, y);
			if (index == 0) 
			{
				result.set(x, y, bgColorCode);
			} 
			else 
			{
				byte[] rgb = lut[index - 1];
				int color = (int) ((rgb[0] & 0xFF) << 16
						| (rgb[1] & 0xFF) << 8 | (rgb[2] & 0xFF));
				result.set(x, y, color);
			}
		}
	}
	
	return result;
}
 
Example 7
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get the total overlap between two label images (source and target).
 * <p>
 * Total Overlap (for all regions) $TO = \frac{ \sum_r{|S_r \cap T_r|} }{ \sum_r{|T_r|} }$.
 * @param sourceImage source label image
 * @param targetImage target label image
 * @return total overlap value or -1 if error
 * @see <a href="http://www.insight-journal.org/browse/publication/707">http://www.insight-journal.org/browse/publication/707</a>
 */
public static final double getTotalOverlap(
		ImageStack sourceImage,
		ImageStack targetImage )
{
	if( sourceImage.getWidth() != targetImage.getWidth() ||
			sourceImage.getHeight() != targetImage.getHeight() )
		return -1;
	double intersection = 0;
	double numPixTarget = 0;
	// calculate the pixel to pixel intersection
	for( int k = 0; k < sourceImage.getSize(); k ++ )
	{
		final ImageProcessor ls = sourceImage.getProcessor( k+1 );
		final ImageProcessor lt = targetImage.getProcessor( k+1 );
		for( int i = 0; i < sourceImage.getWidth(); i++ )
			for( int j = 0; j < sourceImage.getHeight(); j++ )
			{
				if( ls.getf( i, j ) > 0 ) // skip label 0 (background)
	    		{
	    			if( ls.getf( i, j ) == lt.getf( i, j ) )
	    				intersection ++;
	    		}
				if( lt.getf( i, j ) > 0 )
					numPixTarget ++;
			}
	}
    // return the total overlap
    return intersection / numPixTarget;
}
 
Example 8
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new image containing only the specified labels.
 * 
 * @param image
 *            a planar label image
 * @param labels
 *            the list of values to keep
 * @return a new label image containing only the specified labels
 */
public static final ImageProcessor keepLabels(ImageProcessor image, int[] labels)
{
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	
	ImageProcessor result = image.createProcessor(sizeX,  sizeY);
	
	TreeSet<Integer> labelSet = new TreeSet<Integer>();
	for (int i = 0; i < labels.length; i++) 
	{
		labelSet.add(labels[i]);
	}
	
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++)
		{
			int value = (int) image.getf(x, y); 
			if (value == 0)
				continue;
			if (labelSet.contains(value)) 
				result.setf(x, y, value);
		}
	}
	
	return result;
}
 
Example 9
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get the Dice coefficient between two label images.
 * @param labelImage1 first label image
 * @param labelImage2 second label image
 * @return Dice coefficient value or -1 if error
 * @see <a href="https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient">https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient</a>
 */
public static final double getDiceCoefficient(
		ImageStack labelImage1,
		ImageStack labelImage2 )
{
	if( labelImage1.getWidth() != labelImage2.getWidth() ||
			labelImage1.getHeight() != labelImage2.getHeight() )
		return -1;
	double intersection = 0;
	double numPix1 = 0;
	double numPix2 = 0;
	// calculate the pixel to pixel intersection
	for( int k = 0; k < labelImage1.getSize(); k ++ )
	{
		final ImageProcessor l1 = labelImage1.getProcessor( k+1 );
		final ImageProcessor l2 = labelImage2.getProcessor( k+1 );
		for( int i = 0; i < labelImage1.getWidth(); i++ )
			for( int j = 0; j < labelImage1.getHeight(); j++ )
			{
				if( l1.getf( i, j ) > 0 ) // skip label 0 (background)
	    		{
	    			numPix1 ++;
	    			if( l1.getf( i, j ) == l2.getf( i, j ) )
	    				intersection ++;
	    		}
				if( l2.getf( i, j ) > 0 )
					numPix2 ++;
			}
	}
    // return the Dice coefficient
    return 2.0 * intersection / ( numPix1 + numPix2 );
}
 
Example 10
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get the Jaccard index (intersection over union overlap) between
 * two label images.
 * @param labelImage1 first label image
 * @param labelImage2 second label image
 * @return Jaccard index value or -1 if error
 * @see <a href="https://en.wikipedia.org/wiki/Jaccard_index">https://en.wikipedia.org/wiki/Jaccard_index</a>
 */
public static final double getJaccardIndex(
		ImageProcessor labelImage1,
		ImageProcessor labelImage2 )
{
	if( labelImage1.getWidth() != labelImage2.getWidth() ||
			labelImage1.getHeight() != labelImage2.getHeight() )
		return -1;
	double intersection = 0;
	double numPix1 = 0;
	double numPix2 = 0;
	// calculate the pixel to pixel intersection
    for( int i = 0; i < labelImage1.getWidth(); i++ )
    	for( int j = 0; j < labelImage1.getHeight(); j++ )
    	{
    		if( labelImage1.getf( i, j ) > 0 ) // skip label 0 (background)
    		{
    			numPix1 ++;
    			if( labelImage1.getf( i, j ) == labelImage2.getf( i, j ) )
    				intersection ++;
    		}
    		if( labelImage2.getf( i, j ) > 0 )
    			numPix2 ++;
    	}
    // return the intersection over the union
    return intersection / ( numPix1 + numPix2 - intersection );
}
 
Example 11
Source File: GeometricMeasures2D.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Counts the number of transitions in the horizontal direction, by counting
 * +1 when the structure touches image edges.
 */
private static final int countTransitionsD00(ImageProcessor image,
		int label, boolean countBorder) 
{
	int width = image.getWidth();
	int height = image.getHeight();

	int count = 0;
	int previous = 0;
	int current;

	// iterate on image pixels
	for (int y = 0; y < height; y++)
	{
		// Count border of image
		previous = (int) image.getf(0, y);
		if (countBorder && previous == label)
			count++;

		// count middle of image
		for (int x = 0; x < width; x++) {
			current = (int) image.getf(x, y);
			if (previous == label ^ current == label) // Exclusive or
				count++;
			previous = current;
		}

		// Count border of image
		if (countBorder && previous == label)
			count++;
	}

	return count;
}
 
Example 12
Source File: LargestInscribedCircle.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Find one position of maximum value within each label.
 * 
 * @param image
 *            the input image containing the value (for example a distance 
 *            map)
 * @param labelImage
 *            the input image containing label of particles
 * @param labels
 *            the set of labels contained in the label image
 *            
 */
private final static Point[] findPositionOfMaxValues(ImageProcessor image,
		ImageProcessor labelImage, int[] labels)
{
	int width 	= labelImage.getWidth();
	int height 	= labelImage.getHeight();
	
	// Compute value of greatest label
	int nbLabel = labels.length;
	int maxLabel = 0;
	for (int i = 0; i < nbLabel; i++)
	{
		maxLabel = Math.max(maxLabel, labels[i]);
	}
	
	// init index of each label
	// to make correspondence between label value and label index
	int[] labelIndex = new int[maxLabel+1];
	for (int i = 0; i < nbLabel; i++)
	{
		labelIndex[labels[i]] = i;
	}
	
	// Init Position and value of maximum for each label
	Point[] posMax 	= new Point[nbLabel];
	int[] maxValues = new int[nbLabel];
	for (int i = 0; i < nbLabel; i++) 
	{
		maxValues[i] = -1;
		posMax[i] = new Point(-1, -1);
	}
	
	// store current value
	int value;
	int index;
	
	// iterate on image pixels
	for (int y = 0; y < height; y++) 
	{
		for (int x = 0; x < width; x++) 
		{
			int label = (int) labelImage.getf(x, y);
			
			// do not process pixels that do not belong to particle
			if (label==0)
				continue;

			index = labelIndex[label];
			
			// update values and positions
			value = image.get(x, y);
			if (value > maxValues[index])
			{
				posMax[index].setLocation(x, y);
				maxValues[index] = value;
			}
		}
	}
			
	return posMax;
}
 
Example 13
Source File: DistanceTransform5x5Float.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void backwardScan(FloatProcessor distMap, ImageProcessor labelImage) 
{
	this.fireStatusChanged(new AlgoEvent(this, "Backward Scan"));
	
	int[] dx = new int[]{+1, -1,  +2, +1,  0, -1, -2,  +1};
	int[] dy = new int[]{+2, +2,  +1, +1, +1, +1, +1,   0};
	
	float[] dw = new float[] { 
			weights[2], weights[2], 
			weights[2], weights[1], weights[0], weights[1], weights[2], 
			weights[0] };
	
	//  size of image
	int sizeX = labelImage.getWidth();
	int sizeY = labelImage.getHeight();

	// Iterate over pixels
	for (int y = sizeY-1; y >= 0; y--)
	{
		this.fireProgressChanged(this, sizeY-1-y, sizeY);
		for (int x = sizeX-1; x >= 0; x--)
		{
			// get current label
			int label = (int) labelImage.getf(x, y);
			
			// do not process background pixels
			if (label == 0)
				continue;
			
			// current distance value
			float currentDist = distMap.getf(x, y);
			float newDist = currentDist;
			
			// iterate over neighbors
			for (int i = 0; i < dx.length; i++)
			{
				// compute neighbor coordinates
				int x2 = x + dx[i];
				int y2 = y + dy[i];
				
				// check bounds
				if (x2 < 0 || x2 >= sizeX)
					continue;
				if (y2 < 0 || y2 >= sizeY)
					continue;
				
				if ((int) labelImage.getf(x2, y2) != label)
				{
					// Update with distance to nearest different label
				    newDist = Math.min(newDist, dw[i]);
				}
				else
				{
					// Increment distance
					newDist = Math.min(newDist, distMap.getf(x2, y2) + dw[i]);
				}
			}
			
			if (newDist < currentDist) 
			{
				distMap.setf(x, y, newDist);
			}
		}
	}
	
	this.fireProgressChanged(this, sizeY, sizeY);
}
 
Example 14
Source File: DistanceTransform5x5Short.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void backwardScan(ShortProcessor distMap, ImageProcessor labelImage) 
{
	this.fireStatusChanged(new AlgoEvent(this, "Backward Scan"));

	// Initialize pairs of offset and weights
	int[] dx = new int[]{+1, -1,  +2, +1,  0, -1, -2,  +1};
	int[] dy = new int[]{+2, +2,  +1, +1, +1, +1, +1,   0};
	short[] dw = new short[] { 
			weights[2], weights[2], 
			weights[2], weights[1], weights[0], weights[1], weights[2], 
			weights[0] };
	
	//  size of image
	int sizeX = labelImage.getWidth();
	int sizeY = labelImage.getHeight();

	// Iterate over pixels
	for (int y = sizeY-1; y >= 0; y--)
	{
		this.fireProgressChanged(this, sizeY-1-y, sizeY);
		for (int x = sizeX-1; x >= 0; x--)
		{
			// get current label
			int label = (int) labelImage.getf(x, y);
			
			// do not process background pixels
			if (label == 0)
				continue;
			
			// current distance value
			int currentDist = distMap.get(x, y);
			int newDist = currentDist;
			
			// iterate over neighbors
			for (int i = 0; i < dx.length; i++)
			{
				// compute neighbor coordinates
				int x2 = x + dx[i];
				int y2 = y + dy[i];
				
				// check bounds
				if (x2 < 0 || x2 >= sizeX)
					continue;
				if (y2 < 0 || y2 >= sizeY)
					continue;
				
				if ((int) labelImage.getf(x2, y2) != label)
				{
					// Update with distance to nearest different label
				    newDist = Math.min(newDist, dw[i]);
				}
				else
				{
					// Increment distance
					newDist = Math.min(newDist, distMap.get(x2, y2) + dw[i]);
				}
			}
			
			if (newDist < currentDist) 
			{
				distMap.set(x, y, newDist);
			}
		}
	}
	
	this.fireProgressChanged(this, sizeY, sizeY);
}
 
Example 15
Source File: Centroid.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Computes centroid of each region in input label image.
 * 
 * @param image
 *            the input image containing label of particles
 * @param labels
 *            the array of labels within the image
 * @param calib
 *            the calibration of the image
 * @return an array of Point2D representing the calibrated centroid coordinates 
 */
public Point2D[] analyzeRegions(ImageProcessor image, int[] labels, Calibration calib)
{
	// Check validity of parameters
	if (image == null)
		return null;

	// size of image
	int width = image.getWidth();
	int height = image.getHeight();

	// Extract spatial calibration
	double sx = 1, sy = 1;
	double ox = 0, oy = 0;
	if (calib != null)
	{
		sx = calib.pixelWidth;
		sy = calib.pixelHeight;
		ox = calib.xOrigin;
		oy = calib.yOrigin;
	}
	
	// create associative array to know index of each label
       HashMap<Integer, Integer> labelIndices = LabelImages.mapLabelIndices(labels);

	// allocate memory for result
	int nLabels = labels.length;
	int[] counts = new int[nLabels];
	double[] cx = new double[nLabels];
	double[] cy = new double[nLabels];

   	fireStatusChanged(this, "Compute centroids");
	// compute centroid of each region
   	for (int y = 0; y < height; y++) 
	{
		for (int x = 0; x < width; x++)
		{
			int label = (int) image.getf(x, y);
			if (label == 0)
				continue;

			int index = labelIndices.get(label);
			cx[index] += x * sx;
			cy[index] += y * sy;
			counts[index]++;
		}
	}

	// normalize by number of pixels in each region
   	Point2D[] points = new Point2D[nLabels];
	for (int i = 0; i < nLabels; i++)
	{
		points[i] = new Point2D.Double(cx[i] / counts[i] + ox, cy[i] / counts[i] + oy);
	}

	return points;
}
 
Example 16
Source File: RegionBoundaries.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Extracts boundary points from a binary region, keeping middle points of
    * pixel edges.
    * 
    * This method considers middle points of pixel edges, assuming a "diamond
    * shape" for pixels. For a single pixel (x,y), ImageJ considers equivalent
    * area to be [x,x+1[ x [y,y+1[, and pixel center at (x+0.5, y+0.5).
    * 
    * The boundaries extracted by this methods have following coordinates:
    * <ul>
    * <li><it>(x+0.5, y)</it>: top boundary</li>
    * <li><it>(x , y+0.5)</it>: left boundary</li>
    * <li><it>(x+1 , y+0.5)</it>: right boundary</li>
    * <li><it>(x+0.5, y+1)</it>: bottom boundary</li>
    * </ul>
    * 
    * @param binaryImage
    *            the image processor containing the binary region
    * @return an array of Point2D, located on the boundary of the region.
    */
public static final ArrayList<Point2D> boundaryPixelsMiddleEdges(ImageProcessor binaryImage)
{
	// size of image
	int sizeX = binaryImage.getWidth();
	int sizeY = binaryImage.getHeight();

	ArrayList<Point2D> points = new ArrayList<Point2D>();
	
	// boolean values within top-left, top-right, left, and current pixels
	boolean[] configValues = new boolean[4];
	
	// iterate on image pixel configurations
	for (int y = 0; y < sizeY + 1; y++) 
	{
	    // assume values outside image correspond to background
		configValues[2] = false;
		
		for (int x = 0; x < sizeX + 1; x++) 
		{
       		// update pixel values of configuration
			configValues[1] = x < sizeX & y > 0 ? (int) binaryImage.getf(x, y - 1) > 0: false;
			configValues[3] = x < sizeX & y < sizeY ? (int) binaryImage.getf(x, y) > 0: false;

			// check boundary with upper pixel
			if (configValues[1] != configValues[3])
			{
				points.add(new Point2D.Double(x + .5, y));
			}
			if (configValues[2] != configValues[3])
			{
				points.add(new Point2D.Double(x, y + .5));
			}

			// update values of configuration for next iteration
			configValues[2] = configValues[3];
		}
	}

	return points;
}
 
Example 17
Source File: LabelValues.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * For each label, finds the position of the point belonging to label region
 * defined by <code>labelImage</code> and with maximal value in intensity
 * image <code>valueImage</code>.
 * 
 * @param valueImage
 *            the intensity image containing values to compare
 * @param labelImage
 *            the intensity image containing label of each pixel
 * @param labels
 *            the list of labels in the label image
 * @return the position of maximum value in intensity image for each label
 */
public static final Point[] findPositionOfMaxValues(ImageProcessor valueImage, 
		ImageProcessor labelImage, int[] labels)
{
	// Create associative map between each label and its index
	Map<Integer,Integer> labelIndices = LabelImages.mapLabelIndices(labels);
	
	// Init Position and value of maximum for each label
	int nLabels = labels.length;
	Point[] posMax 	= new Point[nLabels];
	float[] maxValues = new float[nLabels];
	for (int i = 0; i < nLabels; i++) 
	{
		maxValues[i] = Float.NEGATIVE_INFINITY;
		posMax[i] = new Point(-1, -1);
	}
	
	// iterate on image pixels
	int width 	= labelImage.getWidth();
	int height 	= labelImage.getHeight();
	for (int y = 0; y < height; y++) 
	{
		for (int x = 0; x < width; x++) 
		{
			int label = (int) labelImage.getf(x, y);
			
			// do not process pixels that do not belong to any particle
			if (label == 0)
				continue;

			int index = labelIndices.get(label);
			
			// update values and positions
			float value = valueImage.getf(x, y);
			if (value > maxValues[index]) 
			{
				posMax[index].setLocation(x, y);
				maxValues[index] = value;
			}
		}
	}
			
	return posMax;
}
 
Example 18
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Get the Jaccard index per label (intersection over union overlap) between
 * two label images.
 * @param labelImage1 reference label image
 * @param labelImage2 label image to compare with
 * @return Jaccard index per label in the reference image or null if error
 * @see <a href="https://en.wikipedia.org/wiki/Jaccard_index">https://en.wikipedia.org/wiki/Jaccard_index</a>
 */
public static final ResultsTable getJaccardIndexPerLabel(
		ImageProcessor labelImage1,
		ImageProcessor labelImage2 )
{
	if( labelImage1.getWidth() != labelImage2.getWidth() ||
			labelImage1.getHeight() != labelImage2.getHeight() )
		return null;

	int[] labels1 = findAllLabels( labelImage1 );
	int[] numPix1 = pixelCount( labelImage1, labels1 );
	double[] intersection = new double[ labels1.length ];
	// create associative array to identify the index of each label
    HashMap<Integer, Integer> labelIndices1 = mapLabelIndices( labels1 );

    int[] labels2 = findAllLabels( labelImage2 );
	int[] numPix2 = pixelCount( labelImage2, labels2 );

	// create associative array to identify the index of each label
    HashMap<Integer, Integer> labelIndices2 = mapLabelIndices( labels2 );

	// calculate the pixel to pixel intersection
    for( int i = 0; i < labelImage1.getWidth(); i++ )
    	for( int j = 0; j < labelImage1.getHeight(); j++ )
    		if( labelImage1.getf( i, j ) > 0 ) // skip label 0 (background)
    		{
    			if( labelImage1.getf( i, j ) == labelImage2.getf( i, j ) )
    				intersection[ labelIndices1.get( (int) labelImage1.getf( i, j ) ) ] ++;
    		}
    // return the intersection over the union
    for( int i = 0; i < intersection.length; i ++ )
    {
    	int num2 = labelIndices2.get( labels1[ i ] ) != null ? numPix2[ labelIndices2.get( labels1[ i ] ) ] : 0;
    	intersection[ i ] /= ( numPix1[ i ] + num2 - intersection[ i ] );
    }
    // create data table
		ResultsTable table = new ResultsTable();
		for (int i = 0; i < labels1.length; i++) {
			table.incrementCounter();
			table.addLabel(Integer.toString( labels1[i] ));
			table.addValue("JaccardIndex", intersection[i]);
		}
	    return table;
}
 
Example 19
Source File: Convexity.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Convexity.Result[] analyzeRegions(ImageProcessor image, int[] labels,
		Calibration calib)
{
	// get image size
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	
	// calibrated area of a single pixel
	double pixelArea = calib.pixelWidth * calib.pixelHeight;
	
	// create result array
	Convexity.Result[] res = new Convexity.Result[labels.length];
	
       // compute convex hull of boundary points around each region
       ArrayList<Point2D>[] pointArrays = RegionBoundaries.boundaryPixelsMiddleEdges(image, labels);

       // iterate over labels
	for (int i = 0; i < labels.length; i++)
	{
		// compute convex hull of boundary points around the binary particle
           Polygon2D convexHull = Polygons2D.convexHull(pointArrays[i]);

		// determine bounds
		Box2D box = convexHull.boundingBox();
           int xmin = (int) Math.max(0, Math.floor(box.getXMin()));
           int xmax = (int) Math.min(sizeX, Math.ceil(box.getXMax()));
		int ymin = (int) Math.max(0, Math.floor(box.getYMin()));
		int ymax = (int) Math.min(sizeY, Math.ceil(box.getYMax()));
		
		double area = 0;
		double convexArea = 0;
		
		// iterate over pixels within bounding box
		for (int y = ymin; y < ymax; y++)
		{
			for (int x = xmin; x < xmax; x++)
			{
				if ((int) image.getf(x, y) == labels[i])
				{
					area++;
				}
				if (convexHull.contains(new Point2D.Double(x + 0.5, y + 0.5)))
				{
					convexArea++;
				}
			}
		}
		
		// calibrate measures
           area *= pixelArea;
           convexArea *= pixelArea;
           
           // save convexity measures for this label
           res[i] = new Convexity.Result(area, convexArea);
	}
	
	return res;
}
 
Example 20
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Get the target overlap between two label images (source and target)
 * per each individual labeled region.
 * <p>
 * Target Overlap (for individual label regions r) $TO_r = \frac{ |S_r \cap T_r| }{ |T_r| }$.
 * @param sourceImage source label image
 * @param targetImage target label image
 * @return target overlap per label or null if error
 * @see <a href="http://www.insight-journal.org/browse/publication/707">http://www.insight-journal.org/browse/publication/707</a>
 */
public static final ResultsTable getTargetOverlapPerLabel(
		ImageStack sourceImage,
		ImageStack targetImage )
{
	if( sourceImage.getWidth() != targetImage.getWidth() ||
			sourceImage.getHeight() != targetImage.getHeight() )
		return null;

	int[] sourceLabels = findAllLabels( sourceImage );
	double[] intersection = new double[ sourceLabels.length ];
	// create associative array to identify the index of each label
    HashMap<Integer, Integer> sourceLabelIndices = mapLabelIndices( sourceLabels );

    int[] targetLabels = findAllLabels( targetImage );
	int[] numPixTarget = voxelCount( targetImage, targetLabels );

	// create associative array to identify the index of each label
    HashMap<Integer, Integer> targetLabelIndices = mapLabelIndices( targetLabels );
    // calculate the pixel to pixel intersection
 	for( int k = 0; k < sourceImage.getSize(); k ++ )
 	{
 		final ImageProcessor ls = sourceImage.getProcessor( k+1 );
		final ImageProcessor lt = targetImage.getProcessor( k+1 );
 		for( int i = 0; i < sourceImage.getWidth(); i++ )
 			for( int j = 0; j < sourceImage.getHeight(); j++ )
 				if( ls.getf( i, j ) > 0 ) // skip label 0 (background)
 				{
 					if( ls.getf( i, j ) == lt.getf( i, j ) )
 						intersection[ sourceLabelIndices.get( (int) ls.getf( i, j ) ) ] ++;
 				}
 	}
    // return the target overlap
    for( int i = 0; i < intersection.length; i ++ )
    	intersection[ i ] = targetLabelIndices.get( sourceLabels[ i ] ) != null ?
    			intersection[ i ] / numPixTarget[ targetLabelIndices.get( sourceLabels[ i ] ) ] : 0;
	// create data table
	ResultsTable table = new ResultsTable();
	for (int i = 0; i < sourceLabels.length; i++) {
		table.incrementCounter();
		table.addLabel(Integer.toString( sourceLabels[i] ));
		table.addValue("TargetOverlap", intersection[i]);
	}
    return table;
}