private synchronized void setLevel(int level) { // System.out.println("level "+level); if (level > 0) { ImageSingleBand small = ss.getLayer(level - 1); ImageSingleBand enlarge = GeneralizedImageOps.createSingleBand( small.getClass(), ss.getInputWidth(), ss.getInputHeight()); DistortImageOps.scale(small, enlarge, TypeInterpolate.NEAREST_NEIGHBOR); // if the size isn't the same null it so a new image will be declared if (levelImage != null && (levelImage.getWidth() != enlarge.width || levelImage.getHeight() != enlarge.height)) { levelImage = null; } levelImage = ConvertBufferedImage.convertTo(enlarge, levelImage); double scale = ss.getScale(level - 1); levelPoints.clear(); for (ScalePoint p : points) { if (p.scale == scale) { levelPoints.add(p); } } } else { levelPoints.clear(); levelPoints.addAll(points); } this.activeLevel = level; }
public static BufferedImage standard(ImageSingleBand<?> src, BufferedImage dst) { if (src.getDataType().isInteger()) { ImageInteger srcInt = (ImageInteger) src; if (src.getDataType().isSigned()) { double max = GImageStatistics.maxAbs(srcInt); return colorizeSign(srcInt, dst, (int) max); } else { if (src.getDataType().getNumBits() == 8) { dst = ConvertBufferedImage.convertTo((ImageUInt8) src, dst); } else { double max = GImageStatistics.maxAbs(srcInt); dst = grayUnsigned(srcInt, dst, (int) max); } } } else if (ImageFloat32.class.isAssignableFrom(src.getClass())) { ImageFloat32 img = (ImageFloat32) src; float max = ImageStatistics.maxAbs(img); boolean hasNegative = false; for (int i = 0; i < img.getHeight(); i++) { for (int j = 0; j < img.getWidth(); j++) { if (img.get(j, i) < 0) { hasNegative = true; break; } } } if (hasNegative) return colorizeSign(img, dst, (int) max); else return grayMagnitude((ImageFloat32) src, dst, max); } return dst; }
public void process(BufferedImage image) { setInputImage(image); T gray = ConvertBufferedImage.convertFromSingle(image, null, imageType); ss.setImage(gray); gui.reset(); for (int i = 0; i < ss.getTotalScales(); i++) { ss.setActiveScale(i); double scale = ss.getCurrentScale(); T scaledImage = ss.getScaledImage(); BufferedImage b = ConvertBufferedImage.convertTo(scaledImage, null); gui.addImage(b, String.format("Scale %6.2f", scale)); } processedImage = true; }
/** * Renders a colored image where the color indicates the sign and intensity its magnitude. The * input is divided by normalize to render it in the appropriate scale. * * @param src Input single band image. * @param dst Where the image is rendered into. If null a new BufferedImage will be created and * return. * @param normalize Used to normalize the input image. If <= 0 then the max value will be used * @return Rendered image. */ public static BufferedImage colorizeSign( ImageSingleBand src, BufferedImage dst, double normalize) { dst = checkInputs(src, dst); if (normalize <= 0) { normalize = GImageStatistics.maxAbs(src); } if (normalize == 0) { // sets the output to black ConvertBufferedImage.convertTo(src, dst, true); return dst; } if (src.getClass().isAssignableFrom(ImageFloat32.class)) { return colorizeSign((ImageFloat32) src, dst, (float) normalize); } else { return colorizeSign((ImageInteger) src, dst, (int) normalize); } }
public static void main(String[] args) throws IOException { if (args.length == 0) { System.out.println( "usage: crop-objects [OPTION]... FILE [DIR]\n" + "crops detected objects from image FILE and writes their subimages to files. \n" + "Can specify the DIR in which to create the files, otherwise subimage files are " + "created in the same directory as FILE is in by default. \n" + "-t [n] [m] set the high and low threshold values. n and m are values between 0 and 1."); System.exit(1); } // interpret options and read arguments if (args[0].equals("-t")) { threshLow = Float.parseFloat(args[1]); threshHigh = Float.parseFloat(args[2]); filename = args[3]; if (args.length == 5) { dir = args[4]; } } else { filename = args[0]; if (args.length == 2) { dir = args[1]; } } // get path to directory file is in String name = FilenameUtils.removeExtension(filename); // get image BufferedImage image = UtilImageIO.loadImage(new File(filename).getAbsolutePath()); if (image == null) { System.out.println( "usage: crop-objects [OPTION]... FILE [DIR]\n" + "crops detected objects from image FILE and writes their subimages to files. \n" + "Can specify the DIR in which to create the files, otherwise subimage files are " + "created in the same directory as FILE is in by default. \n" + "-t [n] [m] set the high and low threshold values. n and m are values between 0 and 1."); System.exit(1); } minsize = (int) (0.1 * Math.min(image.getHeight(), image.getWidth())); // find objects in image // generate candidate contours ArrayList<List<PointIndex_I32>> objects = new ArrayList<List<PointIndex_I32>>(); List<BufferedImage> results = new ArrayList<BufferedImage>(); List<List<PointIndex_I32>> candidates = new ArrayList<List<PointIndex_I32>>(); ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class); BufferedImage bw = ConvertBufferedImage.convertTo( input, new BufferedImage(image.getWidth(), image.getHeight(), image.getType())); File binaryfile = new File(name + "_" + "binary.png"); ImageIO.write(bw, "png", binaryfile); ImageUInt8 binary = new ImageUInt8(input.width, input.height); // Finds edges inside the image CannyEdge<ImageFloat32, ImageFloat32> canny = FactoryEdgeDetectors.canny( blurRadius, true, dynamicThreshold, ImageFloat32.class, ImageFloat32.class); canny.process(input, threshLow, threshHigh, binary); List<Contour> contours = BinaryImageOps.contour(binary, rule, null); BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, null); File cannyfile = new File(name + "_" + "canny.png"); ImageIO.write(visualBinary, "png", cannyfile); BufferedImage cannyContour = VisualizeBinaryData.renderExternal(contours, null, binary.width, binary.height, null); File cannyContourfile = new File(name + "_" + "contour.png"); ImageIO.write(cannyContour, "png", cannyContourfile); for (Contour c : contours) { // Only the external contours are relevant. List<PointIndex_I32> vertices = ShapeFittingOps.fitPolygon(c.external, true, toleranceDist, toleranceAngle, 100); candidates.add(vertices); } for (List<PointIndex_I32> vertices : candidates) { try { Candidate c = new Candidate(vertices, image); if (c.size(minsize)) { c.rotate(); results.add(c.getImage()); objects.add(vertices); } } catch (Exception e) { System.out.println("Error creating candidate from contour " + e.getMessage()); } } // write subimages of objects to files int i = 0; for (BufferedImage obj : results) { // print images to file try { File outputfile = new File(name + "_" + i + ".png"); i++; ImageIO.write(obj, "png", outputfile); } catch (IOException e) { System.out.println("Error writing subimages" + e.getMessage()); } } // draw objects onto original image and save Draw.drawPolygons(objects, image); File outputfile = new File(name + "_" + "annotated.png"); ImageIO.write(image, "png", outputfile); }