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

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

本文整理了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

  1. public static List<Element> detectChanges(Mat base, Mat mChanged) {
  2. int PIXEL_DIFF_THRESHOLD = 3;
  3. int IMAGE_DIFF_THRESHOLD = 5;
  4. Mat mBaseGray = Element.getNewMat();
  5. Mat mChangedGray = Element.getNewMat();
  6. Mat mDiffAbs = Element.getNewMat();
  7. Mat mDiffTresh = Element.getNewMat();
  8. Mat mChanges = Element.getNewMat();
  9. List<Element> rectangles = new ArrayList<>();
  10. Imgproc.cvtColor(base, mBaseGray, toGray);
  11. Imgproc.cvtColor(mChanged, mChangedGray, toGray);
  12. Core.absdiff(mBaseGray, mChangedGray, mDiffAbs);
  13. Imgproc.threshold(mDiffAbs, mDiffTresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
  14. if (Core.countNonZero(mDiffTresh) > IMAGE_DIFF_THRESHOLD) {
  15. Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
  16. Imgproc.dilate(mDiffAbs, mDiffAbs, Element.getNewMat());
  17. Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
  18. Imgproc.morphologyEx(mDiffAbs, mDiffAbs, Imgproc.MORPH_CLOSE, se);
  19. List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  20. Mat mHierarchy = Element.getNewMat();
  21. Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  22. rectangles = contoursToRectangle(contours);
  23. Core.subtract(mDiffAbs, mDiffAbs, mChanges);
  24. Imgproc.drawContours(mChanges, contours, -1, new Scalar(255));
  25. //logShow(mDiffAbs);
  26. }
  27. return rectangles;
  28. }

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

  1. /**
  2. * Edge thinning, based on the awesome algorithm suggested here:
  3. * http://homepages.inf.ed.ac.uk/rbf/HIPR2/thin.htm
  4. */
  5. private void thin(Mat image) {
  6. Mat tmp = new Mat();
  7. int prevnz = Core.countNonZero(image);
  8. Log.v(TAG, "Starting thinning. NZ=" + prevnz);
  9. while (true) {
  10. for (int i = 0; i < 4; ++i) {
  11. HitAndMiss(image, tmp, HNM1POS, HNM1NEG);
  12. Core.subtract(image, tmp, image);
  13. HitAndMiss(image, tmp, HNM2POS, HNM2NEG);
  14. Core.subtract(image, tmp, image);
  15. rotateCCW(HNM1POS);
  16. rotateCCW(HNM1NEG);
  17. rotateCCW(HNM2POS);
  18. rotateCCW(HNM2NEG);
  19. }
  20. int nz = Core.countNonZero(image);
  21. if (nz == prevnz)
  22. break;
  23. prevnz = nz;
  24. }
  25. Log.v(TAG, "Thinning done. NZ=" + prevnz);
  26. }

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

  1. public boolean hasChanges(Mat current) {
  2. int PIXEL_DIFF_THRESHOLD = 5;
  3. int IMAGE_DIFF_THRESHOLD = 5;
  4. Mat bg = new Mat();
  5. Mat cg = new Mat();
  6. Mat diff = new Mat();
  7. Mat tdiff = new Mat();
  8. Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY);
  9. Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY);
  10. Core.absdiff(bg, cg, diff);
  11. Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
  12. if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) {
  13. return false;
  14. }
  15. Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
  16. Imgproc.dilate(diff, diff, new Mat());
  17. Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5,5));
  18. Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se);
  19. List<MatOfPoint> points = new ArrayList<MatOfPoint>();
  20. Mat contours = new Mat();
  21. Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  22. int n = 0;
  23. for (Mat pm: points) {
  24. log(lvl, "(%d) %s", n++, pm);
  25. printMatI(pm);
  26. }
  27. log(lvl, "contours: %s", contours);
  28. printMatI(contours);
  29. return true;
  30. }

相关文章

Core类方法