/** * Takes an image and returns a transformed image. * Uses the random object in the case of random transformations. * * @param image to transform, null == end of stream * @param random object to use (or null for deterministic) * @return transformed image */ @Override public ImageWritable transform(ImageWritable image, Random random) { if (image == null) { return null; } Mat mat = converter.convert(image.getFrame()); int top = random != null ? random.nextInt(cropTop + 1) : cropTop; int left = random != null ? random.nextInt(cropLeft + 1) : cropLeft; int bottom = random != null ? random.nextInt(cropBottom + 1) : cropBottom; int right = random != null ? random.nextInt(cropRight + 1) : cropRight; int y = Math.min(top, mat.rows() - 1); int x = Math.min(left, mat.cols() - 1); int h = Math.max(1, mat.rows() - bottom - y); int w = Math.max(1, mat.cols() - right - x); Mat result = mat.apply(new Rect(x, y, w, h)); return new ImageWritable(converter.convert(result)); }
int y = (cvImage.rows() - N) * j / jMax; Mat patch = cvImage.apply(new Rect(x, y, N, N)).clone();
Rect boundingRect = new Rect(); MatVector contours = new MatVector(); Mat hierarchy = new Mat();
for (int j = 0; j < jMax; j++) { int y = ((cvImage.rows() - N) * j) / jMax; Mat patch = cvImage.apply(new Rect(x, y, N, N)); opencv_core.add(patch, mean, mean);
/** * Takes an image and returns a randomly cropped image. * * @param image to transform, null == end of stream * @param random object to use (or null for deterministic) * @return transformed image */ @Override public ImageWritable transform(ImageWritable image, Random random) { if (image == null) { return null; } // ensure that transform is valid if (image.getFrame().imageHeight < outputHeight || image.getFrame().imageWidth < outputWidth) throw new UnsupportedOperationException( "Output height/width cannot be more than the input image. Requested: " + outputHeight + "+x" + outputWidth + ", got " + image.getFrame().imageHeight + "+x" + image.getFrame().imageWidth); // determine boundary to place random offset int cropTop = image.getFrame().imageHeight - outputHeight; int cropLeft = image.getFrame().imageWidth - outputWidth; Mat mat = converter.convert(image.getFrame()); int top = rng.nextInt(cropTop + 1); int left = rng.nextInt(cropLeft + 1); int y = Math.min(top, mat.rows() - 1); int x = Math.min(left, mat.cols() - 1); Mat result = mat.apply(new Rect(x, y, outputWidth, outputHeight)); return new ImageWritable(converter.convert(result)); }
@Override public ImageWritable transform(ImageWritable image, Random random) { if (image == null) { return null; } Mat mat = converter.convert(image.getFrame()); int top = random != null ? random.nextInt(cropTop + 1) : cropTop; int left = random != null ? random.nextInt(cropLeft + 1) : cropLeft; int bottom = random != null ? random.nextInt(cropBottom + 1) : cropBottom; int right = random != null ? random.nextInt(cropRight + 1) : cropRight; int y = Math.min(top, mat.rows() - 1); int x = Math.min(left, mat.cols() - 1); int h = Math.max(1, mat.rows() - bottom - y); int w = Math.max(1, mat.cols() - right - x); Mat result = mat.apply(new Rect(x, y, w, h)); return new ImageWritable(converter.convert(result)); } }
protected Mat centerCropIfNeeded(Mat img) { int x = 0; int y = 0; int height = img.rows(); int width = img.cols(); int diff = Math.abs(width - height) / 2; if (width > height) { x = diff; width = width - diff; } else if (height > width) { y = diff; height = height - diff; } return img.apply(new Rect(x, y, width, height)); }
protected Mat centerCropIfNeeded(Mat img) { int x = 0; int y = 0; int height = img.rows(); int width = img.cols(); int diff = Math.abs(width - height) / 2; if (width > height) { x = diff; width = width - diff; } else if (height > width) { y = diff; height = height - diff; } return img.apply(new Rect(x, y, width, height)); }