Java Code Examples for ij.process.ImageProcessor#getWidth()
The following examples show how to use
ij.process.ImageProcessor#getWidth() .
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 |
public static final ImageProcessor not(ImageProcessor image) { int width = image.getWidth(); int height = image.getHeight(); ImageProcessor result = image.duplicate(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { result.set(x, y, ~image.get(x, y)); } } return result; }
Example 2
Source File: BinaryOrbit.java From orbit-image-analysis with GNU General Public License v3.0 | 6 votes |
public void fill(ImageProcessor ip, int foreground, int background) { int width = ip.getWidth(); int height = ip.getHeight(); FloodFiller ff = new FloodFiller(ip); ip.setColor(127); for (int y=0; y<height; y++) { if (ip.getPixel(0,y)==background) ff.fill(0, y); if (ip.getPixel(width-1,y)==background) ff.fill(width-1, y); } for (int x=0; x<width; x++){ if (ip.getPixel(x,0)==background) ff.fill(x, 0); if (ip.getPixel(x,height-1)==background) ff.fill(x, height-1); } byte[] pixels = (byte[])ip.getPixels(); int n = width*height; for (int i=0; i<n; i++) { if (pixels[i]==127) pixels[i] = (byte)background; else pixels[i] = (byte)foreground; } }
Example 3
Source File: Threshold.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 6 votes |
/** * 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 4
Source File: NonLinearTransform.java From TrakEM2 with GNU General Public License v3.0 | 6 votes |
public ImageProcessor[] transform(final ImageProcessor ip){ if (!precalculated) this.precalculateTransfom(); final ImageProcessor newIp = ip.createProcessor(ip.getWidth(), ip.getHeight()); if (ip instanceof ColorProcessor) ip.max(0); final ImageProcessor maskIp = new ByteProcessor(ip.getWidth(),ip.getHeight()); for (int x=0; x < width; x++){ for (int y=0; y < height; y++){ if (transField[x][y][0] == -1){ continue; } newIp.set(x, y, (int) ip.getInterpolatedPixel((int)transField[x][y][0],(int)transField[x][y][1])); maskIp.set(x,y,255); } } return new ImageProcessor[]{newIp, maskIp}; }
Example 5
Source File: DistanceTransform3x3Float.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 6 votes |
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: ShiftedCross3x3Strel_LeftTest.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 6 votes |
@Test public void testErosion_Square4x4() { ImageProcessor image = createImage_Square4x4(); Strel strel = ShiftedCross3x3Strel.LEFT; ImageProcessor expected = image.createProcessor(10, 10); for (int y = 4; y < 6; y++) { for (int x = 5; x < 7; x++) { expected.set(x, y, 255); } } ImageProcessor result = strel.erosion(image); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { int exp = expected.get(x, y); int res = result.get(x, y); if(expected.get(x, y) != result.get(x, y)) { System.out.println("At x=" + x + " and y=" + y + ", exp=" + exp + " and res = " + res); } assertEquals(exp, res); } } }
Example 7
Source File: ColorImages.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 5 votes |
/** * Splits the channels of the color image into three new instances of * ByteProcessor. * * @param image * the original image, assumed to be a ColorProcessor * @return a collection containing the red, green and blue channels */ public static final Collection<ByteProcessor> splitChannels(ImageProcessor image) { if (!(image instanceof ColorProcessor)) { throw new IllegalArgumentException("Requires an instance of ColorProcessor"); } // size of input image int width = image.getWidth(); int height = image.getHeight(); int size = width * height; // Extract red, green and blue components byte[] redArray = new byte[size]; byte[] greenArray = new byte[size]; byte[] blueArray = new byte[size]; ((ColorProcessor) image).getRGB(redArray, greenArray, blueArray); // create image processor for each channel ByteProcessor red = new ByteProcessor(width, height, redArray, null); ByteProcessor green = new ByteProcessor(width, height, greenArray, null); ByteProcessor blue = new ByteProcessor(width, height, blueArray, null); // concatenate channels into a new collection ArrayList<ByteProcessor> result = new ArrayList<ByteProcessor>(3); result.add(red); result.add(green); result.add(blue); return result; }
Example 8
Source File: DiamondStrelTest.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void testDilation_Square4x4() { ImageProcessor image = createImage_Square4x4(); Strel strel = new DiamondStrel(5); ImageProcessor expected = image.createProcessor(10, 10); for (int x = 3; x < 7; x++) { expected.set(x, 1, 255); expected.set(x, 8, 255); } for (int x = 2; x < 8; x++) { expected.set(x, 2, 255); expected.set(x, 7, 255); } for (int y = 3; y < 7; y++) { for (int x = 1; x < 9; x++) { expected.set(x, y, 255); } } ImageProcessor result = strel.dilation(image); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { int exp = expected.get(x, y); int res = result.get(x, y); if(expected.get(x, y) != result.get(x, y)) { System.out.println("At x=" + x + " and y=" + y + ", exp=" + exp + " and res = " + res); } assertEquals(exp, res); } } }
Example 9
Source File: OctagonStrelTest.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 5 votes |
/** * Try to compute morphological closing with a strel larger than the * original image. */ @Test public void testClosing_VeryBigStrel() { ImageProcessor image = createImage_Square4x4(); Strel strel = new OctagonStrel(30); ImageProcessor result = strel.closing(image); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { assertEquals(255, result.get(x, y)); } } }
Example 10
Source File: LinearHorizontalStrel.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 5 votes |
private void inPlaceErosionFloat(ImageProcessor image) { // get image size int width = image.getWidth(); int height = image.getHeight(); // shifts between reference position and last position int shift = this.size - this.offset - 1; // create local histogram instance LocalExtremumBufferDouble localMin = new LocalExtremumBufferDouble(size, LocalExtremum.Type.MINIMUM); // Iterate on image rows for (int y = 0; y < height; y++) { fireProgressChanged(this, y, height); // reset local histogram localMin.fill(Float.POSITIVE_INFINITY); // init local histogram with neighbor values for (int x = 0; x < Math.min(shift, width); x++) { localMin.add(image.getf(x, y)); } // iterate along "middle" values for (int x = 0; x < width - shift; x++) { localMin.add(image.getf(x + shift, y)); image.setf(x, y, (float) localMin.getMax()); } // process pixels at the end of the line for (int x = Math.max(0, width - shift); x < width; x++) { localMin.add(Float.POSITIVE_INFINITY); image.setf(x, y, (float) localMin.getMax()); } } // clear the progress bar fireProgressChanged(this, height, height); }
Example 11
Source File: ColorImages.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 5 votes |
/** * Assumes reference image contains a ColorProcessor. */ private final static ImageProcessor binaryOverlayRGB(ImageProcessor refImage, ImageProcessor mask, Color color) { int width = refImage.getWidth(); int height = refImage.getHeight(); ColorProcessor result = new ColorProcessor(width, height); int value; int rgbValue = color.getRGB(); // Iterate on image pixels, and choose result value depending on mask for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if(mask.get(x, y) == 0) { // choose RGB value directly from reference image value = refImage.get(x, y); result.set(x, y, value); } else { // set value to chosen color result.set(x, y, rgbValue); } } } return result; }
Example 12
Source File: ValueToNoise.java From TrakEM2 with GNU General Public License v3.0 | 5 votes |
final static private void processGray(final ImageProcessor ip, final int value, final int min, final int max) { final int scale = max - min + 1; final Random rnd = new Random(); final int n = ip.getWidth() * ip.getHeight(); for (int i =0; i < n; ++i) { final int v = ip.get(i); if (v == value) ip.set(i, rnd.nextInt(scale) + min); } }
Example 13
Source File: Reconstruction.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 5 votes |
/** * Removes the border of the input image, by performing a morphological * reconstruction initialized with image boundary. * * @see #fillHoles(ImageProcessor) * * @param image the image to process * @return a new image with borders removed */ public final static ImageProcessor killBorders(ImageProcessor image) { // Image size int width = image.getWidth(); int height = image.getHeight(); // Initialize marker image with zeros everywhere except at borders ImageProcessor markers = image.duplicate(); for (int y = 1; y < height-1; y++) { for (int x = 1; x < width-1; x++) { markers.setf(x, y, Float.NEGATIVE_INFINITY); } } // Reconstruct image from borders to find touching structures ImageProcessor result = reconstructByDilation(markers, image); // removes result from original image for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int val = image.get(x, y) - result.get(x, y); result.set(x, y, Math.max(val, 0)); } } return result; }
Example 14
Source File: LinearDiagDownStrel.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 4 votes |
private void inPlaceErosionGray8(ImageProcessor image) { // get image size int width = image.getWidth(); int height = image.getHeight(); // Consider all diagonal lines with direction vector (+1,+1) that intersect image. // Diagonal lines are identified by their intersection "d" with axis (-1,+1) // Need to identify bounds for d int dmin = -(width - 1); int dmax = height - 1; // compute shifts int dt0 = this.offset; // create local histogram instance LocalExtremumBufferGray8 localMin = new LocalExtremumBufferGray8(size, LocalExtremum.Type.MINIMUM); // Iterate on diagonal lines for (int d = dmin; d < dmax; d++) { fireProgressChanged(this, d - dmin, dmax - dmin); // reset local histogram localMin.fill(Strel.FOREGROUND); int xmin = Math.max(0, -d); int xmax = Math.min(width, height - d); int ymin = Math.max(0, d); int ymax = Math.min(height, d - width); int tmin = Math.max(xmin, d - ymin); int tmax = Math.min(xmax, d - ymax); // position on the line int t = tmin; // init local histogram image values after current pos while (t < Math.min(tmin + dt0, tmax)) { localMin.add(image.get(t, t + d)); t++; } // process position that do not touch lower-right image boundary while (t < tmax) { localMin.add(image.get(t, t + d)); image.set(t - dt0, t + d - dt0, localMin.getMax()); t++; } // process pixels at the end of the line // and that do not touch the upper left image boundary while (t < tmax + dt0) { localMin.add(Strel.FOREGROUND); int x = t - dt0; int y = t + d - dt0; if (x >= 0 && y >= 0 && x < width && y < height) image.set(x, y, localMin.getMax()); t++; } } // clear the progress bar fireProgressChanged(this, dmax - dmin, dmax - dmin); }
Example 15
Source File: LabelImages.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 4 votes |
/** * Merge several regions identified by their label, filling the gap between * former regions. Labels are merged in place (i.e., the input image is * modified). The background pixels are merged only if they are neighbor of * at least two regions to be merged, and of no other region. * * @param image * a label planar image * @param labels * the list of labels to replace * @param newLabel * the new value for labels * @param conn * the connectivity to check neighbors of background pixels */ public static final void mergeLabelsWithGap(ImageProcessor image, float[] labels, float newLabel, int conn) { // get image size int sizeX = image.getWidth(); int sizeY = image.getHeight(); // convert array to tree (to accelerate search) TreeSet<Float> labelSet = new TreeSet<Float>(); for (int i = 0; i < labels.length; i++) { labelSet.add(labels[i]); } // determines the shift to compute neighbor coordinates int[][] shifts; if (conn == 4) { shifts = new int[][] {{0, -1}, {-1, 0}, {1, 0}, {0, 1}}; } else if (conn == 8) { shifts = new int[][] { { -1, -1 }, { 0, -1 }, { 1, -1 }, // previous line { -1, 0 }, { 1, 0 }, // current line { -1, 1 }, { 0, 1 }, { 1, 1 } }; // next line } else { throw new IllegalArgumentException("Connectivity value should be either 4 or 8."); } // create structure to store the boundary pixels ArrayList<Cursor2D> boundaryPixels = new ArrayList<Cursor2D>(); // process background pixels in-between two or more labels for (int y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++) { // focus on background pixels if (image.getf(x, y) != 0) continue; // extract the neighbor labels ArrayList<Float> neighborLabels = new ArrayList<Float>(shifts.length); for (int[] shift : shifts) { int x2 = x + shift[0]; if (x2 < 0 || x2 >= sizeX) continue; int y2 = y + shift[1]; if (y2 < 0 || y2 >= sizeY) continue; float value2 = image.getf(x2, y2); if (value2 == 0) continue; if (!neighborLabels.contains(value2)) { neighborLabels.add(value2); } } // count number of neighbors if (neighborLabels.size() > 1 && labelSet.containsAll(neighborLabels)) boundaryPixels.add(new Cursor2D(x, y)); } } // replace label value of all regions replaceLabels(image, labels, newLabel); for (Cursor2D c : boundaryPixels) image.setf(c.getX(), c.getY(), newLabel); }
Example 16
Source File: DistanceTransform5x5Float.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 4 votes |
private void forwardScan(FloatProcessor distMap, ImageProcessor labelImage) { this.fireStatusChanged(new AlgoEvent(this, "Forward 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}; 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 = 0; y < sizeY; y++) { this.fireProgressChanged(this, y, sizeY); for (int x = 0; x < sizeX; 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 17
Source File: DistanceTransform5x5Short.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 4 votes |
private void forwardScan(ShortProcessor distMap, ImageProcessor labelImage) { this.fireStatusChanged(new AlgoEvent(this, "Forward 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 = 0; y < sizeY; y++) { this.fireProgressChanged(this, y, sizeY); for (int x = 0; x < sizeX; 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 18
Source File: ShiftedCross3x3Strel.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 4 votes |
private void inPlaceDilationGray8(ImageProcessor image) { // size of image int width = image.getWidth(); int height = image.getHeight(); int[][] buffer = new int[3][width]; // init buffer with background and first two lines for (int x = 0; x < width; x++) { buffer[0][x] = Strel.BACKGROUND; buffer[1][x] = Strel.BACKGROUND; buffer[2][x] = image.get(x, 0); } // Iterate over image lines int valMax; for (int y = 0; y < height; y++) { fireProgressChanged(this, y, height); // permute lines in buffer int[] tmp = buffer[0]; buffer[0] = buffer[1]; buffer[1] = buffer[2]; // initialize values of the last line in buffer if (y < height - 1) { for (int x = 0; x < width; x++) tmp[x] = image.get(x, y+1); } else { for (int x = 0; x < width; x++) tmp[x] = Strel.BACKGROUND; } buffer[2] = tmp; // process first two pixels independently valMax = Math.max(buffer[1][0], Strel.BACKGROUND); image.set(0, y, valMax); valMax = max5(buffer[0][0], buffer[1][0], buffer[1][1], buffer[2][0], Strel.BACKGROUND); image.set(1, y, valMax); // Iterate over pixel of the line, starting from the third one for (int x = 2; x < width; x++) { valMax = max5(buffer[0][x-1], buffer[1][x-2], buffer[1][x-1], buffer[1][x], buffer[2][x-1]); image.set(x, y, valMax); } } // clear the progress bar fireProgressChanged(this, height, height); }
Example 19
Source File: LabelImages.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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( ImageProcessor sourceImage, ImageProcessor 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 = pixelCount( 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 i = 0; i < sourceImage.getWidth(); i++ ) for( int j = 0; j < sourceImage.getHeight(); j++ ) if( sourceImage.getf( i, j ) > 0 ) // skip label 0 (background) { if( sourceImage.getf( i, j ) == targetImage.getf( i, j ) ) intersection[ sourceLabelIndices.get( (int) sourceImage.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; }
Example 20
Source File: RegionalExtremaByFlooding.java From MorphoLibJ with GNU Lesser General Public License v3.0 | 4 votes |
/** * Computes regional extrema in current input image, using * flood-filling-like algorithm with 4 connectivity. * Computations are made with double values. */ private ImageProcessor regionalExtremaC4(ImageProcessor image) { // get image size int sizeX = image.getWidth(); int sizeY = image.getHeight(); // allocate memory for output, and fill with 255 ImageProcessor result = new ByteProcessor(sizeX, sizeY); result.setValue(255); result.fill(); // initialize local data depending on extrema type int sign = 1; if (this.extremaType == ExtremaType.MAXIMA) { sign = -1; } // Iterate over image pixels for (int y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++) { // Check if current pixel was already processed if (result.getf(x, y) == 0) continue; // current value float currentValue = image.getf(x, y); // compute extrema value in 4-neighborhood (computes max value // if sign is -1) float value = currentValue * sign; if (x > 0) value = min(value, image.getf(x-1, y) * sign); if (y > 0) value = min(value, image.getf(x, y-1) * sign); if (x < sizeX - 1) value = min(value, image.getf(x+1, y) * sign); if (y < sizeY - 1) value = min(value, image.getf(x, y+1) * sign); // if one of the neighbors of current pixel has a lower (resp. // greater) value, the the current pixel is not an extremum. // Consequently, the current pixel, and all its connected // neighbors with same value are set to 0 in the output image. if (value < currentValue * sign) { FloodFill.floodFillFloat(image, x, y, result, 0.f, 4); } } } return result; }