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

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

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

Core.absdiff介绍

[英]Calculates the per-element absolute difference between two arrays or between an array and a scalar.

The function absdiff calculates:

  • Absolute difference between two arrays when they have the same size and type:

dst(I) = saturate(| src1(I) - src2(I)|)

  • Absolute difference between an array and a scalar when the second array is constructed from Scalar or has as many elements as the number of channels in src1:

dst(I) = saturate(| src1(I) - src2|)

  • Absolute difference between a scalar and an array when the first array is constructed from Scalar or has as many elements as the number of channels in src2:

dst(I) = saturate(| src1 - src2(I)|)

where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each channel is processed independently.

Note: Saturation is not applied when the arrays have the depth CV_32S. You may even get a negative value in the case of overflow.
[中]计算两个数组之间或数组与标量之间的每元素绝对差。
函数absdiff计算:
*大小和类型相同的两个数组之间的绝对差异:
dst(I)=饱和(| src1(I)-src2(I)|)
*当第二个数组由Scalar构造或元素数与src1中的通道数相同时,数组与标量之间的绝对差值:
dst(I)=饱和(| src1(I)-src2 |)
*当第一个数组由Scalar构造或元素数与src2中的通道数相同时,标量与数组之间的绝对差值:
dst(I)=饱和(| src1-src2(I)|)
其中I是数组元素的多维索引。对于多通道阵列,每个通道都是独立处理的。
注意:当阵列的深度为CV_32S时,不应用饱和度。在溢出的情况下,您甚至可能会得到负值。

代码示例

代码示例来源: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: JavaOpenCVBook/code

  1. public Mat process(Mat inputImage) {
  2. Mat foregroundImage = new Mat();
  3. Core.absdiff(backgroundImage,inputImage , foregroundImage);
  4. return foregroundImage;
  5. }

代码示例来源:origin: openpnp/openpnp

  1. /**
  2. * Calculate the absolute difference between the previously stored Mat called source1 and the
  3. * current Mat.
  4. *
  5. * @param source1
  6. * @param tag
  7. */
  8. public FluentCv absDiff(String source1, String... tag) {
  9. Core.absdiff(get(source1), mat, mat);
  10. return store(mat, tag);
  11. }

代码示例来源:origin: openpnp/openpnp

  1. public static double calculatePsnr(Mat I1, Mat I2) {
  2. Mat s1 = new Mat();
  3. Core.absdiff(I1, I2, s1); // |I1 - I2|
  4. s1.convertTo(s1, CvType.CV_32F); // cannot make a square on 8 bits
  5. s1 = s1.mul(s1); // |I1 - I2|^2
  6. Scalar s = Core.sumElems(s1); // sum elements per channel
  7. double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels
  8. if (sse <= 1e-10) {
  9. // for small values return zero
  10. return 0;
  11. }
  12. else {
  13. double mse = sse / (double) (I1.channels() * I1.total());
  14. double psnr = 10.0 * Math.log10((255 * 255) / mse);
  15. return psnr;
  16. }
  17. }

代码示例来源:origin: openpnp/openpnp

  1. Core.absdiff(lastSettleMat, mat, diff);
  2. MinMaxLocResult result = Core.minMaxLoc(diff);
  3. Logger.debug("autoSettleAndCapture auto settle score: " + result.maxVal);

代码示例来源:origin: JavaOpenCVBook/code

  1. public Mat process(Mat inputImage) {
  2. Mat foregroundThresh = new Mat();
  3. // Firstly, convert to gray-level image, yields good results with performance
  4. Imgproc.cvtColor(inputImage, inputGray, Imgproc.COLOR_BGR2GRAY);
  5. // initialize background to 1st frame, convert to floating type
  6. if (accumulatedBackground.empty())
  7. inputGray.convertTo(accumulatedBackground, CvType.CV_32F);
  8. // convert background to 8U, for differencing with input image
  9. accumulatedBackground.convertTo(backImage,CvType.CV_8U);
  10. // compute difference between image and background
  11. Core.absdiff(backImage,inputGray,foreground);
  12. // apply threshold to foreground image
  13. Imgproc.threshold(foreground,foregroundThresh, threshold,255, Imgproc.THRESH_BINARY_INV);
  14. // accumulate background
  15. Mat inputFloating = new Mat();
  16. inputGray.convertTo(inputFloating, CvType.CV_32F);
  17. Imgproc.accumulateWeighted(inputFloating, accumulatedBackground,learningRate, foregroundThresh);
  18. return negative(foregroundThresh);
  19. }

代码示例来源: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类方法