@Override public ImageWritable transform(ImageWritable image, Random random) { if (image == null) { return null; } Mat mat = converter.convert(image.getFrame()); int h = Math.round(mat.rows() + dy * (random != null ? 2 * random.nextFloat() - 1 : 1)); int w = Math.round(mat.cols() + dx * (random != null ? 2 * random.nextFloat() - 1 : 1)); Mat result = new Mat(); resize(mat, result, new Size(w, h)); 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)); } }
@Override public ImageWritable transform(ImageWritable image, Random random) { if (image == null) { return null; } Mat mat = converter.convert(image.getFrame()); int h = Math.round(mat.rows() + dy * (random != null ? 2 * random.nextFloat() - 1 : 1)); int w = Math.round(mat.cols() + dx * (random != null ? 2 * random.nextFloat() - 1 : 1)); Mat result = new Mat(); resize(mat, result, new Size(w, h)); return new ImageWritable(converter.convert(result)); } }
/** * 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 = (Mat) converter.convert(image.getFrame()); Mat result = new Mat(); try { cvtColor(mat, result, conversionCode); } catch (Exception e) { throw new RuntimeException(e); } 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 mode = random != null ? random.nextInt(4) - 2 : flipMode; Mat result = new Mat(); if (mode < -1) { // no flip mat.copyTo(result); } else { flip(mat, result, mode); } 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 mode = random != null ? random.nextInt(4) - 2 : flipMode; Mat result = new Mat(); if (mode < -1) { // no flip mat.copyTo(result); } else { flip(mat, result, mode); } return new ImageWritable(converter.convert(result)); } }
/** * 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)); }
@Override public ImageWritable transform(ImageWritable image, Random random) { if (image == null) { return null; } Mat mat = converter.convert(image.getFrame()); float cy = mat.rows() / 2 + centery * (random != null ? 2 * random.nextFloat() - 1 : 1); float cx = mat.cols() / 2 + centerx * (random != null ? 2 * random.nextFloat() - 1 : 1); float a = angle * (random != null ? 2 * random.nextFloat() - 1 : 1); float s = 1 + scale * (random != null ? 2 * random.nextFloat() - 1 : 1); Mat result = new Mat(); Mat M = getRotationMatrix2D(new Point2f(cx, cy), angle, scale); warpAffine(mat, result, M, mat.size(), interMode, borderMode, borderValue); return new ImageWritable(converter.convert(result)); }
/** * 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; } opencv_core.Mat mat = converter.convert(image.getFrame()); opencv_core.Mat result = new opencv_core.Mat(); resize(mat, result, new opencv_core.Size(newWidth, newHeight)); return new ImageWritable(converter.convert(result)); }
@Override public Frame grab() throws Exception { try { final byte[] b = readImage(); final Mat mat = new Mat(1, b.length, CV_8UC1, new BytePointer(b)); releaseDecoded(); return converter.convert(decoded = imdecode(mat, IMREAD_COLOR)); } catch (IOException e) { throw new Exception(e.getMessage(), e); } }
public Frame grab() throws Exception { IplImage image = depth ? grabDepth() : grabVideo(); int w = image.width(); int h = image.height(); int iplDepth = image.depth(); int channels = image.nChannels(); if (imageMode == ImageMode.COLOR && channels == 1) { if (returnImage == null) { returnImage = IplImage.create(w, h, iplDepth, 3); } cvCvtColor(image, returnImage, CV_GRAY2BGR); return converter.convert(returnImage); } else if (imageMode == ImageMode.GRAY && channels == 3) { if (returnImage == null) { returnImage = IplImage.create(w, h, iplDepth, 1); } cvCvtColor(image, returnImage, CV_BGR2GRAY); return converter.convert(returnImage); } else { return converter.convert(image); } } }
/** Grab one frame; * the caller have to make a copy of returned image before processing. * * It will throw null pointer exception if not started before grabbing. * @return "read-only" RGB, 4-channel or GRAY/1-channel image, it throws exception if no image is available */ @Override public Frame grab() throws Exception { IplImage img = null; switch (triggered) { case NO_TRIGGER: img = grab_RGB4(); break; case HAS_FRAME: triggered = Triggered.NO_TRIGGER; img = makeImage(ps3_frame); break; case NO_FRAME: triggered = Triggered.NO_TRIGGER; return null; default: // just schizophrenia - for future enhancement throw new Exception("Int. error - unknown triggering state"); } if ((img != null) && (imageMode == ImageMode.GRAY)) { cvCvtColor(img, image_1ch, CV_RGB2GRAY); img = image_1ch; } return converter.convert(img); }
/** * 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 = (Mat) converter.convert(image.getFrame()); Mat result = new Mat(); try { if (mat.channels() == 1) { equalizeHist(mat, result); } else { split(mat, splitChannels); equalizeHist(splitChannels.get(0), splitChannels.get(0)); //equalize histogram on the 1st channel (Y) merge(splitChannels, result); } } catch (Exception e) { throw new RuntimeException(e); } return new ImageWritable(converter.convert(result)); }
public Frame grab() throws Exception { if (myVideoInput == null) { throw new Exception("videoInput is null. (Has start() been called?)"); } int w = myVideoInput.getWidth(deviceNumber), h = myVideoInput.getHeight(deviceNumber); if (bgrImage == null || bgrImage.width() != w || bgrImage.height() != h) { bgrImage = IplImage.create(w, h, IPL_DEPTH_8U, 3); bgrImageData = bgrImage.imageData(); } if (!myVideoInput.getPixels(deviceNumber, bgrImageData, false, true)) { throw new Exception("videoInput.getPixels() Error: Could not get pixels."); } timestamp = System.nanoTime()/1000; if (imageMode == ImageMode.GRAY) { if (grayImage == null) { grayImage = IplImage.create(w, h, IPL_DEPTH_8U, 1); } cvCvtColor(bgrImage, grayImage, CV_BGR2GRAY); return converter.convert(grayImage); } else { return converter.convert(bgrImage); } } }
/** * 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()); Point2f src = new Point2f(4); Point2f dst = new Point2f(4); src.put(0, 0, mat.cols(), 0, mat.cols(), mat.rows(), 0, mat.rows()); for (int i = 0; i < 8; i++) { dst.put(i, src.get(i) + deltas[i] * (random != null ? 2 * random.nextFloat() - 1 : 1)); } Mat result = new Mat(); Mat M = getPerspectiveTransform(src, dst); warpPerspective(mat, result, M, mat.size()); return new ImageWritable(converter.convert(result)); } }
/** * 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()); Point2f src = new Point2f(4); Point2f dst = new Point2f(4); src.put(0, 0, mat.cols(), 0, mat.cols(), mat.rows(), 0, mat.rows()); for (int i = 0; i < 8; i++) { dst.put(i, src.get(i) + deltas[i] * (random != null ? 2 * random.nextFloat() - 1 : 1)); } Mat result = new Mat(); Mat M = getPerspectiveTransform(src, dst); warpPerspective(mat, result, M, mat.size(), interMode, borderMode, borderValue); return new ImageWritable(converter.convert(result)); }
return converter.convert(return_image);