Bressam

LBP - I.A.

May 10th, 2018
252
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. //#include <opencv2/cv.h>
  6. #include <opencv2/highgui/highgui.hpp>
  7.  
  8. int main() {
  9.     IplImage* image, image2;
  10.     char caminho[9999];
  11.     int i, j, k, classe = 0, l, m, n;
  12.     float teste;
  13.     FILE * fp;
  14.     errno_t err;
  15.     //*** CRIA O ARQUIVO ARFF E SALVA O CABEÇALHO ***
  16.     //fp = fopen("baseLBP.arff", "w");
  17.     err = fopen_s(&fp, "baseLBP.arff", "w");
  18.     //fprintf(fp, "%base de teste criada por Alessandro and Zacarias para a disciplina de PDI - Engenharia de Software UEPG\n\n");
  19.     fprintf(fp, "@relation imgs_teste_lbp\n\n");
  20.  
  21.     for (i = 0; i<256; i++)
  22.         fprintf(fp, "@attribute At%i numeric\n", (i / 256));
  23.  
  24.  
  25.     fprintf(fp, "@attribute Class {1,2,3,4,5,6,7,8,9,10}\n\n");
  26.     fprintf(fp, "@DATA\n");
  27.  
  28.  
  29.     for (i = 0; i<2; i++) {
  30.         //sprintf(caminho, "C:\\Downloads\\image\\%d.jpg", i);
  31.         sprintf_s(caminho, 9999, "C:\\Downloads\\image\\%d.jpg", i);
  32.         image = cvLoadImage(caminho, 0);
  33.  
  34.         float h[256];// vetor de features com 256 valores disponiveis      
  35.  
  36.  
  37.         for (n = 0; n<256; n++) h[n] = 0.0;   //inicializa vetor de features
  38.  
  39.  
  40.                                               //*** VERIFICA SE EH UMA NOVA CLASSE (TIPO DA IMAGEM)***
  41.         if (i % 100 == 0) { //a cada 100 muda a categoria das fotos
  42.             classe = classe + 1;
  43.         }
  44.  
  45.  
  46.         int lbp[3][3];//vetor do lbp       
  47.         int lbpBinario[3][3];//vetor do lbp binario
  48.         int largura = image->width - 4;
  49.         int altura = image->height - 4;
  50.  
  51.         for (j = 0; j<largura; j++) {//vamos percorrer toda a imagem passando e criando a mascara, tem o -1 para pular a ultima linha de pixel
  52.             for (l = 0; l<3; l++) { //zera lbp
  53.                 for (n = 0; n<3; n++) {
  54.                     lbp[l][n] = 0;
  55.                 }
  56.             }
  57.             for (l = 0; l<3; l++) { //zera lbpbinario
  58.                 for (n = 0; n<3; n++) {
  59.                     lbpBinario[l][n] = 0;
  60.  
  61.                 }
  62.             }
  63.             for (k = 0; k<altura; k++) {
  64.                 for (l = 0; l<3; l++) { //pega a matrix 3X3 da imagem com os vales de cinza
  65.                     for (n = 0; n<3; n++) {
  66.                         CvScalar v;
  67.                         v = cvGet2D(image, k + l, +n);
  68.                         lbp[l][n] = (int)v.val[0];
  69.  
  70.                     }
  71.                 }
  72.  
  73.  
  74.  
  75.                 int central = lbp[1][1];//pega o valor central para ser comparado e gerar a matriz binaria
  76.                 for (l = 0; l<3; l++) {
  77.                     for (n = 0; n<3; n++) {
  78.                         if (lbp[l][n] >= central) {
  79.                             lbpBinario[l][n] = 1;
  80.                         }
  81.                         else {
  82.                             lbpBinario[l][n] = 0;
  83.                         }
  84.                     }
  85.                 }
  86.                 central = 0;//agora vai pegar os binarios e trasformar no novo decimal que sera o novo valor da cadeia LBP, gambiarra para trasnformar um decimal que eh `binario` em decimarl
  87.                 central = central + (128 * lbpBinario[0][0]);
  88.                 central = central + (64 * lbpBinario[0][1]);
  89.                 central = central + (32 * lbpBinario[0][2]);
  90.                 central = central + (16 * lbpBinario[1][2]);
  91.                 central = central + (8 * lbpBinario[2][2]);
  92.                 central = central + (4 * lbpBinario[2][1]);
  93.                 central = central + (2 * lbpBinario[2][0]);
  94.                 central = central + (1 * lbpBinario[1][0]);
  95.  
  96.                 h[central]++;   // histograma LBP
  97.  
  98.  
  99.             }
  100.  
  101.         }
  102.  
  103.         // Normalização
  104.         for (n = 0; n<256; n++)
  105.             h[n] = h[n] / (largura * altura);
  106.  
  107.  
  108.         // Salve em arquivo
  109.         for (n = 0; n<256; n++) {
  110.             fprintf(fp, "%f,", (h[n]));
  111.         }
  112.  
  113.         // Salva a classe
  114.         fprintf(fp, "%d\n", classe);
  115.  
  116.  
  117.     }
  118.     fclose(fp);
  119.     return 0;
  120. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment