public static void main(String[] args) { Mat pFrame = imread("image0.png", CV_LOAD_IMAGE_GRAYSCALE); Mat cFrame = imread("image1.png", CV_LOAD_IMAGE_GRAYSCALE); Mat pGray = new Mat(); Mat cGray = new Mat(); pFrame.convertTo(pGray, CV_32FC1); cFrame.convertTo(cGray, CV_32FC1); Mat Optical_Flow = new Mat(); DenseOpticalFlow tvl1 = createOptFlow_DualTVL1(); tvl1.calc(pGray, cGray, Optical_Flow); Mat OF = new Mat(pGray.rows(), pGray.cols(), CV_32FC1); FloatBuffer in = Optical_Flow.getFloatBuffer(); FloatBuffer out = OF.getFloatBuffer(); int height = pGray.rows(); int width = pGray.cols(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float xVelocity = in.get(); float yVelocity = in.get(); float pixelVelocity = (float) Math.sqrt(xVelocity * xVelocity + yVelocity * yVelocity); out.put(pixelVelocity); } } imwrite("OF.png", OF); }
@Override @SuppressWarnings("unchecked") public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs, Optional<?> data) { final Mat input = ((InputSocket<Mat>) inputs[0]).getValue().get(); final Mat tmp = ((Optional<Mat>) data).get(); final boolean externalOnly = ((InputSocket<Boolean>) inputs[1]).getValue().get(); if (input.empty()) { return; } // findContours modifies its input, so we pass it a temporary copy of the input image input.copyTo(tmp); // OpenCV has a few different things it can return from findContours, but for now we only use // EXTERNAL and LIST. // The other ones involve hierarchies of contours, which might be useful in some situations, but // probably only // when processing the contours manually in code (so, not in a graphical pipeline). MatVector contours = new MatVector(); findContours( tmp, contours, externalOnly ? CV_RETR_EXTERNAL : CV_RETR_LIST, CV_CHAIN_APPROX_TC89_KCOS); final OutputSocket<ContoursReport> contoursSocket = (OutputSocket<ContoursReport>) outputs[0]; contoursSocket.setValue(new ContoursReport(contours, input.rows(), input.cols())); }