コード例 #1
0
  private Mat rotateImage(Mat src, double angle) {
    Point center = new Point(src.cols() / 2, src.rows() / 2);
    double scale = 1.0;

    Mat rotateMat = Imgproc.getRotationMatrix2D(center, angle, scale);
    Mat dst = new Mat();
    Size frameSize = new Size(src.rows(), src.cols());

    // adjust dst center point
    double m[] = new double[6];
    rotateMat.get(0, 0, m);
    m[2] += (frameSize.width - src.cols()) / 2;
    m[5] += (frameSize.height - src.rows()) / 2;
    rotateMat.put(0, 0, m);

    Imgproc.warpAffine(
        src,
        dst,
        rotateMat,
        frameSize,
        Imgproc.INTER_LINEAR,
        Imgproc.BORDER_CONSTANT,
        Scalar.all(0));

    return dst;
  }
コード例 #2
0
  public void onCameraViewStarted(int width, int height) {
    mGray = new Mat();
    mRgba = new Mat();
    mIntermediateMat = new Mat();
    mSize0 = new Size();
    mHist = new Mat();
    mChannels = new MatOfInt[] {new MatOfInt(0), new MatOfInt(1), new MatOfInt(2)};
    mHistSizeNum = 25;
    mBuff = new float[mHistSizeNum];
    mHistSize = new MatOfInt(mHistSizeNum);
    mRanges = new MatOfFloat(0f, 256f);
    mMat0 = new Mat();
    mColorsRGB =
        new Scalar[] {
          new Scalar(200, 0, 0, 255), new Scalar(0, 200, 0, 255), new Scalar(0, 0, 200, 255)
        };
    mColorsHue =
        new Scalar[] {
          new Scalar(255, 0, 0, 255), new Scalar(255, 60, 0, 255), new Scalar(255, 120, 0, 255),
              new Scalar(255, 180, 0, 255), new Scalar(255, 240, 0, 255),
          new Scalar(215, 213, 0, 255), new Scalar(150, 255, 0, 255), new Scalar(85, 255, 0, 255),
              new Scalar(20, 255, 0, 255), new Scalar(0, 255, 30, 255),
          new Scalar(0, 255, 85, 255), new Scalar(0, 255, 150, 255), new Scalar(0, 255, 215, 255),
              new Scalar(0, 234, 255, 255), new Scalar(0, 170, 255, 255),
          new Scalar(0, 120, 255, 255), new Scalar(0, 60, 255, 255), new Scalar(0, 0, 255, 255),
              new Scalar(64, 0, 255, 255), new Scalar(120, 0, 255, 255),
          new Scalar(180, 0, 255, 255), new Scalar(255, 0, 255, 255), new Scalar(255, 0, 215, 255),
              new Scalar(255, 0, 85, 255), new Scalar(255, 0, 0, 255)
        };
    mWhilte = Scalar.all(255);
    mP1 = new Point();
    mP2 = new Point();

    // Fill sepia kernel
    mSepiaKernel = new Mat(4, 4, CvType.CV_32F);
    mSepiaKernel.put(0, 0, /* R */ 0.189f, 0.769f, 0.393f, 0f);
    mSepiaKernel.put(1, 0, /* G */ 0.168f, 0.686f, 0.349f, 0f);
    mSepiaKernel.put(2, 0, /* B */ 0.131f, 0.534f, 0.272f, 0f);
    mSepiaKernel.put(3, 0, /* A */ 0.000f, 0.000f, 0.000f, 1f);
  }
