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

mas cosas

parent 71ca961f
No related branches found
No related tags found
No related merge requests found
......@@ -14,139 +14,169 @@ 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 Mat tablero;
private final int CANT_FILAS = 8;
private final int CANT_SCAQUE = CANT_FILAS * CANT_FILAS;
private final int MIN_CANT_PIX_WHITE = 50;
private Mat tablero;
// se cuentan de arrib a abajo y de izquierda a derecha
private final Mat[][] escaques;
//private static Modelador instance;
private final Mat[][] escaques;
// private static Modelador instance;
public Modelador(){
public Modelador() {
tablero = Mat.zeros(new Size(8, 8), CvType.CV_8UC1);
escaques = new Mat[CANT_FILAS][CANT_FILAS];
}
public void dividirTablero(Mat inputFrame){
Imgproc.cvtColor(inputFrame ,inputFrame, Imgproc.COLOR_RGBA2GRAY,4);
public void dividirTablero(Mat inputFrame) {
Imgproc.cvtColor(inputFrame, inputFrame, Imgproc.COLOR_RGBA2GRAY, 4);
int largo = inputFrame.rows();
int ancho = inputFrame.cols();
// obtengo cuanto mide en ancho y largo un escaque
int largoEscaque = (int) Math.floor(largo /(CANT_FILAS+2) );//+2 porque se toma un escaque mas de borde
int anchoEscaque = (int) Math.floor(ancho /(CANT_FILAS+2) );
int largoEscaque = (int) Math.floor(largo / (CANT_FILAS + 2));// +2 porque se toma un escaque mas de borde
int anchoEscaque = (int) Math.floor(ancho / (CANT_FILAS + 2));
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)+ i*largoEscaque+4;
int rowEnd = (int)Math.floor(largoEscaque)+i*largoEscaque + largoEscaque-4;
int colStart = (int)Math.floor(anchoEscaque)+j*anchoEscaque+4;
int colEnd = (int)Math.floor(anchoEscaque)+j*anchoEscaque + anchoEscaque-4;
escaques[i][j]= inputFrame.submat(colStart, colEnd,rowStart, rowEnd);
for (int i = 0; i < CANT_FILAS; i++) {
for (int j = 0; j < CANT_FILAS; j++) {
int rowStart = (int) Math.floor(largoEscaque) + i
* largoEscaque + 4;
int rowEnd = (int) Math.floor(largoEscaque) + i * largoEscaque
+ largoEscaque - 4;
int colStart = (int) Math.floor(anchoEscaque) + j
* anchoEscaque + 4;
int colEnd = (int) Math.floor(anchoEscaque) + j * anchoEscaque
+ anchoEscaque - 4;
escaques[i][j] = inputFrame.submat(colStart, colEnd, rowStart,
rowEnd);
}
}
}
private int getHayFichaEnEscaque(int i, int j){
private int getHayFichaEnEscaque(int i, int j) {
int ret = 0;
Mat mIntermediateMat = new Mat();
Imgproc.Sobel(escaques[i][j], mIntermediateMat, CvType.CV_8U, 1, 1);
Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0);
Imgproc.threshold(mIntermediateMat, mIntermediateMat, 70, 255, Imgproc.THRESH_BINARY);
int cant_pix = mIntermediateMat.cols()*mIntermediateMat.rows();
Imgproc.threshold(mIntermediateMat, mIntermediateMat, 70, 255,
Imgproc.THRESH_BINARY);
int cant_pix = mIntermediateMat.cols() * mIntermediateMat.rows();
int cant_pix_white = Core.countNonZero(mIntermediateMat);
if (cant_pix_white > (50) ){
if (cant_pix_white > (MIN_CANT_PIX_WHITE)) {
ret = 1;
}
return ret;
}
public Mat getMatrizFichas(Mat frame){
public Mat getMatrizFichas(Mat frame) {
dividirTablero(frame);
tablero = Mat.zeros(new Size(8, 8), CvType.CV_8UC1);
for (int i = 0; i < CANT_FILAS; i++){
for (int j = 0; j < CANT_FILAS; j++){
tablero.put(i, j, getHayFichaEnEscaque(i,j));
for (int i = 0; i < CANT_FILAS; i++) {
for (int j = 0; j < CANT_FILAS; j++) {
tablero.put(i, j, getHayFichaEnEscaque(i, j));
}
}
return tablero;
}
public Mat dibujarEscaque(){
Mat tmp = Mat.ones(Calibracion.getInstance().get_sizeFrame(), CvType.CV_8UC4);
Mat subm ; //tmp.submat(new Rect(new Point(0,0) ,escaques[i][j].size()));
//escaques[i][j].copyTo(subm);
public Mat dibujarEscaque() {
Mat tmp = Mat.ones(Calibracion.getInstance().get_sizeFrame(),
CvType.CV_8UC4);
Mat subm; // tmp.submat(new Rect(new Point(0,0)
// ,escaques[i][j].size()));
// escaques[i][j].copyTo(subm);
int pos_x = 0;
int pos_y = 0;
for (int i = 0; i < CANT_FILAS; i++){
for (int j = 0; j < CANT_FILAS; j++){
pos_x = (int) (escaques[i][j].size().width*i);
pos_y = (int) (escaques[i][j].size().height*j);
subm = tmp.submat(new Rect(new Point(pos_x,pos_y) ,escaques[i][j].size()));
for (int i = 0; i < CANT_FILAS; i++) {
for (int j = 0; j < CANT_FILAS; j++) {
pos_x = (int) (escaques[i][j].size().width * i);
pos_y = (int) (escaques[i][j].size().height * j);
subm = tmp.submat(new Rect(new Point(pos_x, pos_y),
escaques[i][j].size()));
escaques[i][j].copyTo(subm);
Core.putText(tmp, Integer.toString(i)+ "-" +Integer.toString(j), new Point(pos_x+1, pos_y+1),Core.FONT_HERSHEY_SIMPLEX, 0.3 , new Scalar(255,0,0));
Core.putText(tmp,
Integer.toString(i) + "-" + Integer.toString(j),
new Point(pos_x + 1, pos_y + 1),
Core.FONT_HERSHEY_SIMPLEX, 0.3, new Scalar(255, 0, 0));
}
}
return tmp;
}
public Mat dibujarEscaquesCanny(){
Mat tmp = Mat.zeros(Calibracion.getInstance().get_sizeFrame(), CvType.CV_8UC4);
Mat subm ; //tmp.submat(new Rect(new Point(0,0) ,escaques[i][j].size()));
//escaques[i][j].copyTo(subm);
public Mat dibujarEscaquesCanny() {
Mat tmp = Mat.zeros(Calibracion.getInstance().get_sizeFrame(),
CvType.CV_8UC4);
Mat subm; // tmp.submat(new Rect(new Point(0,0)
// ,escaques[i][j].size()));
// escaques[i][j].copyTo(subm);
Mat mIntermediateMat = new Mat();
int pos_x = 0;
int pos_y = 0;
for (int i = 0; i < CANT_FILAS; i++){
for (int j = 0; j < CANT_FILAS; j++){
pos_x = (int) (escaques[i][j].size().width*i);
pos_y = (int) (escaques[i][j].size().height*j);
subm = tmp.submat(new Rect(new Point(pos_x,pos_y) ,escaques[i][j].size()));
for (int i = 0; i < CANT_FILAS; i++) {
for (int j = 0; j < CANT_FILAS; j++) {
pos_x = (int) (escaques[i][j].size().width * i);
pos_y = (int) (escaques[i][j].size().height * j);
subm = tmp.submat(new Rect(new Point(pos_x, pos_y),
escaques[i][j].size()));
Imgproc.Canny(escaques[i][j], mIntermediateMat, 80, 90);
Imgproc.cvtColor(mIntermediateMat, subm, Imgproc.COLOR_GRAY2RGBA,4);
Imgproc.cvtColor(mIntermediateMat, subm,
Imgproc.COLOR_GRAY2RGBA, 4);
// escaques[i][j].copyTo(subm);
// Core.putText(tmp, Integer.toString(i)+ "-" +Integer.toString(j), new Point(pos_x+1, pos_y+1),Core.FONT_HERSHEY_SIMPLEX, 0.3 , new Scalar(255,0,0));
escaques[i][j].copyTo(subm);
Core.putText(tmp, Integer.toString(i) + "-" + Integer.toString(j), new Point(pos_x + 1, pos_y + 1), Core.FONT_HERSHEY_SIMPLEX, 0.3, new Scalar(255, 0, 0));
}
}
return tmp;
}
public Mat dibujarEscaquesSobel(){
Mat tmp = Mat.zeros(Calibracion.getInstance().getRectROI().size(), CvType.CV_8UC4);
Mat subm ; //tmp.submat(new Rect(new Point(0,0) ,escaques[i][j].size()));
//escaques[i][j].copyTo(subm);
public Mat dibujarEscaquesSobel(int version) {
Mat tmp = Mat.zeros(Calibracion.getInstance().getRectROI().size(),
CvType.CV_8UC4);
Mat subm; // tmp.submat(new Rect(new Point(0,0)
// ,escaques[i][j].size()));
// escaques[i][j].copyTo(subm);
Mat mIntermediateMat = new Mat();
int pos_x = 0;
int pos_y = 0;
for (int i = 0; i < CANT_FILAS; i++){
for (int j = 0; j < CANT_FILAS; j++){
pos_x = (int) (escaques[i][j].size().width*i);
pos_y = (int) (escaques[i][j].size().height*j);
subm = tmp.submat(new Rect(new Point(pos_x,pos_y) ,escaques[i][j].size()));
//Imgproc.cvtColor(escaques[i][j] ,mIntermediateMat, Imgproc.COLOR_RGBA2GRAY,4);
Imgproc.Sobel(escaques[i][j], mIntermediateMat, CvType.CV_8U, 1, 1);
for (int i = 0; i < CANT_FILAS; i++) {
for (int j = 0; j < CANT_FILAS; j++) {
pos_x = (int) (escaques[i][j].size().width * i);
pos_y = (int) (escaques[i][j].size().height * j);
subm = tmp.submat(new Rect(new Point(pos_x, pos_y),
escaques[i][j].size()));
// Imgproc.cvtColor(escaques[i][j] ,mIntermediateMat,
// Imgproc.COLOR_RGBA2GRAY,4);
Imgproc.Sobel(escaques[i][j], mIntermediateMat, CvType.CV_8U,
1, 1);
Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0);
Imgproc.threshold(mIntermediateMat, mIntermediateMat, 70, 255, Imgproc.THRESH_BINARY);
int cant_pix = mIntermediateMat.cols()*mIntermediateMat.rows();
Imgproc.threshold(mIntermediateMat, mIntermediateMat, 70, 255,
Imgproc.THRESH_BINARY);
int cant_pix = mIntermediateMat.cols()
* mIntermediateMat.rows();
int cant_pix_white = Core.countNonZero(mIntermediateMat);
Log.i(TAG, "cant_pix=" + cant_pix);
Log.i(TAG, "cant_pix_white=" + cant_pix_white);
switch (version) {
case 1:
if (cant_pix_white > (MIN_CANT_PIX_WHITE)) {
Imgproc.cvtColor(mIntermediateMat, subm, Imgproc.COLOR_GRAY2RGBA, 4);
} else {
Imgproc.cvtColor(escaques[i][j], subm, Imgproc.COLOR_GRAY2RGBA, 4);
}
break;
case 2:
Imgproc.cvtColor(mIntermediateMat, subm, Imgproc.COLOR_GRAY2RGBA, 4);
break;
default:
Imgproc.cvtColor(escaques[i][j], subm, Imgproc.COLOR_GRAY2RGBA, 4);
break;
if (cant_pix_white > (50) ){
Imgproc.cvtColor(mIntermediateMat, subm, Imgproc.COLOR_GRAY2RGBA,4);
}else{
Imgproc.cvtColor(escaques[i][j], subm, Imgproc.COLOR_GRAY2RGBA,4);
}
}
}
return tmp;
}
}
......@@ -3,11 +3,11 @@ package uy.edu.fing.chesstrack.modulovision;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import uy.edu.fing.chesstrack.communication.Client;
import uy.edu.fing.chesstrack.modulomodelador.Modelador;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.util.Log;
import uy.edu.fing.chesstrack.communication.Client;
import uy.edu.fing.chesstrack.modulomodelador.Modelador;
public class Manager {
......@@ -31,7 +31,7 @@ public class Manager {
private static Client _client;
private static DetectorOclusion _detectorOclusion;
private static Modelador _modelador;
private boolean _debug;
private final boolean _debug;
protected Manager() {
_estado = JUGANDO;
......@@ -115,7 +115,7 @@ public class Manager {
// llamar al modelador
Mat aux = _modelador.getMatrizFichas(region);
Log.i(TAG, "MATRIZ=" + aux.dump());
region = _modelador.dibujarEscaquesSobel();
region = _modelador.dibujarEscaquesSobel(2);
if (_salida == OK) {
_client.SendData("Nuevo Tablero Valido!\n");
......
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