Gruppe 1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OpenCVHelper.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.example.ueberwachungssystem.Detection;
  2. import android.graphics.Bitmap;
  3. import android.media.Image;
  4. import android.widget.ImageView;
  5. import androidx.annotation.NonNull;
  6. import androidx.camera.core.ExperimentalGetImage;
  7. import androidx.camera.core.ImageProxy;
  8. import org.opencv.android.Utils;
  9. import org.opencv.core.Core;
  10. import org.opencv.core.CvType;
  11. import org.opencv.core.Mat;
  12. import org.opencv.core.MatOfPoint;
  13. import org.opencv.core.Scalar;
  14. import org.opencv.core.Size;
  15. import org.opencv.imgproc.Imgproc;
  16. import java.nio.ByteBuffer;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;
  20. @ExperimentalGetImage
  21. public class OpenCVHelper {
  22. /** OpenCV helper methods **/
  23. public static Mat addGaussianBlur(Mat inputMat, Size kernelSize){
  24. Mat outputMat = new Mat();
  25. Imgproc.GaussianBlur(inputMat, outputMat, kernelSize, 0);
  26. return outputMat;
  27. }
  28. public static Mat addBlur(Mat inputMat, Size kernelSize){
  29. Mat outputMat = new Mat();
  30. Imgproc.blur(inputMat, outputMat, kernelSize);
  31. return outputMat;
  32. }
  33. public static Mat extractYChannel(@NonNull ImageProxy imgProxy) {
  34. Image img = imgProxy.getImage();
  35. assert img != null;
  36. ByteBuffer yBuffer = img.getPlanes()[0].getBuffer();
  37. byte[] yData = new byte[yBuffer.remaining()];
  38. yBuffer.get(yData);
  39. Mat yMat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
  40. yMat.put(0, 0, yData);
  41. return yMat;
  42. }
  43. public static Mat thresholdPixels(Mat inputMat, Mat previousImage, int threshold){
  44. Mat diffImage = new Mat();
  45. Core.absdiff(inputMat, previousImage, diffImage);
  46. Mat binaryMat = new Mat();
  47. Imgproc.threshold(diffImage, binaryMat, threshold, 255, Imgproc.THRESH_BINARY);
  48. return binaryMat;
  49. }
  50. public static Mat thresholdContourArea(Mat inputMat, float areaThreshold){
  51. List<MatOfPoint> contours = new ArrayList<>();
  52. Mat hierarchy = new Mat();
  53. Imgproc.findContours(inputMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  54. Mat outputMat = new Mat(inputMat.size(), inputMat.type(), new Scalar(0));
  55. // Iterate over the contours and draw only the larger contours on the outputMat
  56. for (MatOfPoint contour : contours) {
  57. double contourArea = Imgproc.contourArea(contour);
  58. if (contourArea > areaThreshold) {
  59. Imgproc.drawContours(outputMat, Collections.singletonList(contour), 0, new Scalar(255), -1);
  60. }
  61. }
  62. // Apply the outputMat as a mask to the dilatedImage
  63. Mat maskedImage = new Mat();
  64. inputMat.copyTo(maskedImage, outputMat);
  65. return outputMat;
  66. }
  67. public static Mat dilateBinaryMat(Mat inputMat, Size kernelSize){
  68. Mat dilatedMat = new Mat();
  69. Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, kernelSize);
  70. Imgproc.dilate(inputMat, dilatedMat, kernel);
  71. return dilatedMat;
  72. }
  73. public static int countNonZeroPixels(Mat inputImage) {
  74. if (inputImage != null)
  75. return Core.countNonZero(inputImage);
  76. else
  77. return 0;
  78. }
  79. public static void debugMat(Mat mat, ImageView imageView) {
  80. if (imageView == null || mat == null)
  81. return;
  82. Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
  83. Utils.matToBitmap(mat, bitmap);
  84. // Display the bitmap in an ImageView
  85. imageView.setImageBitmap(bitmap);
  86. }
  87. }