/** Realiza la deteccion de todas las celulas de la imagen de forma automatica. */ void DeteccionAut::deteccionBordesSupervisado(vector <int> x, vector <int> y) { // Maximun roundness of interested objects in the image for unsupervised algorithm double MAX_ROUND ( 1.4 ); int i, l; PointList **nsdetection=NULL; PointList **supervised; int ns=0, nsupervised; int *xs, *ys, ncs; // Se convierten los dos vectores de coordenadas (puntos introducidos por el usuario) a dos arrays dinámicos ncs = x.size ( ); xs = (int *) calloc (ncs, sizeof (int)); ys = (int *) calloc (ncs, sizeof (int)); for (i = 0; i < ncs; i++) { xs [i ] = x [i]; ys [i ] = y [i]; } //Pasamos las coordenadas obtenidas por el algoritmo no supervisado pasoCoordGovocitosPointList(nsdetection); //utilizando funcion aplico a deteccion non supervisada supervised = superviseDetection(contour, num_edges, num_cols, num_rows, MIN_DIAMETER, MAX_DIAMETER, MAX_ROUND, nsdetection, ns, xs, ys, ncs, &nsupervised); for ( l = 0; l <nsupervised; l++) { /************************Pintado govocitos*************************/ PointList *p = supervised[l]; /*Guardamos los puntos en un objeto celula y lo añadimos al objeto de listado de celulas*/ pasoCoordSistemaGovocitos(p); /*******************************************************************/ } for(i=0; i<nsupervised; i++) { free_point_list(supervised[i]); free(supervised[i]->point); free(supervised[i]); } free(supervised); for(i=0; i<ns; i++) { free_point_list(nsdetection[i]); free(nsdetection[i]->point); free(nsdetection[i]); } free(nsdetection); free(xs); free(ys); }
/** Destructor. */ DeteccionAut::~DeteccionAut() { for(int i = 0; i < num_edges; i++) { free_point_list(contour[i]); free(contour[i]->point); free(contour[i]); } free(contour); img = NULL; img_grey = NULL; contour = NULL; }
/** Carga los ficheros de clasificacion automatica de estados y clases y clasifica las celulas de la imagen cuya ruta corresponde con la pasada por parametro. @param ruta, string de la ruta de la imagen a analizar. @param listCell, objeto ListadoCeulas que contiene todas las celulas de la imagen a analizar. */ void Clasificador::clasificarCelulas(string ruta, ListadoCelulas &listCell) { struct svm_model *svmEst, *svmCl; float media_est[N_ENTRADAS], desv_est[N_ENTRADAS]; float media_cl[N_ENTRADAS], desv_cl[N_ENTRADAS]; int y1, y2; int numCelulas, numPuntos; vector <int> *cx, *cy; Image *img, *img_grey, *img_bin; img = read_img ( ruta.c_str() ); img_grey = rgb_to_gray (img); numCelulas = listCell.getNumCelulas(); //Obtenemos path de los ficheros de clasificacion const char *f_svm_Est = fichClasificEst.c_str(); const char *f_med_desv_Est = fichMediaDesviaEst.c_str(); const char *f_svm_Cl = fichClasificCl.c_str(); const char *f_med_desv_Cl = fichMediaDesviaCl.c_str(); //Creamos los svm svmEst = crea_svm(f_svm_Est, f_med_desv_Est, media_est, desv_est); svmCl = crea_svm(f_svm_Cl, f_med_desv_Cl, media_cl, desv_cl); for(int i = 0; i < numCelulas; i++) { PointList *puntos; if(listCell.getCelula(i)->getEstadoCelula() != "outimage" && listCell.getCelula(i)->getClaseCelula() != "outimage") { cx = listCell.getCelula(i)->getBordeCellX(); cy = listCell.getCelula(i)->getBordeCellY(); numPuntos = cx->size(); puntos = alloc_point_list(numPuntos); //Convertimos puntos for(int j = 0; j < numPuntos; j++) { puntos->point[j] = alloc_point((*cy)[j], (*cx)[j]); } puntos->type = GEN_VALID; img_bin=computeBinImage ( puntos, get_num_rows ( img_grey ) , get_num_cols (img_grey)); // double* resul = texture_features_haralick(img, puntos, 1, 1); double * resul = texture_features_glrls_grey ( img_grey, img_bin, puntos, 1000, 0); float resul2[N_ENTRADAS], resul3[N_ENTRADAS]; for(unsigned int z = 0; z < N_ENTRADAS; z++) { resul2[z] = (float)resul[z]; resul3[z] = (float)resul[z]; } //Salida clasificador clases y1 = saida_svm(svmCl, resul2, media_cl, desv_cl); switch(y1) { case 0: listCell.getCelula(i)->setClaseCelula("cn"); break; case 1: listCell.getCelula(i)->setClaseCelula("sn"); break; } //Salida clasificador estados y2 = saida_svm(svmEst, resul3, media_est, desv_est); switch (y2) { case 0: listCell.getCelula(i)->setEstadoCelula("ac"); break; case 1: listCell.getCelula(i)->setEstadoCelula("hid"); break; case 2: listCell.getCelula(i)->setEstadoCelula("vit");// vitelinas o atresicas break; } free_point_list ( puntos ); free ( puntos->point ); free ( puntos ); // delete puntos; free ( resul ); } } destrue_svm(svmCl); destrue_svm(svmEst); free_img ( img ); free ( img ); free_img ( img_grey ); free ( img_grey ); }
/** Realiza la deteccion de todas las celulas de la imagen de forma automatica. */ bool DeteccionAut::deteccionBordesNoSupervisado() { // Maximun number of objects in the image int NUM_CELLS (10000 ); // Maximun roundness of interested objects in the image for unsupervised algorithm double MAX_ROUND ( 1.4 ); int i, l; int nout; PointList **out; if(diamMax != 0) { MAX_DIAMETER = diamMax / calibracion; } else { MAX_DIAMETER = MAX_DIAMETER / calibracion; } if(diamMin != 0) { MIN_DIAMETER = diamMin / calibracion; } else { MIN_DIAMETER = MIN_DIAMETER / calibracion; } // Read the input image img = read_img ( ruta.c_str() ); if ( !is_rgb_img ( img ) || (img == NULL)) { fprintf ( stderr, "Input image ( %s ) must be color image !", ruta.c_str() ); return false; } /********************************Reloj**************************************/ // comienzo = clock(); /***************************************************************************/ /***************************************************************************/ string mensaje, titulo; //mensaje = "Se va a proceder al cálculo de los bordes de los ovocitos, el proceso durara aproximadamente 1 minuto durante el cual la aplicación quedara bloqueada, ¿Esta seguro de continuar?"; mensaje = "The automatic oocyte edge detection is going to be performed. The application will be unaccesible during this process. Are you sure that you want to continue?"; //titulo = "Cálculo automatico de bordes de los ovocitos"; titulo = "Oocyte edge detection"; if(Dialogos::dialogoConfirmacion(mensaje, titulo)) { //DialogoBarraProgres dProgres("Detectando Bordes"); DialogoBarraProgres dProgres("Oocyte edge detection"); dProgres.ejectuaDialogoProgreso(); dProgres.setEstadoBarraProgreso(0.1); dProgres.setPercentText(0.1); Utiles::actualizarInterfaz(); /***************************************************************************/ // Detect the edges in the input image num_rows=get_num_rows(img); num_cols=get_num_cols(img); /*Este dobre bucle que ven a continuacion e a aplicacion do filtro de Canny para distintos umbrais e suavizados (distintas escalas). O que obtemos e en contour (unha estrutura da libreria fourier.0.8 que almacena os bordes que teñen mais de MIN_LENGTH pixels) e en n_all o numero de borde. */ // Este paso e independente de que o algoritmo se aplique de forma supervisada ou non supervisada. //num_edges = 0; if(num_rows > MAX_ROWS_IMAGE || num_cols > MAX_COLS_IMAGE) { fprintf(stderr, "Error: image of size %i x %i too large (max. size= %i x %i): border detection aborted\n", num_rows, num_cols, MAX_ROWS_IMAGE, MAX_COLS_IMAGE); Dialogos::dialogoError("Image too large for border detection", "Error"); return false; } img_grey=rgb_to_gray(img); // Aplicando o filtro de canny multiescalar contour=detect_edge_multiscalar_canny ( img_grey, &num_edges, MIN_DIAMETER, NUM_CELLS); if ( IS_NULL( contour)) { printf ( "Invalid point list objects !"); } // printf("Tempo transcurrido: %f bordes= %d \n", stop_timer ( start_time), num_edges) ; /**********************************fase 1****************************/ /* final = clock(); cout<<"Tiempo fase 1 "<<(final - comienzo)/(double) CLOCKS_PER_SEC<<endl; comienzo = clock();*/ dProgres.setEstadoBarraProgreso(0.5); dProgres.setPercentText(0.5); Utiles::actualizarInterfaz(); /**************************************************************************/ // A partir de aqui fanse calculos para analizar os bordes que hai en contour e asi mostrar so os que cremos que son bordes de govocitos maduros. //utilizando funcion aplico a deteccion non supervisada out=nonSuperviseDetection(contour, num_edges, num_cols, num_rows, MIN_DIAMETER, MAX_DIAMETER, MAX_ROUND, &nout); /**********************************Reloj fase 2****************************/ /* final = clock(); cout<<"Tiempo fase 2 "<<(final - comienzo)/(double) CLOCKS_PER_SEC<<endl; comienzo = clock();*/ dProgres.setEstadoBarraProgreso(0.9); dProgres.setPercentText(0.9); Utiles::actualizarInterfaz(); /**************************************************************************/ for ( l = 0; l <nout; l++) { /************************Pintado govocitos*************************/ PointList *p = out[l]; /*Guardamos los puntos en un objeto celula y lo añadimos al objeto de listado de celulas*/ pasoCoordSistemaGovocitos(p); /*******************************************************************/ } for(i=0; i<nout; i++) { free_point_list(out[i]); free(out[i]->point); free(out[i]); } free(out); free_img(img); free_img(img_grey); free(img); free(img_grey); /**********************************Reloj fase 3****************************/ /* final = clock(); cout<<"Tiempo fase 3 "<<(final - comienzo)/(double) CLOCKS_PER_SEC<<endl;*/ dProgres.setEstadoBarraProgreso(1); dProgres.setPercentText(1); Utiles::actualizarInterfaz(); dProgres.cierraVentanaProgreso(); /**************************************************************************/ } else { return false; } return true; }