diff --git a/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Client.java b/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Client.java index 246e230a49ef3e9a966d389905a67a1d1790df60..1dbcddb902e090ccac1596fafb5ba4a0d6347d86 100644 --- a/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Client.java +++ b/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Client.java @@ -12,19 +12,19 @@ public class Client { private static final String TAG = "CLIENT"; private static Client _clientInstance; - private Socket serverSocket; + private Socket _serverSocket; private int _serverPort; private String _serverIp; - //private BufferedReader input; - private DataOutputStream output; + // private BufferedReader _input; + private DataOutputStream _output; - protected Client(){ + protected Client() { } - public static Client getInstance(){ - if(_clientInstance == null){ + public static Client getInstance() { + if (_clientInstance == null) { _clientInstance = new Client(); } return _clientInstance; @@ -37,46 +37,44 @@ public class Client { try { InetAddress serverAddr = InetAddress.getByName(this._serverIp); - serverSocket = new Socket(serverAddr, _serverPort); + _serverSocket = new Socket(serverAddr, _serverPort); Log.i(TAG, "Server on " + this._serverIp + ":" + _serverPort); - // get stream to send data - this.output = new DataOutputStream(this.serverSocket.getOutputStream()); - + if (_serverSocket != null) { + // get stream to send data + this._output = new DataOutputStream( + this._serverSocket.getOutputStream()); + } // get stream to receive data - //this.input = new BufferedReader(new InputStreamReader(this.serverSocket.getInputStream())); + // this._input = new BufferedReader(new + // InputStreamReader(this._serverSocket.getInputStream())); } catch (IOException e) { e.printStackTrace(); } } public void SendData(String msg) { - try { - Log.i(TAG, "sending="+ msg); - this.output.writeBytes(msg); - } catch (IOException e) { - e.printStackTrace(); + if (this._serverSocket != null && this._output != null) { + try { + Log.i(TAG, "sending=" + msg); + this._output.writeBytes(msg); + } catch (IOException e) { + e.printStackTrace(); + } } } /* - public String ReceiveData() { - try { - String read = input.readLine(); - Log.i(TAG, "received="+ read); - return read; - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - */ + * public String ReceiveData() { try { String read = _input.readLine(); + * Log.i(TAG, "received="+ read); return read; } catch (IOException e) { + * e.printStackTrace(); return null; } } + */ public void Stop() { try { - //input.close(); - output.close(); - serverSocket.close(); + // _input.close(); + _output.close(); + _serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } diff --git a/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/Adquisicion.java b/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/Adquisicion.java index f0cd93c45a6b1580f7c2af83409276c9b208c4f5..dbf8a067d6e06542873a8fc57df7427aabf14d27 100644 --- a/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/Adquisicion.java +++ b/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/Adquisicion.java @@ -8,6 +8,7 @@ import java.util.List; import org.opencv.core.Mat; import uy.edu.fing.chesstrack.modulovision.imgproc.BackgroundSupress; +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 android.util.Log; @@ -15,21 +16,18 @@ import android.util.Log; public class Adquisicion { - List<ImgProcInterface> listProc; - private static final String TAG = "CHESSTRACK::Adquisicion"; + List<ImgProcInterface> _listProc; public Adquisicion() throws Exception { super(); Log.i(TAG, "Cargado...."); - listProc = new ArrayList<ImgProcInterface>(); - - listProc.add( new Homografia()); - //listProc.add( new EcualizarImagen()); - //listProc.add( new SustraccionDeFondo()); - //listProc.add( new BackgroundSupress()); + _listProc = new ArrayList<ImgProcInterface>(); + _listProc.add(new Homografia()); + //_listProc.add(new EcualizarImagen()); + _listProc.add(new BackgroundSupress()); } /** @@ -41,12 +39,10 @@ public class Adquisicion { */ public synchronized Mat processFrame(Mat inputPicture) { Log.i(TAG, "processFrame inicia"); - Mat salida = inputPicture; - Mat prevImg = inputPicture; - for (ImgProcInterface proc: listProc){ - prevImg = proc.procesarImagen(prevImg); + Mat tempImg = inputPicture; + for (ImgProcInterface proc: _listProc){ + tempImg = proc.procesarImagen(tempImg); } - return prevImg; - + return tempImg; } } diff --git a/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/imgproc/Homografia.java b/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/imgproc/Homografia.java index ef3b500611826504da272158d6e07f5be471128e..2ce69129ad9074c56bec21ad583b3e7ade850716 100644 --- a/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/imgproc/Homografia.java +++ b/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/modulovision/imgproc/Homografia.java @@ -14,104 +14,98 @@ import android.util.Log; public class Homografia implements ImgProcInterface { private static final String TAG = "CHESSTRACK::Homografia"; - private final Mat matrizTransformada; - private final Rect rectPOI; - private int media; - private int offset; + + private final Mat _matrizTransformada; + private final Rect _rectPOI; + private int _media; + private int _offset; + public Homografia() throws Exception { super(); 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; Point[] ret = getBestPoints(vertices, Calibracion.getInstance().getImagenCalibrada().size()); - Log.i(TAG, "coso= " + ret); + + Log.i(TAG, "Best Points= " + ret); p1 = ret[0]; p2 = ret[1]; p3 = ret[2]; p4 = ret[3]; - 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- p3.x),2) + Math.pow((p2.y- p3.y),2)); - double d143 = Math.sqrt(Math.pow((p3.x- p4.x),2) + Math.pow((p3.y- p4.y),2)); - double d4349 = Math.sqrt(Math.pow((p4.x- p1.x),2) + Math.pow((p4.y- p1.y),2)); - - media = (int) Math.floor( Math.round((d17+d749+d143+d4349)/4)); - offset = 2*(media/6); - - 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+media,offset+media, offset,offset+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{ + + // se obtienen las distancias de cada una de las aristas que determinan los vertices + // internos mas externos y se determina el promedio de esa medida + double d12 = Math.sqrt(Math.pow((p1.x- p2.x),2) + Math.pow((p1.y- p2.y),2)); + double d23 = Math.sqrt(Math.pow((p2.x- p3.x),2) + Math.pow((p2.y- p3.y),2)); + double d34 = Math.sqrt(Math.pow((p3.x- p4.x),2) + Math.pow((p3.y- p4.y),2)); + double d14 = Math.sqrt(Math.pow((p4.x- p1.x),2) + Math.pow((p4.y- p1.y),2)); + + _media = (int) Math.floor( Math.round((d12+d23+d34+d14)/4)); + // El offset es el tamaño de dos celdas (para abarcar el primer casillero y dejar un borde al tablero + _offset = (_media/6); + + Log.i(TAG, "media= " + _media); + Log.i(TAG, "offset= " + _offset); + + 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+_media,_offset+_media, _offset,_offset+_media ); + + _rectPOI = new Rect(new Point(0,0), new Point(2*_offset+_media,2*_offset+_media)); + Calibracion.getInstance(); + Calibracion.setRectPOI(_rectPOI); + + Log.i(TAG, "rectPOI size= " + _rectPOI.size()); + + _matrizTransformada = Imgproc.getPerspectiveTransform(src,dst); + } else { throw new Exception(TAG + "No se han calculado los vertices"); } } + /** * Hace homografia y recorta la imagen */ @Override public Mat procesarImagen(Mat inputFrame) { + Log.i(TAG, "processing Frame - INI"); - Log.i(TAG, "before"); - Imgproc.warpPerspective(inputFrame,inputFrame, matrizTransformada, inputFrame.size()); - Mat subMat = inputFrame.submat(rectPOI); + // hace la homografia con la matriz calculada en un ppio + Imgproc.warpPerspective(inputFrame,inputFrame, _matrizTransformada, inputFrame.size()); + Mat subMat = inputFrame.submat(_rectPOI); Mat tmp = Mat.zeros(inputFrame.size(), CvType.CV_8UC4); - Mat matTMP = tmp.submat(rectPOI); + Mat matTMP = tmp.submat(_rectPOI); subMat.copyTo(matTMP); - Log.i(TAG, "after"); + + Log.i(TAG, "processing Frame - FIN"); return tmp; } private Point[] getBestPoints(MatOfPoint2f vertices, Size tamanio){ + double d, best_dist; Point[] ret = new Point[4]; + Point[] vect = new Point[4]; - double d, dx ,dy; - double betst_dist = Double.MAX_VALUE; - Point aux = new Point(0,0); - for(Point pt : vertices.toList()){ - - d = Math.sqrt(Math.pow((pt.x- aux.x),2) + Math.pow((pt.y- aux.y),2)); - if (d < betst_dist){ - betst_dist = d; - ret[0] = pt; - } - } + vect[0] = new Point(0,0); + vect[1] = new Point(tamanio.width,0); + vect[2] = new Point(tamanio.width,tamanio.height); + vect[3] = new Point(0,tamanio.height); - betst_dist = Double.MAX_VALUE; - aux = new Point(tamanio.width,0); - for(Point pt : vertices.toList()){ - d = Math.sqrt(Math.pow((pt.x- aux.x),2) + Math.pow((pt.y- aux.y),2)); - if (d < betst_dist){ - betst_dist = d; - ret[1] = pt; - } + for (int i = 0; i < 4; i++){ + Point aux = vect[i]; + best_dist = Double.MAX_VALUE; + for(Point pt : vertices.toList()){ + d = Math.sqrt(Math.pow((pt.x- aux.x),2) + Math.pow((pt.y- aux.y),2)); + if (d < best_dist){ + best_dist = d; + ret[i] = pt; + } + } } - betst_dist = Double.MAX_VALUE; - aux = new Point(tamanio.width,tamanio.height); - for(Point pt : vertices.toList()){ - d = Math.sqrt(Math.pow((pt.x- aux.x),2) + Math.pow((pt.y- aux.y),2)); - if (d < betst_dist){ - betst_dist = d; - ret[2] = pt; - } - } - - betst_dist = Double.MAX_VALUE; - aux = new Point(0,tamanio.height); - for(Point pt : vertices.toList()){ - d = Math.sqrt(Math.pow((pt.x- aux.x),2) + Math.pow((pt.y- aux.y),2)); - if (d < betst_dist){ - betst_dist = d; - ret[3] = pt; - } - } return ret; } - }