diff --git a/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/DetectorOclusion.java b/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/DetectorOclusion.java new file mode 100644 index 0000000000000000000000000000000000000000..4467dc5b0d53965ed325815e5aaa639883d5e604 --- /dev/null +++ b/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/DetectorOclusion.java @@ -0,0 +1,124 @@ +package uy.edu.fing.chesstrack.modulovision; + +import org.opencv.core.Core; +import org.opencv.core.CvType; +import org.opencv.core.Mat; +import org.opencv.core.Point; +import org.opencv.core.Scalar; +import org.opencv.core.Size; +import org.opencv.imgproc.*; +import org.opencv.video.BackgroundSubtractorMOG2; + +import android.util.Log; + + +public class DetectorOclusion { + + private static final String TAG = "CHESSTRACK::BackgroundSupress"; + private static final int OCLUSION = 1; + private static final int ESTABLE = 2; + private static final int TRANSICION = 0; + private static final int START = -1; + + private Mat _fgMaskMOG2; + private BackgroundSubtractorMOG2 _pMOG2; + private final Mat _morphKernel; + private int _estadoANTERIOR; + private int _estadoACTUAL; + //private final List<MatOfPoint> contours; + + public DetectorOclusion() { + super(); + Log.i(TAG, "constructor INI"); + + _morphKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3)); + _fgMaskMOG2 = new Mat(4,1,CvType.CV_8UC1); + _pMOG2 = new BackgroundSubtractorMOG2(); + _estadoACTUAL = START; + //contours = new ArrayList<MatOfPoint>(); + + Log.i(TAG, "constructor FIN"); + } + + public Mat procesarImagen(Mat inputFrame) { + Log.i(TAG, "Procesar!"); + + Log.i(TAG, "Region SIZE=" + inputFrame.size()); + + _pMOG2.apply(inputFrame, _fgMaskMOG2); + + Imgproc.erode(_fgMaskMOG2, _fgMaskMOG2, _morphKernel); + Log.i(TAG, "Apply erode"); + + Imgproc.threshold(_fgMaskMOG2, _fgMaskMOG2, 200, 255, Imgproc.THRESH_BINARY); + Log.i(TAG, "Apply threshold"); + + //Imgproc.dilate(_fgMaskMOG2, _fgMaskMOG2, _morphKernel); + //Log.i(TAG, "Apply dilate"); + + Scalar suma = Core.sumElems(_fgMaskMOG2); + Log.i(TAG, "SUMA = " + suma); + + _estadoANTERIOR = _estadoACTUAL; + if (suma.val[0] > 1250000){ + Log.i(TAG, "MANO !!!"); + + _estadoACTUAL = OCLUSION; + Core.putText(_fgMaskMOG2, "MANO !!!", new Point(20, 20),Core.FONT_HERSHEY_SIMPLEX, 0.8 , new Scalar(255,255,0)); + + } else { + Log.i(TAG, "ESTABLE !!!"); + if (_estadoANTERIOR == OCLUSION){ + Core.putText(_fgMaskMOG2, "TRANSI !!!", new Point(20, 20),Core.FONT_HERSHEY_SIMPLEX, 0.8 , new Scalar(255,255,0)); + _estadoACTUAL = TRANSICION; + } + if (_estadoANTERIOR == TRANSICION){ + Core.putText(_fgMaskMOG2, "ESTABLE !!!", new Point(20, 20),Core.FONT_HERSHEY_SIMPLEX, 0.8 , new Scalar(255,255,0)); + _estadoACTUAL = ESTABLE; + _pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach + } + } + + /*double maxArea = 0; + MatOfPoint largestContour = null; + Imgproc.findContours(_fgMaskMOG2, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_TC89_L1); + for (MatOfPoint contour : contours) { + double area = Imgproc.contourArea(contour); + if (area > maxArea) { + maxArea = area; + largestContour = contour; + } + } + Log.i(TAG, "ESTABLE !!!" + maxArea); + if ((largestContour != null) && (maxArea > 10000)){ + Log.i(TAG, "MANO !!!" + maxArea); + Rect boundingRect = Imgproc.boundingRect(largestContour); + ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50); + toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); // 200 is duration in ms + }*/ + + /*Imgproc.findContours(_fgMaskMOG2, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE); + + for(int idx = 0; idx < contours.size(); idx++) + { + double area = Imgproc.contourArea(contours.get(idx)); + Log.i(TAG, "CONTOUR = AREA: " + area); + if ((area > 15000) && (area < 70000)){ + Log.i(TAG, "DRAW !!! : " + area); + Scalar color = new Scalar(255,127,127); + Rect r = Imgproc.boundingRect(contours.get(idx)); + Log.i(TAG, "3 at backgroundS "+(_fgMaskMOG2.type() == CvType.CV_8UC1)); + Core.rectangle(_fgMaskMOG2, r.tl(), r.br(), color, 2, 8, 0); + //Imgproc.drawContours(_fgMaskMOG2, contours, idx, color); + } + } + + contours.clear();*/ + + Log.i(TAG, "END"); + Mat region = new Mat(); + Imgproc.cvtColor(_fgMaskMOG2, region, Imgproc.COLOR_GRAY2RGBA,4); + return region; + } +} +