AmbarG

paper

Jul 7th, 2026
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.68 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5. #include <string>
  6. #include <algorithm>
  7. #include <cstdlib>
  8.  
  9. struct PGMHeader {
  10.     int width = 0; int height = 0; int max_val = 0;
  11. };
  12.  
  13. bool readPGM(const std::string& filename, PGMHeader& header, std::vector<unsigned char>& data) {
  14.     std::ifstream file(filename, std::ios::binary);
  15.     if (!file.is_open()) return false;
  16.     std::string type; file >> type;
  17.     if (type != "P5") return false;
  18.     char ch; file >> ch;
  19.     while (ch == '#') { std::string comment; std::getline(file, comment); file >> ch; }
  20.     file.putback(ch);
  21.     file >> header.width >> header.height >> header.max_val;
  22.     file.ignore();
  23.     data.resize(header.width * header.height);
  24.     file.read(reinterpret_cast<char*>(data.data()), data.size());
  25.     return file.good();
  26. }
  27.  
  28. bool writePGM(const std::string& filename, const PGMHeader& header, const std::vector<unsigned char>& data) {
  29.     std::ofstream file(filename, std::ios::binary);
  30.     if (!file.is_open()) return false;
  31.     file << "P5\n" << header.width << " " << header.height << "\n" << header.max_val << "\n";
  32.     file.write(reinterpret_cast<const char*>(data.data()), data.size());
  33.     return file.good();
  34. }
  35.  
  36. bool writePPM(const std::string& filename, const PGMHeader& header, const std::vector<unsigned char>& rgb_data) {
  37.     std::ofstream file(filename, std::ios::binary);
  38.     if (!file.is_open()) return false;
  39.     file << "P6\n" << header.width << " " << header.height << "\n255\n";
  40.     file.write(reinterpret_cast<const char*>(rgb_data.data()), rgb_data.size());
  41.     return file.good();
  42. }
  43.  
  44. void procesarFiltro(int filtro, int rank, int size, const std::string& nombre_archivo) {
  45.     PGMHeader header;
  46.     std::vector<unsigned char> full_image;
  47.     std::vector<int> send_counts(size, 0), displacements(size, 0);
  48.  
  49.     if (rank == 0) {
  50.         readPGM(nombre_archivo, header, full_image);
  51.         int base_rows = header.height / size;
  52.         int remainder = header.height % size;
  53.         int current_disp = 0;
  54.         for (int i = 0; i < size; ++i) {
  55.             int rows_for_proc = base_rows + (i < remainder ? 1 : 0);
  56.             send_counts[i] = rows_for_proc * header.width;
  57.             displacements[i] = current_disp;
  58.             current_disp += send_counts[i];
  59.         }
  60.     }
  61.  
  62.     MPI_Bcast(&header.width, 1, MPI_INT, 0, MPI_COMM_WORLD);
  63.     MPI_Bcast(&header.height, 1, MPI_INT, 0, MPI_COMM_WORLD);
  64.     MPI_Bcast(&header.max_val, 1, MPI_INT, 0, MPI_COMM_WORLD);
  65.  
  66.     int base_rows = header.height / size;
  67.     int remainder = header.height % size;
  68.     int my_rows = base_rows + (rank < remainder ? 1 : 0);
  69.     int my_pixels_count = my_rows * header.width;
  70.  
  71.     std::vector<unsigned char> local_data(my_pixels_count);
  72.     MPI_Scatterv(full_image.data(), send_counts.data(), displacements.data(), MPI_UNSIGNED_CHAR,
  73.                  local_data.data(), my_pixels_count, MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD);
  74.  
  75.     std::vector<unsigned char> local_buffer((my_rows + 2) * header.width, 0);
  76.     std::copy(local_data.begin(), local_data.end(), local_buffer.begin() + header.width);
  77.  
  78.     int vec_sup = (rank == 0) ? MPI_PROC_NULL : rank - 1;
  79.     int vec_inf = (rank == size - 1) ? MPI_PROC_NULL : rank + 1;
  80.  
  81.     MPI_Sendrecv(&local_buffer[header.width], header.width, MPI_UNSIGNED_CHAR, vec_sup, 0,
  82.                  &local_buffer[(my_rows + 1) * header.width], header.width, MPI_UNSIGNED_CHAR, vec_inf, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  83.     MPI_Sendrecv(&local_buffer[my_rows * header.width], header.width, MPI_UNSIGNED_CHAR, vec_inf, 1,
  84.                  &local_buffer[0], header.width, MPI_UNSIGNED_CHAR, vec_sup, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  85.  
  86.     if (rank == 0) std::copy(local_buffer.begin() + header.width, local_buffer.begin() + 2 * header.width, local_buffer.begin());
  87.     if (rank == size - 1) std::copy(local_buffer.begin() + my_rows * header.width, local_buffer.begin() + (my_rows + 1) * header.width, local_buffer.begin() + (my_rows + 1) * header.width);
  88.  
  89.     std::vector<unsigned char> local_result(my_pixels_count, 0);
  90.  
  91.     if (filtro == 1) { // EROSIÓN
  92.         for (int i = 1; i <= my_rows; ++i) {
  93.             for (int j = 0; j < header.width; ++j) {
  94.                 unsigned char min_val = 255;
  95.                 for (int ky = -1; ky <= 1; ++ky) {
  96.                     for (int kx = -1; kx <= 1; ++kx) {
  97.                         int n_col = j + kx;
  98.                         if (n_col >= 0 && n_col < header.width) {
  99.                             unsigned char p = local_buffer[(i + ky) * header.width + n_col];
  100.                             if (p < min_val) min_val = p;
  101.                         }
  102.                     }
  103.                 }
  104.                 local_result[(i - 1) * header.width + j] = min_val;
  105.             }
  106.         }
  107.     }
  108.     else if (filtro == 2) { // DILATACIÓN
  109.         for (int i = 1; i <= my_rows; ++i) {
  110.             for (int j = 0; j < header.width; ++j) {
  111.                 unsigned char max_val = 0;
  112.                 for (int ky = -1; ky <= 1; ++ky) {
  113.                     for (int kx = -1; kx <= 1; ++kx) {
  114.                         int n_col = j + kx;
  115.                         if (n_col >= 0 && n_col < header.width) {
  116.                             unsigned char p = local_buffer[(i + ky) * header.width + n_col];
  117.                             if (p > max_val) max_val = p;
  118.                         }
  119.                     }
  120.                 }
  121.                 local_result[(i - 1) * header.width + j] = max_val;
  122.             }
  123.         }
  124.     }
  125.     else if (filtro == 3) { // DETECCIÓN DE NÓDULOS
  126.         std::vector<unsigned char> eroded(my_pixels_count, 255);
  127.         for (int i = 1; i <= my_rows; ++i) {
  128.             for (int j = 0; j < header.width; ++j) {
  129.                 unsigned char min_val = 255;
  130.                 for (int ky = -1; ky <= 1; ++ky) {
  131.                     for (int kx = -1; kx <= 1; ++kx) {
  132.                         int n_col = j + kx;
  133.                         if (n_col >= 0 && n_col < header.width) {
  134.                             unsigned char p = local_buffer[(i + ky) * header.width + n_col];
  135.                             if (p < min_val) min_val = p;
  136.                         }
  137.                     }
  138.                 }
  139.                 eroded[(i - 1) * header.width + j] = min_val;
  140.             }
  141.         }
  142.         std::copy(eroded.begin(), eroded.end(), local_buffer.begin() + header.width);
  143.         MPI_Sendrecv(&local_buffer[header.width], header.width, MPI_UNSIGNED_CHAR, vec_sup, 0, &local_buffer[(my_rows + 1) * header.width], header.width, MPI_UNSIGNED_CHAR, vec_inf, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  144.         MPI_Sendrecv(&local_buffer[my_rows * header.width], header.width, MPI_UNSIGNED_CHAR, vec_inf, 1, &local_buffer[0], header.width, MPI_UNSIGNED_CHAR, vec_sup, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  145.  
  146.         for (int i = 1; i <= my_rows; ++i) {
  147.             for (int j = 0; j < header.width; ++j) {
  148.                 unsigned char max_val = 0;
  149.                 for (int ky = -1; ky <= 1; ++ky) {
  150.                     for (int kx = -1; kx <= 1; ++kx) {
  151.                         int n_col = j + kx;
  152.                         if (n_col >= 0 && n_col < header.width) {
  153.                             unsigned char p = local_buffer[(i + ky) * header.width + n_col];
  154.                             if (p > max_val) max_val = p;
  155.                         }
  156.                     }
  157.                 }
  158.                 int diff = local_data[(i - 1) * header.width + j] - max_val;
  159.                 local_result[(i - 1) * header.width + j] = (diff > 15) ? 255 : local_data[(i - 1) * header.width + j] / 4;
  160.             }
  161.         }
  162.     }
  163.  
  164.     std::vector<unsigned char> final_image;
  165.     if (rank == 0) final_image.resize(header.width * header.height);
  166.  
  167.     MPI_Gatherv(local_result.data(), my_pixels_count, MPI_UNSIGNED_CHAR,
  168.                 final_image.data(), send_counts.data(), displacements.data(), MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD);
  169.  
  170.     if (rank == 0) {
  171.         std::string archivo_salida;
  172.         if (filtro == 1) {
  173.             archivo_salida = "resultado_erosion.pgm";
  174.             writePGM(archivo_salida, header, final_image);
  175.         } else if (filtro == 2) {
  176.             archivo_salida = "resultado_dilatacion.pgm";
  177.             writePGM(archivo_salida, header, final_image);
  178.         } else if (filtro == 3) {
  179.             archivo_salida = "resultado_nodulos.ppm";
  180.             std::vector<unsigned char> rgb_image(header.width * header.height * 3);
  181.             for (int i = 0; i < header.width * header.height; ++i) {
  182.                 if (final_image[i] == 255) {
  183.                     rgb_image[i * 3] = 255; rgb_image[i * 3 + 1] = 0; rgb_image[i * 3 + 2] = 0;
  184.                 } else {
  185.                     rgb_image[i * 3] = final_image[i]; rgb_image[i * 3 + 1] = final_image[i]; rgb_image[i * 3 + 2] = final_image[i];
  186.                 }
  187.             }
  188.             writePPM(archivo_salida, header, rgb_image);
  189.         }
  190.  
  191.         std::cout << "\n[HPC] Procesamiento listo. Creando imagen comparativa integrada..." << std::endl;
  192.        
  193.         // Comando mágico: Une las dos imágenes lado a lado (+append), les da un tamaño escalado estable (x500) y las guarda en un PNG limpio.
  194.         std::string cmd_comparar = "montage -geometry +2+2 -resize x500 mamografia.pgm " + archivo_salida + " comparacion.png && xdg-open comparacion.png &";
  195.         std::system(cmd_comparar.c_str());
  196.     }
  197. }
  198.  
  199. int main(int argc, char** argv) {
  200.     int prop;
  201.     MPI_Init_thread(&argc, &argv, MPI_THREAD_SINGLE, &prop);
  202.     int rank, size;
  203.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  204.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  205.  
  206.     std::string archivo_objetivo = "mamografia.pgm";
  207.  
  208.     if (rank == 0) {
  209.         int opcion_principal = 0;
  210.         do {
  211.             std::cout << "\n=======================================================" << std::endl;
  212.             std::cout << "  SISTEMA DE PROCESAMIENTO PARALELO DE IMAGENES MEDICAS " << std::endl;
  213.             std::cout << "=======================================================" << std::endl;
  214.             std::cout << "1. Cargar/Subir imagen desde carpeta 'Downloads'" << std::endl;
  215.             std::cout << "2. Salir" << std::endl;
  216.             std::cout << "Seleccione una opcion: "; std::cin >> opcion_principal;
  217.  
  218.             MPI_Bcast(&opcion_principal, 1, MPI_INT, 0, MPI_COMM_WORLD);
  219.  
  220.             if (opcion_principal == 1) {
  221.                 std::string nombre_archivo;
  222.                 std::cout << "Ingrese el nombre del archivo en Downloads: ";
  223.                 std::cin >> nombre_archivo;
  224.  
  225.                 std::string comando_mover = "cp ~/Downloads/" + nombre_archivo + " ./" + archivo_objetivo + " 2>/dev/null";
  226.                 std::system(comando_mover.c_str());
  227.  
  228.                 std::ifstream test(archivo_objetivo);
  229.                 if (!test.good()) {
  230.                     std::cout << "Error: No se encontro el archivo." << std::endl;
  231.                     int flag_error = -1; MPI_Bcast(&flag_error, 1, MPI_INT, 0, MPI_COMM_WORLD);
  232.                     continue;
  233.                 }
  234.                 test.close();
  235.                 std::cout << "¡Imagen cargada con exito al sistema!" << std::endl;
  236.  
  237.                 int opcion_filtro = 0;
  238.                 do {
  239.                     std::cout << "\n--- SUBMENU: FILTROS MORFOLOGICOS DISPONIBLES ---" << std::endl;
  240.                     std::cout << "1. Aplicar Erosion Morfologica" << std::endl;
  241.                     std::cout << "2. Aplicar Dilatacion Morfologica" << std::endl;
  242.                     std::cout << "3. Aplicar Deteccion de Nodulos (Marcado en Rojo)" << std::endl;
  243.                     std::cout << "4. Volver al menu principal" << std::endl;
  244.                     std::cout << "Seleccione un filtro: "; std::cin >> opcion_filtro;
  245.  
  246.                     MPI_Bcast(&opcion_filtro, 1, MPI_INT, 0, MPI_COMM_WORLD);
  247.  
  248.                     if (opcion_filtro >= 1 && opcion_filtro <= 3) {
  249.                         procesarFiltro(opcion_filtro, rank, size, archivo_objetivo);
  250.                     }
  251.                 } while (opcion_filtro != 4);
  252.             }
  253.         } while (opcion_principal != 2);
  254.     } else {
  255.         int opcion_principal = 0;
  256.         do {
  257.             MPI_Bcast(&opcion_principal, 1, MPI_INT, 0, MPI_COMM_WORLD);
  258.             if (opcion_principal == 1) {
  259.                 int opcion_filtro = 0;
  260.                 do {
  261.                     MPI_Bcast(&opcion_filtro, 1, MPI_INT, 0, MPI_COMM_WORLD);
  262.                     if (opcion_filtro == -1) break;
  263.                     if (opcion_filtro >= 1 && opcion_filtro <= 3) {
  264.                         procesarFiltro(opcion_filtro, rank, size, archivo_objetivo);
  265.                     }
  266.                 } while (opcion_filtro != 4);
  267.             }
  268.         } while (opcion_principal != 2);
  269.     }
  270.  
  271.     MPI_Finalize();
  272.     return 0;
  273. }
Add Comment
Please, Sign In to add comment