Skip to content
Snippets Groups Projects
Commit 9984770b authored by Aylen Ricca's avatar Aylen Ricca
Browse files

merge....

parents d1f1fd11 506037a5
No related branches found
No related tags found
No related merge requests found
Showing with 288 additions and 56 deletions
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
*~ *~
# Directories # # Directories #
#ProyectoAndroid/ChessTrack/bin ProyectoAndroid/ChessTrack/bin/*
ProyectoAndroid/ChessTrack/gen/*
#java specific #java specific
*.class *.class
......
...@@ -37,9 +37,9 @@ ...@@ -37,9 +37,9 @@
android:minSdkVersion="9" android:minSdkVersion="9"
android:targetSdkVersion="10" /> android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/> <uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
......
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:opencv="http://schemas.android.com/apk/res-auto" xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" > android:layout_height="wrap_content"
>
<Button
android:id="@+id/btn_calibrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="calibrar" />
<org.opencv.android.JavaCameraView <org.opencv.android.JavaCameraView
android:layout_width="fill_parent" android:layout_width="fill_parent"
......
...@@ -7,8 +7,8 @@ import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; ...@@ -7,8 +7,8 @@ import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader; import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import uy.edu.fing.chesstrack.communication.Client;
import uy.edu.chesstrack.communication.Client; import uy.edu.fing.chesstrack.modulomodelador.Modelador;
import uy.edu.fing.chesstrack.modulovision.Adquisicion; import uy.edu.fing.chesstrack.modulovision.Adquisicion;
import uy.edu.fing.chesstrack.modulovision.Calibracion; import uy.edu.fing.chesstrack.modulovision.Calibracion;
import android.app.Activity; import android.app.Activity;
...@@ -19,9 +19,9 @@ import android.media.ToneGenerator; ...@@ -19,9 +19,9 @@ import android.media.ToneGenerator;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.util.Log; import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
...@@ -44,7 +44,7 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener ...@@ -44,7 +44,7 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
private Calibracion calibrar; private Calibracion calibrar;
private Button btnCalibrar; private Button btnCalibrar;
private Client ClientCommunication; private Client ClientCommunication;
private MenuItem mItemCalibrar;
//TODO es chancho pero ver luego //TODO es chancho pero ver luego
private Mat frame; private Mat frame;
private boolean isUsed; private boolean isUsed;
...@@ -84,10 +84,11 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener ...@@ -84,10 +84,11 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.chess_track_layout); mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.chess_track_layout);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this); mOpenCvCameraView.setCvCameraViewListener(this);
Modelador.getInstance();
try { try {
isUsed = false; isUsed = false;
isCalibrada =false; isCalibrada =false;
addListenerOnButton();
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
...@@ -158,35 +159,39 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener ...@@ -158,35 +159,39 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
alert.show(); alert.show();
} }
public void addListenerOnButton() { @Override
public boolean onCreateOptionsMenu(Menu menu) {
btnCalibrar = (Button) findViewById(R.id.btn_calibrar); Log.i(TAG, "called onCreateOptionsMenu");
mItemCalibrar = menu.add("Show/hide tile numbers");
btnCalibrar.setOnClickListener(new OnClickListener() {
return true;
@Override }
public void onClick(View arg0) {
isUsed = true; @Override
if(frame!=null){ public boolean onOptionsItemSelected(MenuItem item) {
if (Calibracion.getInstance().calibrar(frame)){ Log.i(TAG, "Menu Item selected " + item);
isCalibrada = true; if (item == mItemCalibrar) {
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50); isUsed = true;
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); // 200 is duration in ms if(frame!=null){
try { if (Calibracion.getInstance().calibrar(frame)){
adq = new Adquisicion();
} catch (Exception e) { ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
// TODO Auto-generated catch block toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); // 200 is duration in ms
e.printStackTrace(); try {
} adq = new Adquisicion();
}else{ } catch (Exception e) {
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50); // TODO Auto-generated catch block
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 700); // 200 is duration in ms e.printStackTrace();
} }
isCalibrada = true;
}else{
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 700); // 200 is duration in ms
} }
isUsed = false;
} }
isUsed = false;
}); }
return true;
} }
...@@ -230,7 +235,11 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener ...@@ -230,7 +235,11 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
if(isCalibrada){ if(isCalibrada){
Log.i(TAG, "calibrada"); Log.i(TAG, "calibrada");
return adq.processFrame(inputFrame);
inputFrame = adq.processFrame(inputFrame);
//Modelador.getInstance().dividirTablero(inputFrame);
//return Modelador.getInstance().dibujarEscaque(2, 3);
return inputFrame;
} }
return inputFrame; return inputFrame;
......
package uy.edu.fing.chesstrack.communication;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import android.util.Log;
public class Client {
private static final String TAG = "CLIENT";
private static Client _clientInstance;
private Socket serverSocket;
private int _serverPort;
private String _serverIp;
//private BufferedReader input;
private DataOutputStream output;
protected Client(){
}
public static Client getInstance(){
if(_clientInstance == null){
_clientInstance = new Client();
}
return _clientInstance;
}
public void EstablishConnection(String serverIp, int serverPort) {
Log.i(TAG, "init client-server communication");
this._serverIp = serverIp;
this._serverPort = serverPort;
try {
Log.i(TAG, "Server on " + this._serverIp + ":" + _serverPort);
InetAddress serverAddr = InetAddress.getByName(this._serverIp);
serverSocket = new Socket(serverAddr, _serverPort);
// 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()));
} 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();
}
}
/*
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();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package uy.edu.fing.chesstrack.modulomodelador;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import uy.edu.fing.chesstrack.modulovision.Calibracion;
import android.util.Log;
public class Modelador {
private static final String TAG = "CHESSTRACK::Modelador";
private final int CANT_FILAS= 8;
private final int CANT_SCAQUE = CANT_FILAS* CANT_FILAS;
private final int[][] tablero;
// se cuentan de arrib a abajo y de izquierda a derecha
private final Mat[][] escaques;
private static Modelador instance;
private Modelador(){
tablero = new int[CANT_FILAS][CANT_FILAS];
escaques = new Mat[CANT_FILAS][CANT_FILAS];
//TODO init tablero
}
public static Modelador getInstance(){
if (instance == null){
instance = new Modelador();
}
return instance;
}
public void dividirTablero(Mat inputFrame){
Rect roi = Calibracion.getInstance().getRectPOI();
Mat proccesFrame = inputFrame.submat(roi);
int largo = proccesFrame.rows();
int ancho = proccesFrame.cols();
// obtengo cuanto mide en ancho y largo un escaque
int largoEscaque = (int) Math.floor(largo /(CANT_FILAS+1) );//+1 porque se toma un escaque mas de borde
int anchoEscaque = (int) Math.floor(ancho /(CANT_FILAS+1) );
Log.i(TAG, "largoEscaque= " + largoEscaque);
Log.i(TAG, "anchoEscaque= " + anchoEscaque);
for (int i = 0; i < CANT_FILAS; i++){
for (int j = 0; j < CANT_FILAS; j++){
int rowStart=(int)Math.floor(largoEscaque/2)+ i*largoEscaque +2;
int rowEnd = (int)Math.floor(largoEscaque/2)+i*largoEscaque + largoEscaque -2;
int colStart = (int)Math.floor(anchoEscaque/2)+j*anchoEscaque + 2;
int colEnd = (int)Math.floor(anchoEscaque/2)+j*anchoEscaque + anchoEscaque - 2;;
escaques[i][j]= inputFrame.submat(rowStart, rowEnd, colStart, colEnd);
}
}
}
public Mat dibujarEscaque(int i, int j){
Mat tmp = Mat.zeros(Calibracion.getInstance().getImagenCalibrada().size(), CvType.CV_8UC4);
Mat subm = tmp.submat(new Rect(new Point(0,0) ,escaques[i][j].size()));
escaques[i][j].copyTo(subm);
return tmp;
}
}
...@@ -7,9 +7,9 @@ import java.util.List; ...@@ -7,9 +7,9 @@ import java.util.List;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import uy.edu.fing.chesstrack.modulovision.imgproc.BackgroundSupress;
import uy.edu.fing.chesstrack.modulovision.imgproc.Homografia; import uy.edu.fing.chesstrack.modulovision.imgproc.Homografia;
import uy.edu.fing.chesstrack.modulovision.imgproc.ImgProcInterface; import uy.edu.fing.chesstrack.modulovision.imgproc.ImgProcInterface;
import uy.edu.fing.chesstrack.modulovision.imgproc.SustraccionDeFondo;
import android.util.Log; import android.util.Log;
...@@ -25,9 +25,11 @@ public class Adquisicion { ...@@ -25,9 +25,11 @@ public class Adquisicion {
Log.i(TAG, "Cargado...."); Log.i(TAG, "Cargado....");
listProc = new ArrayList<ImgProcInterface>(); listProc = new ArrayList<ImgProcInterface>();
listProc.add( new Homografia()); //listProc.add( new Homografia());
//listProc.add( new EcualizarImagen()); //listProc.add( new EcualizarImagen());
listProc.add( new SustraccionDeFondo()); //listProc.add( new SustraccionDeFondo());
listProc.add( new BackgroundSupress());
} }
/** /**
......
...@@ -17,7 +17,7 @@ import org.opencv.video.BackgroundSubtractorMOG; ...@@ -17,7 +17,7 @@ import org.opencv.video.BackgroundSubtractorMOG;
import org.opencv.video.BackgroundSubtractorMOG2; import org.opencv.video.BackgroundSubtractorMOG2;
import org.opencv.imgproc.*; import org.opencv.imgproc.*;
import uy.edu.chesstrack.communication.Client; import uy.edu.fing.chesstrack.communication.Client;
import android.graphics.SumPathEffect; import android.graphics.SumPathEffect;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.ToneGenerator; import android.media.ToneGenerator;
......
...@@ -5,17 +5,18 @@ import org.opencv.core.Mat; ...@@ -5,17 +5,18 @@ import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f; import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point; import org.opencv.core.Point;
import org.opencv.core.Rect; import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Imgproc;
import uy.edu.fing.chesstrack.modulovision.Calibracion; import uy.edu.fing.chesstrack.modulovision.Calibracion;
import android.util.Log;
public class Homografia implements ImgProcInterface { public class Homografia implements ImgProcInterface {
private static final String TAG = "CHESSTRACK::Homografia"; private static final String TAG = "CHESSTRACK::Homografia";
private final Mat matrizTransformada; private final Mat matrizTransformada;
private final Rect rectPOI; private final Rect rectPOI;
private int media;
private int offset;
public Homografia() throws Exception { public Homografia() throws Exception {
super(); super();
MatOfPoint2f vertices = Calibracion.getInstance().getVertices(); MatOfPoint2f vertices = Calibracion.getInstance().getVertices();
...@@ -32,15 +33,33 @@ public class Homografia implements ImgProcInterface { ...@@ -32,15 +33,33 @@ public class Homografia implements ImgProcInterface {
double d143 = Math.sqrt(Math.pow((p1.x- p3.x),2) + Math.pow((p1.y- p3.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)); 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)); media = (int) Math.floor( Math.round((d17+d749+d143+d4349)/4));
int offset = 2*(media/7); offset = 2*(media/7);
// offset = (media/7);
// Point p1, p2, p3, p4;
// Point[] ret = getBestPoints(vertices, Calibracion.getInstance().getImagenCalibrada().size());
// Log.i(TAG, "coso= " + ret);
// p1 = ret[0];
// p2 = ret[1];
// p3 = ret[2];
// p4 = ret[3];
//
// double corner_width = Math.abs(Math.sqrt(Math.pow((p1.x- p2.x),2) + Math.pow((p1.y- p2.y),2))/7);
// double corner_height = corner_width;
//
// double real_left_x = 0 + corner_width;
// double real_right_x = 0 + 9 * corner_width;
// double real_top_y = 0 + corner_height;
// double real_bottom_y = 0 + 9 * corner_height;
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); 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, real_left_x ,real_top_y ,real_right_x ,real_top_y, real_right_x,real_bottom_y, real_left_x,real_bottom_y );
dst.put(0,0, offset,offset ,offset+media,offset , offset,offset+media , offset+media,offset+media ); dst.put(0,0, offset,offset ,offset+media,offset , offset,offset+media , offset+media,offset+media );
Log.i(TAG, "offset= " + offset); // Log.i(TAG, "corner_width= " + corner_width);
Log.i(TAG, "media = " + media); // Log.i(TAG, "corner_height = " + corner_height);
// EL offset es casi el tamaño de una celda // 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)); //rectPOI = new Rect(new Point(0,0), new Point(9*corner_width,9*corner_height) );
rectPOI = new Rect(new Point(0,0), new Point(2*offset+media,2*offset+media) );
Calibracion.getInstance().setRectPOI(rectPOI); Calibracion.getInstance().setRectPOI(rectPOI);
matrizTransformada = Imgproc.getPerspectiveTransform(src,dst); matrizTransformada = Imgproc.getPerspectiveTransform(src,dst);
}else{ }else{
...@@ -53,14 +72,74 @@ public class Homografia implements ImgProcInterface { ...@@ -53,14 +72,74 @@ public class Homografia implements ImgProcInterface {
@Override @Override
public Mat procesarImagen(Mat inputFrame) { public Mat procesarImagen(Mat inputFrame) {
Imgproc.warpPerspective(inputFrame,inputFrame, matrizTransformada, inputFrame.size()); Imgproc.warpPerspective(inputFrame,inputFrame, matrizTransformada, inputFrame.size(),(Imgproc.INTER_LINEAR | Imgproc.CV_WARP_FILL_OUTLIERS));
Mat subMat = inputFrame.submat(rectPOI); Mat subMat = inputFrame.submat(rectPOI);
Mat tmp = Mat.zeros(inputFrame.size(), CvType.CV_8UC4); Mat tmp = Mat.zeros(inputFrame.size(), CvType.CV_8UC4);
Mat matTMP = tmp.submat(rectPOI); Mat matTMP = tmp.submat(rectPOI);
subMat.copyTo(matTMP); subMat.copyTo(matTMP);
return tmp; return tmp;
//Core.circle(inputFrame, new Point(offset,offset), offset, new Scalar(0,0 , 255, 255));
//Core.circle(inputFrame, new Point(offset,offset), 5, new Scalar(0,255, 0, 255));
// return inputFrame;
} }
private Point[] getBestPoints(MatOfPoint2f vertices, Size tamanio){
Point[] ret = new Point[4];
double d, dx ,dy;
double betst_dist = Double.MAX_VALUE;
Point aux = new Point(0,0);
for(Point pt : vertices.toList()){
// dx = pt.x-aux.x;
// dy= pt.y -aux.y;
// d = dx*dx + dy*dy;
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;
}
}
betst_dist = Double.MAX_VALUE;
aux = new Point(tamanio.width,0);
for(Point pt : vertices.toList()){
// dx = pt.x - aux.x;
// dy= pt.y -aux.y;
// d = dx*dx + dy*dy;
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;
}
}
betst_dist = Double.MAX_VALUE;
aux = new Point(tamanio.width,tamanio.height);
for(Point pt : vertices.toList()){
// dx = pt.x-aux.x;
// dy= pt.y -aux.y;
// d = dx*dx + dy*dy;
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()){
// dx = pt.x-aux.x;
// dy= pt.y -aux.y;
// d = dx*dx + dy*dy;
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;
}
} }
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