Skip to content
Snippets Groups Projects
Commit 0488c62f authored by Nicolas Furquez's avatar Nicolas Furquez
Browse files

Se modifica la clase Calibracion para que sea Singleton

se agrega una clase para sustrasccion de fondo
parent aab5454f
No related branches found
No related tags found
No related merge requests found
......@@ -70,11 +70,16 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.chess_track_layout);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
try {
isUsed = false;
isCalibrada =false;
addListenerOnButton();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
calibrar = new Calibracion();
isUsed = false;
isCalibrada =false;
addListenerOnButton();
}
public void addListenerOnButton() {
......@@ -87,13 +92,16 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
public void onClick(View arg0) {
isUsed = true;
if(frame!=null){
if (calibrar.calibrar(frame)){
if (Calibracion.getInstance().calibrar(frame)){
isCalibrada = true;
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); // 200 is duration in ms
adq = new Adquisicion(calibrar);
try {
adq = new Adquisicion();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 700); // 200 is duration in ms
......
......@@ -7,9 +7,9 @@ import java.util.List;
import org.opencv.core.Mat;
import uy.edu.fing.chesstrack.modulovision.imgproc.EcualizarImagen;
import uy.edu.fing.chesstrack.modulovision.imgproc.Homografia;
import uy.edu.fing.chesstrack.modulovision.imgproc.ImgProcInterface;
import uy.edu.fing.chesstrack.modulovision.imgproc.SustraccionDeFondo;
import android.util.Log;
......@@ -20,13 +20,14 @@ public class Adquisicion {
private static final String TAG = "CHESSTRACK::Adquisicion";
public Adquisicion(Calibracion cal) {
public Adquisicion() throws Exception {
super();
Log.i(TAG, "Cargado....");
listProc = new ArrayList<ImgProcInterface>();
listProc.add( new Homografia(cal.getVertices()));
listProc.add( new EcualizarImagen());
listProc.add( new Homografia());
//listProc.add( new EcualizarImagen());
listProc.add( new SustraccionDeFondo());
}
/**
......
......@@ -3,6 +3,7 @@ package uy.edu.fing.chesstrack.modulovision;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import android.util.Log;
......@@ -13,7 +14,20 @@ public class Calibracion {
private Mat imagenOriginal;
private MatOfPoint2f vertices;
private static final String TAG = "CHESSTRACK::Calibracion";
private static Rect rectPOI = null;
private static Calibracion instance;
protected Calibracion(){
}
public static Calibracion getInstance(){
if(instance == null){
instance = new Calibracion();
}
return instance;
}
public boolean calibrar(Mat img){
Log.i(TAG, "INI calibrando");
......@@ -28,12 +42,8 @@ public class Calibracion {
Log.i(TAG, "Encontro = " + corners.size().toString());
Calib3d.drawChessboardCorners(imagenCalibrada, patternSize, corners, ret);
vertices = corners;
}
OK = ret;
Log.i(TAG, "FIN calibrando = " + ret);
return OK;
}
......@@ -58,6 +68,14 @@ public class Calibracion {
this.vertices = vertices;
}
public static Rect getRectPOI() {
return rectPOI;
}
public static void setRectPOI(Rect rectPOI) {
Calibracion.rectPOI = rectPOI;
}
......
......@@ -7,6 +7,7 @@ import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.imgproc.Imgproc;
import uy.edu.fing.chesstrack.modulovision.Calibracion;
import android.util.Log;
public class Homografia implements ImgProcInterface {
......@@ -15,34 +16,36 @@ public class Homografia implements ImgProcInterface {
private final Mat matrizTransformada;
private final Rect rectPOI;
public Homografia(MatOfPoint2f vertices) {
public Homografia() throws Exception {
super();
//int offset = 48*2;
//int media = 48*6;
Mat src = new Mat(4,1,CvType.CV_32FC2);
Mat dst = new Mat(4,1,CvType.CV_32FC2);
Point p1,p2,p3,p4;
p1 = vertices.toList().get(0);
p2 = vertices.toList().get(6);
p3 = vertices.toList().get(42);
p4 = vertices.toList().get(48);
double d17 = Math.sqrt(Math.pow((p1.x- p2.x),2) + Math.pow((p1.y- p2.y),2));
double d749 = Math.sqrt(Math.pow((p2.x- p4.x),2) + Math.pow((p2.y- p4.y),2));
double d143 = Math.sqrt(Math.pow((p1.x- p3.x),2) + Math.pow((p1.y- p3.y),2));
double d4349 = Math.sqrt(Math.pow((p3.x- p4.x),2) + Math.pow((p3.y- p4.y),2));
int media = (int) Math.floor( Math.round((d17+d749+d143+d4349)/4));
int offset = 2*(media/7);
src.put(0,0 ,(int)p1.x,(int)p1.y, (int)p2.x,(int)p2.y, (int)p3.x,(int)p3.y, (int)p4.x,(int)p4.y);
dst.put(0,0, offset,offset ,offset+media,offset , offset,offset+media , offset+media,offset+media );
Log.i(TAG, "offset= " + offset);
Log.i(TAG, "media = " + media);
// EL offset es casi el tamaño de una celda
rectPOI = new Rect(new Point(0,0), new Point(2*offset+media,2*offset+media));
matrizTransformada = Imgproc.getPerspectiveTransform(src,dst);
MatOfPoint2f vertices = Calibracion.getInstance().getVertices();
if (vertices != null){
Mat src = new Mat(4,1,CvType.CV_32FC2);
Mat dst = new Mat(4,1,CvType.CV_32FC2);
Point p1,p2,p3,p4;
p1 = vertices.toList().get(0);
p2 = vertices.toList().get(6);
p3 = vertices.toList().get(42);
p4 = vertices.toList().get(48);
double d17 = Math.sqrt(Math.pow((p1.x- p2.x),2) + Math.pow((p1.y- p2.y),2));
double d749 = Math.sqrt(Math.pow((p2.x- p4.x),2) + Math.pow((p2.y- p4.y),2));
double d143 = Math.sqrt(Math.pow((p1.x- p3.x),2) + Math.pow((p1.y- p3.y),2));
double d4349 = Math.sqrt(Math.pow((p3.x- p4.x),2) + Math.pow((p3.y- p4.y),2));
int media = (int) Math.floor( Math.round((d17+d749+d143+d4349)/4));
int offset = 2*(media/7);
src.put(0,0 ,(int)p1.x,(int)p1.y, (int)p2.x,(int)p2.y, (int)p3.x,(int)p3.y, (int)p4.x,(int)p4.y);
dst.put(0,0, offset,offset ,offset+media,offset , offset,offset+media , offset+media,offset+media );
Log.i(TAG, "offset= " + offset);
Log.i(TAG, "media = " + media);
// EL offset es casi el tamaño de una celda
rectPOI = new Rect(new Point(0,0), new Point(2*offset+media,2*offset+media));
Calibracion.getInstance().setRectPOI(rectPOI);
matrizTransformada = Imgproc.getPerspectiveTransform(src,dst);
}else{
throw new Exception(TAG + "No se han calculado los vertices");
}
}
/**
* Hace homografia y recorta la imagen
......
package uy.edu.fing.chesstrack.modulovision.imgproc;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.BackgroundSubtractorMOG2;
public class SustraccionDeFondo implements ImgProcInterface {
private Mat fMaskMOG = null;
private final BackgroundSubtractorMOG2 pMOG;
private static final String TAG = "CHESSTRACK::SustraccionDeFondo";
public SustraccionDeFondo(){
pMOG = new BackgroundSubtractorMOG2();
//fMaskMOG = new Mat();
}
@Override
public Mat procesarImagen(Mat inputFrame) {
Imgproc.cvtColor(inputFrame, inputFrame, Imgproc.COLOR_RGBA2RGB);
fMaskMOG = new Mat(inputFrame.size(),inputFrame.channels());
pMOG.apply(inputFrame, fMaskMOG);
//Mat frame = inputFrame.submat(Calibracion.getInstance().getRectPOI());
//fMaskMOG = new Mat(frame.size(),frame.channels());
//pMOG.apply(frame, fMaskMOG);
//Imgproc.resize(inputFrame, frame, frame.size());
//Log.i(TAG, "Size frame= " + frame.size());
//Log.i(TAG, "Size fMaskMOG= " + fMaskMOG.size());
//Rect aux = Calibracion.getInstance().getRectPOI();
//frame.copyTo(frame,fMaskMOG);
//Mat subMat = inputFrame.submat(0,frame.rows(),0,frame.cols());
//frame.copyTo(subMat);
//Mat frame = new Mat();
//inputFrame.copyTo(frame,fMaskMOG);
return fMaskMOG;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment