Exemple #1
0
  public static void main(String[] args) {
    try {

      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
      Mat source = Imgcodecs.imread("test_image.jpg", 0);

      Mat destination = new Mat(source.rows(), source.cols(), source.type());
      Imgproc.GaussianBlur(source, source, new Size(45, 45), 0);
      Imgproc.adaptiveThreshold(
          source, source, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 75, 10);
      Core.bitwise_not(source, source);

      // Line detection
      Mat img2 = null;
      Imgproc.cvtColor(source, img2, Imgproc.COLOR_GRAY2RGB);

      Mat img3 = null;
      Imgproc.cvtColor(source, img3, Imgproc.COLOR_GRAY2RGB);

      MatOfInt4 lines = new MatOfInt4();
      // Imgproc.HoughLines(img, lines, rho, theta, threshold);

      // Write to File
      Imgcodecs.imwrite("gaussian.jpg", source);
      System.out.println("Success!");
    } catch (Exception e) {
      System.out.println("Error has occurred: " + e.getMessage());
    }
  }
  private ArrayList<Double> applyThresholdOnImage(Mat srcImgMat, Mat outputImgMat) {
    double localThreshold;
    int startRow;
    int endRow;
    int startCol;
    int endCol;

    ArrayList<Double> localThresholds = new ArrayList<Double>();

    int numberOfTiles = mPreference.getNumberOfTiles();
    int tileWidth = (int) srcImgMat.size().height / numberOfTiles;
    int tileHeight = (int) srcImgMat.size().width / numberOfTiles;

    // Split image into tiles and apply threshold on each image tile separately.

    // process image tiles other than the last one.
    for (int tileRowCount = 0; tileRowCount < numberOfTiles; tileRowCount++) {
      startRow = tileRowCount * tileWidth;
      if (tileRowCount < numberOfTiles - 1) endRow = (tileRowCount + 1) * tileWidth;
      else endRow = (int) srcImgMat.size().height;

      for (int tileColCount = 0; tileColCount < numberOfTiles; tileColCount++) {
        startCol = tileColCount * tileHeight;
        if (tileColCount < numberOfTiles - 1) endCol = (tileColCount + 1) * tileHeight;
        else endCol = (int) srcImgMat.size().width;

        Mat tileThreshold = new Mat();
        Mat tileMat = srcImgMat.submat(startRow, endRow, startCol, endCol);
        // localThreshold = Imgproc.threshold(tileMat, tileThreshold, 0, 255, Imgproc.THRESH_BINARY
        // | Imgproc.THRESH_OTSU);
        // RNM: Adaptive threshold rules!
        localThreshold = 0x80;
        Imgproc.adaptiveThreshold(
            tileMat,
            tileThreshold,
            255,
            Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
            Imgproc.THRESH_BINARY,
            91,
            2);
        Mat copyMat = outputImgMat.submat(startRow, endRow, startCol, endCol);
        tileThreshold.copyTo(copyMat);
        tileThreshold.release();
        localThresholds.add(localThreshold);
      }
    }

    return localThresholds;
  }