コード例 #3
0
ファイル: CaptureImage.java プロジェクト: bmalla6920/Chequers
  /**
   * Identifies the color in the frame
   *
   * @param in the Mat image in the region of interest
   * @return the color
   */
  public char identifyColor(Mat in) {
    // Mat blue = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);
    // Mat green = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);
    // Mat red = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);

    // split the channels of the image
    Mat blue = new Mat(); // default is CV_8UC3
    Mat green = new Mat();
    Mat red = new Mat();
    List<Mat> channels = new ArrayList<Mat>(3);
    Core.split(in, channels);
    blue = channels.get(0); // makes all 3 CV_8UC1
    green = channels.get(1);
    red = channels.get(2);
    // System.out.println(blue.toString());

    // add the intensities
    Mat intensity = new Mat(in.rows(), in.cols(), CvType.CV_32F);
    // Mat mask = new Mat();
    Core.add(blue, green, intensity); // , mask, CvType.CV_32F);
    Core.add(intensity, red, intensity); // , mask, CvType.CV_32F);

    // not sure if correct from here to ...

    Mat inten = new Mat();
    Core.divide(intensity, Scalar.all(3.0), inten);
    // System.out.println(intensity.toString());
    // Core.divide(3.0, intensity, inten);
    // if intensity = intensity / 3.0; means element-wise division
    // use intensity.muls(Mat m)
    // so make new Mat m of same size that has each element of 1/3

    /*
    * or
    * About per-element division you can use Core.divide()

    Core.divide(A,Scalar.all(d), B);

    It's equivalent to B=A/d
    */

    // find normalized values
    Mat bnorm = new Mat();
    Mat gnorm = new Mat();
    Mat rnorm = new Mat();
    // blue.convertTo(blue, CvType.CV_32F);
    // green.convertTo(green, CvType.CV_32F);
    // red.convertTo(red, CvType.CV_32F);

    Core.divide(blue, inten, bnorm);
    Core.divide(green, inten, gnorm);
    Core.divide(red, inten, rnorm);

    // find average norm values
    Scalar val = new Scalar(0);
    val = Core.mean(bnorm);
    String value[] = val.toString().split(",");
    String s = value[0].substring(1);
    double bavg = Double.parseDouble(s);
    val = Core.mean(gnorm);
    String value1[] = val.toString().split(",");
    String s1 = value1[0].substring(1);
    double gavg = Double.parseDouble(s1);
    val = Core.mean(rnorm);
    String value2[] = val.toString().split(",");
    String s2 = value2[0].substring(1);
    double ravg = Double.parseDouble(s2);

    // ... here

    // original values
    /*
    // define the reference color values
    //double RED[] = {0.4, 0.5, 1.8};
    //double GREEN[] = {1.0, 1.2, 1.0};
    double BLUE[] = {1.75, 1.0, 0.5};
    //double YELLOW[] = {0.82, 1.7, 1.7};
    double ORANGE[] = {0.2, 1.0, 2.0};
    double WHITE[] = {2.0, 1.7, 1.7};
    //double BLACK[] = {0.0, 0.3, 0.3};
    */

    // define the reference color values
    // double RED[] = {0.4, 0.5, 1.8};
    // double GREEN[] = {1.0, 1.2, 1.0};
    double BLUE[] = {1.75, 1.0, 0.5};
    // double YELLOW[] = {0.82, 1.7, 1.7};
    double ORANGE[] = {0.2, 1.0, 2.0};
    double WHITE[] = {2.0, 1.7, 1.7};
    // double BLACK[] = {0.0, 0.3, 0.3};

    // compute the square error relative to the reference color values
    // double minError = 3.0;
    double minError = 2.0;
    double errorSqr;
    char bestFit = 'x';

    // test++;
    // System.out.print("\n\n" + test + "\n\n");

    // check BLUE fitness
    errorSqr = normSqr(BLUE[0], BLUE[1], BLUE[2], bavg, gavg, ravg);
    System.out.println("Blue: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_BLUE;
    }
    // check ORANGE fitness
    errorSqr = normSqr(ORANGE[0], ORANGE[1], ORANGE[2], bavg, gavg, ravg);
    System.out.println("Orange: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_ORANGE;
    }
    // check WHITE fitness
    errorSqr = normSqr(WHITE[0], WHITE[1], WHITE[2], bavg, gavg, ravg);
    System.out.println("White: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_WHITE;
    }
    // check BLACK fitness
    /*errorSqr = normSqr(BLACK[0], BLACK[1], BLACK[2], bavg, gavg, ravg);
    System.out.println("Black: " + errorSqr);
    if(errorSqr < minError)
    {
    	minError = errorSqr;
    	bestFit = COLOR_BLACK;
    }*/

    // return the best fit color label
    return bestFit;
  }