コード例 #1
0
  public void templateMatching() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    int match_method = 5;
    int max_Trackbar = 5;
    Mat data = Highgui.imread("images/training_data/1" + "/data (" + 1 + ").jpg");
    Mat temp = Highgui.imread("images/template.jpg");
    Mat img = data.clone();

    int result_cols = img.cols() - temp.cols() + 1;
    int result_rows = img.rows() - temp.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

    Imgproc.matchTemplate(img, temp, result, match_method);
    Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    double minVal;
    double maxVal;
    Point minLoc;
    Point maxLoc;
    Point matchLoc;
    // minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
    Core.MinMaxLocResult res = Core.minMaxLoc(result);

    if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
      matchLoc = res.minLoc;
    } else {
      matchLoc = res.maxLoc;
    }

    // / Show me what you got
    Core.rectangle(
        img,
        matchLoc,
        new Point(matchLoc.x + temp.cols(), matchLoc.y + temp.rows()),
        new Scalar(0, 255, 0));

    // Save the visualized detection.
    Highgui.imwrite("images/samp.jpg", img);
  }
コード例 #2
0
  public Mat onCameraFrame(Mat inputFrame) {
    inputFrame.copyTo(mRgba);

    switch (ImageManipulationsActivity.viewMode) {
      case ImageManipulationsActivity.VIEW_MODE_RGBA:
        break;

      case ImageManipulationsActivity.VIEW_MODE_HIST:
        if ((mSizeRgba == null)
            || (mRgba.cols() != mSizeRgba.width)
            || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats();
        int thikness = (int) (mSizeRgba.width / (mHistSizeNum + 10) / 5);
        if (thikness > 5) thikness = 5;
        int offset = (int) ((mSizeRgba.width - (5 * mHistSizeNum + 4 * 10) * thikness) / 2);
        // RGB
        for (int c = 0; c < 3; c++) {
          Imgproc.calcHist(Arrays.asList(mRgba), mChannels[c], mMat0, mHist, mHistSize, mRanges);
          Core.normalize(mHist, mHist, mSizeRgba.height / 2, 0, Core.NORM_INF);
          mHist.get(0, 0, mBuff);
          for (int h = 0; h < mHistSizeNum; h++) {
            mP1.x = mP2.x = offset + (c * (mHistSizeNum + 10) + h) * thikness;
            mP1.y = mSizeRgba.height - 1;
            mP2.y = mP1.y - 2 - (int) mBuff[h];
            Core.line(mRgba, mP1, mP2, mColorsRGB[c], thikness);
          }
        }
        // Value and Hue
        Imgproc.cvtColor(mRgba, mIntermediateMat, Imgproc.COLOR_RGB2HSV_FULL);
        // Value
        Imgproc.calcHist(
            Arrays.asList(mIntermediateMat), mChannels[2], mMat0, mHist, mHistSize, mRanges);
        Core.normalize(mHist, mHist, mSizeRgba.height / 2, 0, Core.NORM_INF);
        mHist.get(0, 0, mBuff);
        for (int h = 0; h < mHistSizeNum; h++) {
          mP1.x = mP2.x = offset + (3 * (mHistSizeNum + 10) + h) * thikness;
          mP1.y = mSizeRgba.height - 1;
          mP2.y = mP1.y - 2 - (int) mBuff[h];
          Core.line(mRgba, mP1, mP2, mWhilte, thikness);
        }
        // Hue
        Imgproc.calcHist(
            Arrays.asList(mIntermediateMat), mChannels[0], mMat0, mHist, mHistSize, mRanges);
        Core.normalize(mHist, mHist, mSizeRgba.height / 2, 0, Core.NORM_INF);
        mHist.get(0, 0, mBuff);
        for (int h = 0; h < mHistSizeNum; h++) {
          mP1.x = mP2.x = offset + (4 * (mHistSizeNum + 10) + h) * thikness;
          mP1.y = mSizeRgba.height - 1;
          mP2.y = mP1.y - 2 - (int) mBuff[h];
          Core.line(mRgba, mP1, mP2, mColorsHue[h], thikness);
        }
        break;

      case ImageManipulationsActivity.VIEW_MODE_CANNY:
        if ((mRgbaInnerWindow == null)
            || (mGrayInnerWindow == null)
            || (mRgba.cols() != mSizeRgba.width)
            || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats();
        Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 80, 90);
        Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
        break;

      case ImageManipulationsActivity.VIEW_MODE_SOBEL:
        Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);

        if ((mRgbaInnerWindow == null)
            || (mGrayInnerWindow == null)
            || (mRgba.cols() != mSizeRgba.width)
            || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats();

        Imgproc.Sobel(mGrayInnerWindow, mIntermediateMat, CvType.CV_8U, 1, 1);
        Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0);
        Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
        break;

      case ImageManipulationsActivity.VIEW_MODE_SEPIA:
        Core.transform(mRgba, mRgba, mSepiaKernel);
        break;

      case ImageManipulationsActivity.VIEW_MODE_ZOOM:
        if ((mZoomCorner == null)
            || (mZoomWindow == null)
            || (mRgba.cols() != mSizeRgba.width)
            || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats();
        Imgproc.resize(mZoomWindow, mZoomCorner, mZoomCorner.size());

        Size wsize = mZoomWindow.size();
        Core.rectangle(
            mZoomWindow,
            new Point(1, 1),
            new Point(wsize.width - 2, wsize.height - 2),
            new Scalar(255, 0, 0, 255),
            2);
        break;

      case ImageManipulationsActivity.VIEW_MODE_PIXELIZE:
        if ((mRgbaInnerWindow == null)
            || (mRgba.cols() != mSizeRgba.width)
            || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats();
        Imgproc.resize(mRgbaInnerWindow, mIntermediateMat, mSize0, 0.1, 0.1, Imgproc.INTER_NEAREST);
        Imgproc.resize(
            mIntermediateMat, mRgbaInnerWindow, mSizeRgbaInner, 0., 0., Imgproc.INTER_NEAREST);
        break;

      case ImageManipulationsActivity.VIEW_MODE_POSTERIZE:
        if ((mRgbaInnerWindow == null)
            || (mRgba.cols() != mSizeRgba.width)
            || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats();
        /*
        Imgproc.cvtColor(mRgbaInnerWindow, mIntermediateMat, Imgproc.COLOR_RGBA2RGB);
        Imgproc.pyrMeanShiftFiltering(mIntermediateMat, mIntermediateMat, 5, 50);
        Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_RGB2RGBA);
        */

        Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 80, 90);
        mRgbaInnerWindow.setTo(new Scalar(0, 0, 0, 255), mIntermediateMat);
        Core.convertScaleAbs(mRgbaInnerWindow, mIntermediateMat, 1. / 16, 0);
        Core.convertScaleAbs(mIntermediateMat, mRgbaInnerWindow, 16, 0);
        break;
    }

    return mRgba;
  }