public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
   frame = inputFrame.rgba();
   outputFrame = frame.clone();
   if (homography != null) {
     if (markerValue != -1 && markerValue != oldMarkerValue) {
       oldMarkerValue = markerValue;
       outputLogo = logo.clone();
       Core.putText(
           outputLogo,
           Integer.toString(markerValue),
           new Point(5, 505),
           Core.FONT_HERSHEY_SIMPLEX,
           5,
           new Scalar(255, 0, 0),
           5);
     }
     Imgproc.warpPerspective(
         outputLogo,
         outputFrame,
         homography,
         new Size(WIDTH, HEIGHT),
         Imgproc.INTER_NEAREST,
         Imgproc.BORDER_TRANSPARENT,
         new Scalar(0));
   }
   return outputFrame;
 }
Esempio n. 2
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;
  }
Esempio n. 4
0
  public void processFrame(CameraEvents.Frame frameEvent) {
    if (service.getActivityMode() != ActivityEvent.Mode.WARP) return;
    if (mode == Mode.SAMPLEWARPPLANE || mode == Mode.SAMPLEWARPGLASS) {
      synchronized (this) {
        if (!isSetup) setupMatrices();
        if (captureSample) {
          captureSample = false;
          Log.d(TAG, "Warp: Capturing Sample");
          Mat frame = frameEvent.getCameraFrame().getRGB();
          byte[] frameJPEG = frameEvent.getCameraFrame().getJPEG();
          if (sampleBGR == null
              || sampleBGR.height() != frame.height()
              || sampleBGR.width() != frame.width())
            sampleBGR = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC3);
          Imgproc.cvtColor(frame, sampleBGR, Imgproc.COLOR_RGB2BGR);
          useSample = true;
          // TODO: Specialize it for this group/device
          com.dappervision.wearscript.Utils.eventBusPost(
              new SendEvent("warpsample", "", ValueFactory.createRawValue(frameJPEG)));
        }
      }
    }

    if (busy) return;
    synchronized (this) {
      busy = true;
      if (!isSetup) setupMatrices();
      if (mode == Mode.CAM2GLASS) {
        Mat inputBGR;
        Mat frame = frameEvent.getCameraFrame().getRGB();
        if (frameBGR == null
            || frameBGR.height() != frame.height()
            || frameBGR.width() != frame.width())
          frameBGR = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC3);
        Mat hSmallToGlassMat = getHSmallToGlassMat(frame.rows(), frame.cols());
        if (hSmallToGlassMat == null) {
          Log.w(TAG, "Warp: Bad size");
          busy = false;
          return;
        }
        Imgproc.cvtColor(frame, frameBGR, Imgproc.COLOR_RGB2BGR);
        inputBGR = frameBGR;
        Imgproc.warpPerspective(
            inputBGR, frameWarp, hSmallToGlassMat, new Size(frameWarp.width(), frameWarp.height()));
        drawFrame(frameWarp);
      }
      busy = false;
    }
  }
Esempio n. 5
0
 public void onEventAsync(WarpHEvent event) {
   double[] h = event.getH();
   synchronized (this) {
     if ((mode == Mode.SAMPLEWARPPLANE || mode == Mode.SAMPLEWARPGLASS) && useSample) {
       if (!isSetup) setupMatrices();
       double hSmallToGlass[] = getHSmallToGlass(sampleBGR.height(), sampleBGR.width());
       if (hSmallToGlass == null) {
         Log.w(TAG, "Warp: Bad size");
         return;
       }
       Log.d(TAG, "Warp: WarpHEvent");
       if (mode == Mode.SAMPLEWARPGLASS) {
         h = HMult(hSmallToGlass, h);
       }
       Mat hMat = HMatFromArray(h);
       Imgproc.warpPerspective(
           sampleBGR, frameWarp, hMat, new Size(frameWarp.width(), frameWarp.height()));
       drawFrame(frameWarp);
     }
   }
 }