private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { final double ASPECT_TOLERANCE = 0.05; double targetRatio = (double) w / h; if (sizes == null) return null; Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; // Try to find an size match aspect ratio and size for (Size size : sizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } // Cannot find the one match the aspect ratio, ignore the requirement if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Size size : sizes) { if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; }
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); }