org.opencv.core.Core.countNonZero()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(3.3k)|赞(0)|评价(0)|浏览(371)

本文整理了Java中org.opencv.core.Core.countNonZero()方法的一些代码示例,展示了Core.countNonZero()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Core.countNonZero()方法的具体详情如下:
包路径:org.opencv.core.Core
类名称:Core
方法名:countNonZero

Core.countNonZero介绍

[英]Counts non-zero array elements.

The function returns the number of non-zero elements in src :

sum(by: I: src(I) != 0) 1
[中]计数非零数组元素。
函数返回src中非零元素的数目:
总数(由:I:src(I)!=0) 1

代码示例

代码示例来源:origin: RaiMan/SikuliX2

public static List<Element> detectChanges(Mat base, Mat mChanged) {
 int PIXEL_DIFF_THRESHOLD = 3;
 int IMAGE_DIFF_THRESHOLD = 5;
 Mat mBaseGray = Element.getNewMat();
 Mat mChangedGray = Element.getNewMat();
 Mat mDiffAbs = Element.getNewMat();
 Mat mDiffTresh = Element.getNewMat();
 Mat mChanges = Element.getNewMat();
 List<Element> rectangles = new ArrayList<>();
 Imgproc.cvtColor(base, mBaseGray, toGray);
 Imgproc.cvtColor(mChanged, mChangedGray, toGray);
 Core.absdiff(mBaseGray, mChangedGray, mDiffAbs);
 Imgproc.threshold(mDiffAbs, mDiffTresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
 if (Core.countNonZero(mDiffTresh) > IMAGE_DIFF_THRESHOLD) {
  Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
  Imgproc.dilate(mDiffAbs, mDiffAbs, Element.getNewMat());
  Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
  Imgproc.morphologyEx(mDiffAbs, mDiffAbs, Imgproc.MORPH_CLOSE, se);
  List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  Mat mHierarchy = Element.getNewMat();
  Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  rectangles = contoursToRectangle(contours);
  Core.subtract(mDiffAbs, mDiffAbs, mChanges);
  Imgproc.drawContours(mChanges, contours, -1, new Scalar(255));
  //logShow(mDiffAbs);
 }
 return rectangles;
}

代码示例来源:origin: ytai/IOIOPlotter

/**
 * Edge thinning, based on the awesome algorithm suggested here:
 * http://homepages.inf.ed.ac.uk/rbf/HIPR2/thin.htm
 */
private void thin(Mat image) {
  Mat tmp = new Mat();
  int prevnz = Core.countNonZero(image);
  Log.v(TAG, "Starting thinning. NZ=" + prevnz);
  while (true) {
    for (int i = 0; i < 4; ++i) {
      HitAndMiss(image, tmp, HNM1POS, HNM1NEG);
      Core.subtract(image, tmp, image);
      HitAndMiss(image, tmp, HNM2POS, HNM2NEG);
      Core.subtract(image, tmp, image);
      rotateCCW(HNM1POS);
      rotateCCW(HNM1NEG);
      rotateCCW(HNM2POS);
      rotateCCW(HNM2NEG);
    }
    int nz = Core.countNonZero(image);
    if (nz == prevnz)
      break;
    prevnz = nz;
  }
  Log.v(TAG, "Thinning done. NZ=" + prevnz);
}

代码示例来源:origin: com.sikulix/sikulixapi

public boolean hasChanges(Mat current) {
 int PIXEL_DIFF_THRESHOLD = 5;
 int IMAGE_DIFF_THRESHOLD = 5;
 Mat bg = new Mat();
 Mat cg = new Mat();
 Mat diff = new Mat();
 Mat tdiff = new Mat();
 Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY);
 Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY);
 Core.absdiff(bg, cg, diff);
 Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
 if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) {
  return false;
 }
 Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
 Imgproc.dilate(diff, diff, new Mat());
 Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5,5));
 Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se);
 List<MatOfPoint> points = new ArrayList<MatOfPoint>();
 Mat contours = new Mat();
 Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
 int n = 0;
 for (Mat pm: points) {
  log(lvl, "(%d) %s", n++, pm);
  printMatI(pm);
 }
 log(lvl, "contours: %s", contours);
 printMatI(contours);
 return true;
}

相关文章

Core类方法