package com.example.ueberwachungssystem.Detection; import android.graphics.Bitmap; import android.media.Image; import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.camera.core.ExperimentalGetImage; import androidx.camera.core.ImageProxy; import org.opencv.android.Utils; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.imgproc.Imgproc; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.List; @ExperimentalGetImage public class OpenCVHelper { /** OpenCV helper methods **/ public static Mat addGaussianBlur(Mat inputMat, Size kernelSize){ Mat outputMat = new Mat(); Imgproc.GaussianBlur(inputMat, outputMat, kernelSize, 0); return outputMat; } public static Mat addBlur(Mat inputMat, Size kernelSize){ Mat outputMat = new Mat(); Imgproc.blur(inputMat, outputMat, kernelSize); return outputMat; } public static Mat extractYChannel(@NonNull ImageProxy imgProxy) { Image img = imgProxy.getImage(); assert img != null; ByteBuffer yBuffer = img.getPlanes()[0].getBuffer(); byte[] yData = new byte[yBuffer.remaining()]; yBuffer.get(yData); Mat yMat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1); yMat.put(0, 0, yData); return yMat; } public static Mat thresholdPixels(Mat inputMat, Mat previousImage, int threshold){ Mat diffImage = new Mat(); Core.absdiff(inputMat, previousImage, diffImage); Mat binaryMat = new Mat(); Imgproc.threshold(diffImage, binaryMat, threshold, 255, Imgproc.THRESH_BINARY); return binaryMat; } public static Mat thresholdContourArea(Mat inputMat, float areaThreshold){ List contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(inputMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); Mat outputMat = new Mat(inputMat.size(), inputMat.type(), new Scalar(0)); // Iterate over the contours and draw only the larger contours on the outputMat for (MatOfPoint contour : contours) { double contourArea = Imgproc.contourArea(contour); if (contourArea > areaThreshold) { Imgproc.drawContours(outputMat, Collections.singletonList(contour), 0, new Scalar(255), -1); } } // Apply the outputMat as a mask to the dilatedImage Mat maskedImage = new Mat(); inputMat.copyTo(maskedImage, outputMat); return outputMat; } public static Mat dilateBinaryMat(Mat inputMat, Size kernelSize){ Mat dilatedMat = new Mat(); Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, kernelSize); Imgproc.dilate(inputMat, dilatedMat, kernel); return dilatedMat; } public static int countNonZeroPixels(Mat inputImage) { if (inputImage != null) return Core.countNonZero(inputImage); else return 0; } public static void debugMat(Mat mat, ImageView imageView) { if (imageView == null || mat == null) return; Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(mat, bitmap); // Display the bitmap in an ImageView imageView.setImageBitmap(bitmap); } }