Beispiel #1
0
  public static Mat warp(Mat inputMat, Mat startM, int factor) {

    int resultWidth = 400 * factor;
    int resultHeight = 240 * factor;

    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);

    Point ocvPOut1 = new Point(0, 0);
    Point ocvPOut2 = new Point(0, resultHeight);
    Point ocvPOut3 = new Point(resultWidth, resultHeight);
    Point ocvPOut4 = new Point(resultWidth, 0);
    List<Point> dest = new ArrayList<Point>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);
    Mat endM = Converters.vector_Point2f_to_Mat(dest);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

    Imgproc.warpPerspective(
        inputMat,
        outputMat,
        perspectiveTransform,
        new Size(resultWidth, resultHeight),
        Imgproc.INTER_AREA);
    Imgproc.GaussianBlur(outputMat, outputMat, new org.opencv.core.Size(5, 5), 5);
    Imgproc.resize(outputMat, outputMat, new Size(resultWidth / factor, resultHeight / factor));

    Imgproc.threshold(outputMat, outputMat, 127, 255, Imgproc.THRESH_TOZERO);
    return outputMat;
  }
  private static Bitmap imgtrafo(
      Bitmap image1,
      Bitmap image2,
      int p1_x,
      int p1_y,
      int p2_x,
      int p2_y,
      int p3_x,
      int p3_y,
      int p4_x,
      int p4_y) {
    // set output size same size as input
    int resultWidth = image1.getWidth();
    int resultHeight = image1.getHeight();

    Mat inputMat = new Mat(image1.getWidth(), image1.getHeight(), CvType.CV_32FC1);
    Utils.bitmapToMat(image1, inputMat);
    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_32FC1);

    Point ocvPIn1 = new Point(p1_x, p1_y);
    Point ocvPIn2 = new Point(p2_x, p2_y);
    Point ocvPIn3 = new Point(p3_x, p3_y);
    Point ocvPIn4 = new Point(p4_x, p4_y);
    List<Point> source = new ArrayList<Point>();
    source.add(ocvPIn1);
    source.add(ocvPIn2);
    source.add(ocvPIn3);
    source.add(ocvPIn4);
    Mat inputQuad = Converters.vector_Point2f_to_Mat(source);

    Point ocvPOut1 = new Point(256, 40); // manually set
    Point ocvPOut2 = new Point(522, 62);
    Point ocvPOut3 = new Point(455, 479);
    Point ocvPOut4 = new Point(134, 404);
    List<Point> dest = new ArrayList<Point>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);
    Mat outputQuad = Converters.vector_Point2f_to_Mat(dest);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(inputQuad, outputQuad);

    Imgproc.warpPerspective(
        inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight)); // ?

    Bitmap output = Bitmap.createBitmap(resultWidth, resultHeight, Bitmap.Config.RGB_565);
    Utils.matToBitmap(outputMat, output);
    return output;
  }