Codigo fuente desarrollado

 Manejando una estructura que busca ser ordenada y convencional para el desarrollo de algoritmos en este trabajo se utilizaron los archivos: Cabecera.hpp, que contiene la estructura del programa tanto las clases como los atributos y metodos; Principal.cpp, que definira los parametros y dond ese definirar los metodos a ser ejecutados; Fuente.cpp, en donde esta todo el codigo desarrollado, el desarrollo de funciones y metodos que nos permite realizar las funciones del programa.


  1. Cabecera.hpp



  • #include <cstring>
    #include <math.h>
    #include <vector>
    #include <fstream>
    #include <sstream>

    //librerias c puro .h
    #include <dirent.h>

    #include <iostream>
    #include <cstdlib>

    #include <opencv2/core/core.hpp> // Contiene los elementos básicos como el objeto Mat (matriz que representa la imagen)
    #include <opencv2/highgui/highgui.hpp> // Contiene los elementos para crear una interfaz gráfica básica
    #include <opencv2/imgcodecs/imgcodecs.hpp> // Contiene las funcionalidad para acceder a los códecs que permiten leer diferentes formatos de imagen (JPEG, JPEG-2000, PNG, TIFF, GIF, etc.)
    // Librerías para acceder al video y para poder escribir vídeos en disco
    #include <opencv2/video/video.hpp> 
    #include <opencv2/videoio/videoio.hpp>

    #include <opencv2/imgproc/imgproc.hpp> // Librería para realizar operaciones de PDI 


    using namespace std;
    using namespace cv;

    class detectorMovimiento{
        private:
            string pathVideo1;
            string pathVideo2;
            string pathVideo3;        

        public:
            detectorMovimiento(string,string,string);
            void obtenerAreaInteres();
    };


    1. Principal.cpp

    #include "Cabecera.hpp"

        int main(int argc, char *argv[]){

            detectorMovimiento movimiento("/home/adrian/Documents/8vo Ciclo/Vision por Comutador/Practicas/Proyecto Interciclo/video1.mp4","/home/adrian/Documents/8vo Ciclo/Vision por Comutador/Practicas/Proyecto Interciclo/video2.mp4","/home/adrian/Documents/8vo Ciclo/Vision por Comutador/Practicas/Proyecto Interciclo/video3.mp4"); 
              movimiento.obtenerAreaInteres();
                    return 0;   
                  }
                  1. Fuente.cpp


                  #include "Cabecera.hpp"
                    #include <ctime>
                      int thresholdVal = 18;
                        int thresGaus1Val = 1;
                          int thresMorfologicasVal = 5;
                            int thresDCVal = 31;

                                int mascara1 = 0;
                                  int mascaraMorfologica = 0;
                                    int mascaraDC = 0;

                                        int contador =0;
                                          Mat elementoInundar;
                                            Mat elementoCruz;
                                              detectorMovimiento::detectorMovimiento(string pathVideo1, string pathVideo2, string pathVideo3){
                                                    
                                                      this->pathVideo1 = pathVideo1;
                                                        this->pathVideo2 = pathVideo2;
                                                          this->pathVideo3 = pathVideo3;
                                                          }
                                                            void eventoTrackThreshold(int v, void *pP1){
                                                                  cout << "Valor Thres: " << v << endl;
                                                                }
                                                                  void eventoTrackThresholdGaus(int u, void *pP2){
                                                                        cout << "Valor Gauss 1: " << u << endl;
                                                                      }
                                                                        void eventoTrackThresholdMorfologicas(int t, void *pP3){
                                                                              cout << "Valor Morfologico : " << t << endl;
                                                                                if(thresMorfologicasVal>0)
                                                                                      elementoCruz = getStructuringElement(MORPH_CROSS, Size(thresMorfologicasVal,thresMorfologicasVal), Point(-1,-1));
                                                                                }

                                                                                    void eventoTrackThresholdDilateClose(int w, void *pP4){
                                                                                          cout << "Valor DilateClose: " << w << endl;
                                                                                            
                                                                                              elementoInundar = getStructuringElement(MORPH_ELLIPSE, Size(thresDCVal, thresDCVal), Point(-1,-1));
                                                                                                

                                                                                            }

                                                                                                void detectorMovimiento::obtenerAreaInteres(){
                                                                                                      
                                                                                                        VideoCapture video(pathVideo1);
                                                                                                          VideoCapture video(pathVideo1);
                                                                                                            //VideoCapture video(0);
                                                                                                              int ratio = 3;
                                                                                                                int minT = 50;
                                                                                                                  int maxT = ratio*minT;

                                                                                                                      if(video.isOpened()){
                                                                                                                            Mat frame;
                                                                                                                              Mat actual;
                                                                                                                                Mat anterior;
                                                                                                                                  Mat resta;
                                                                                                                                    
                                                                                                                                      Mat imgGray;
                                                                                                                                        
                                                                                                                                          Mat thresholding;
                                                                                                                                            Mat bordes;
                                                                                                                                              Mat seccionInteres;
                                                                                                                                                
                                                                                                                                                  Mat apertura;
                                                                                                                                                    
                                                                                                                                                      /////prueba2//
                                                                                                                                                        Mat topHatAc;
                                                                                                                                                          Mat blackHatAc;
                                                                                                                                                            Mat sumaAc;
                                                                                                                                                              
                                                                                                                                                                Mat topHatAn;
                                                                                                                                                                  Mat blackHatAn;
                                                                                                                                                                    Mat sumaAn;
                                                                                                                                                                      
                                                                                                                                                                        Mat apoyo;
                                                                                                                                                                          
                                                                                                                                                                            Mat fondo;
                                                                                                                                                                              Mat bordesResta;
                                                                                                                                                                            
                                                                                                                                                                                  Mat antN;
                                                                                                                                                                                    namedWindow("Video Original");
                                                                                                                                                                                      namedWindow("Movimiento");
                                                                                                                                                                                        
                                                                                                                                                                                          
                                                                                                                                                                                            createTrackbar("Threshold (m)", "Video Original", &thresholdVal, 255, eventoTrackThreshold, NULL);
                                                                                                                                                                                              createTrackbar("Filtro Gauss \n 0: 3x3 \n 1: 5x5 \n 2: 9x9 \n 3: 21x21 ", "Video Original", &thresGaus1Val, 3, eventoTrackThresholdGaus, NULL);
                                                                                                                                                                                                createTrackbar("Mascara TopHat/BlackHat(impares)", "Video Original", &thresMorfologicasVal, 100, eventoTrackThresholdMorfologicas, NULL);
                                                                                                                                                                                                  createTrackbar("Mascara Dilate/Close (impares)", "Video Original", &thresDCVal, 100, eventoTrackThresholdDilateClose, NULL);
                                                                                                                                                                                                 
                                                                                                                                                                                                      Mat elementoBorrarPuntos = getStructuringElement(MORPH_CROSS, Size(9,9), Point(-1,-1));

                                                                                                                                                                                                            while(true){
                                                                                                                                                                                                                  video >> frame;
                                                                                                                                                                                                                    if (frame.rows==0 || frame.cols==0)
                                                                                                                                                                                                                          break;
                                                                                                                                                                                                                        resize(frame, frame, Size(), 0.45, 0.45);

                                                                                                                                                                                                                            fondo= frame.clone();
                                                                                                                                                                                                                              
                                                                                                                                                                                                                                if(thresGaus1Val==0)
                                                                                                                                                                                                                                      mascara1 = 3;
                                                                                                                                                                                                                                    if(thresGaus1Val==1)
                                                                                                                                                                                                                                          mascara1 =5;
                                                                                                                                                                                                                                        if(thresGaus1Val==2)
                                                                                                                                                                                                                                              mascara1 =9;
                                                                                                                                                                                                                                            if(thresGaus1Val==3)
                                                                                                                                                                                                                                                  mascara1 =21;
                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                    GaussianBlur(frame, frame, Size(mascara1,mascara1), 1.7,1.7);
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                        cvtColor(frame, imgGray, COLOR_BGR2GRAY);

                                                                                                                                                                                                                                                            actual = imgGray.clone();
                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                if(anterior.rows==0 || anterior.cols==0){
                                                                                                                                                                                                                                                                      anterior = imgGray.clone();
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                          morphologyEx(actual, topHatAc, MORPH_TOPHAT, elementoCruz);
                                                                                                                                                                                                                                                                            morphologyEx(actual, blackHatAc, MORPH_BLACKHAT, elementoCruz);
                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                sumaAc = actual + abs(topHatAc-blackHatAc);
                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                    morphologyEx(anterior, topHatAn, MORPH_TOPHAT, elementoCruz);
                                                                                                                                                                                                                                                                                      morphologyEx(anterior, blackHatAn, MORPH_BLACKHAT, elementoCruz);
                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                          sumaAn = anterior + abs(topHatAn-blackHatAn);
                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                              resta = cv::abs(sumaAc - sumaAn);
                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                  GaussianBlur(resta, resta, Size( 5,5), 1.7,1.7);
                                                                                                                                                                                                                                                                                                    morphologyEx(resta, resta, MORPH_OPEN, elementoBorrarPuntos);

                                                                                                                                                                                                                                                                                                        threshold(resta, resta, thresholdVal, 255, THRESH_BINARY);
                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                            Canny(resta, seccionInteres, minT, maxT, 3);
                                                                                                                                                                                                                                                                                                              morphologyEx(seccionInteres, seccionInteres, MORPH_DILATE, elementoInundar);
                                                                                                                                                                                                                                                                                                                morphologyEx(seccionInteres, seccionInteres, MORPH_CLOSE, elementoInundar);
                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                    morphologyEx(seccionInteres, seccionInteres, MORPH_OPEN, elementoBorrarPuntos);
                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                        Vec3b pixel;
                                                                                                                                                                                                                                                                                                                          for(size_t i = 0; i< actual.rows; i++){
                                                                                                                                                                                                                                                                                                                                for(size_t j = 0; j< actual.cols; j++){
                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                        if( (int) seccionInteres.at<uchar>(i,j) != 0)
                                                                                                                                                                                                                                                                                                                                              break;
                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                            fondo.at<Vec3b>(i,j) = Vec3b(0,0,0);
                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                    for(int j = actual.cols-1; j >= 0; j--){
                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                            if( (int) seccionInteres.at<uchar>(i,j) != 0){
                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                    break;
                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                  fondo.at<Vec3b>(i,j) = Vec3b(0,0,0);
                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                          imshow("Video Original", frame);
                                                                                                                                                                                                                                                                                                                                                                            imshow("Movimiento", resta);
                                                                                                                                                                                                                                                                                                                                                                              imshow("Area de interes", seccionInteres);
                                                                                                                                                                                                                                                                                                                                                                                imshow("Final", fondo);

                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                      if (waitKey(23)==27)
                                                                                                                                                                                                                                                                                                                                                                                            break;
                                                                                                                                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                          }    
                                                                                                                                                                                                                                                                                                                                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                    

                                                                                                                                                                                                                                                                                                                                                                                                Comentarios