Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- #include <algorithm>
- #include <cstdlib>
- struct PGMHeader {
- int width = 0; int height = 0; int max_val = 0;
- };
- bool readPGM(const std::string& filename, PGMHeader& header, std::vector<unsigned char>& data) {
- std::ifstream file(filename, std::ios::binary);
- if (!file.is_open()) return false;
- std::string type; file >> type;
- if (type != "P5") return false;
- char ch; file >> ch;
- while (ch == '#') { std::string comment; std::getline(file, comment); file >> ch; }
- file.putback(ch);
- file >> header.width >> header.height >> header.max_val;
- file.ignore();
- data.resize(header.width * header.height);
- file.read(reinterpret_cast<char*>(data.data()), data.size());
- return file.good();
- }
- bool writePGM(const std::string& filename, const PGMHeader& header, const std::vector<unsigned char>& data) {
- std::ofstream file(filename, std::ios::binary);
- if (!file.is_open()) return false;
- file << "P5\n" << header.width << " " << header.height << "\n" << header.max_val << "\n";
- file.write(reinterpret_cast<const char*>(data.data()), data.size());
- return file.good();
- }
- bool writePPM(const std::string& filename, const PGMHeader& header, const std::vector<unsigned char>& rgb_data) {
- std::ofstream file(filename, std::ios::binary);
- if (!file.is_open()) return false;
- file << "P6\n" << header.width << " " << header.height << "\n255\n";
- file.write(reinterpret_cast<const char*>(rgb_data.data()), rgb_data.size());
- return file.good();
- }
- void procesarFiltro(int filtro, int rank, int size, const std::string& nombre_archivo) {
- PGMHeader header;
- std::vector<unsigned char> full_image;
- std::vector<int> send_counts(size, 0), displacements(size, 0);
- if (rank == 0) {
- readPGM(nombre_archivo, header, full_image);
- int base_rows = header.height / size;
- int remainder = header.height % size;
- int current_disp = 0;
- for (int i = 0; i < size; ++i) {
- int rows_for_proc = base_rows + (i < remainder ? 1 : 0);
- send_counts[i] = rows_for_proc * header.width;
- displacements[i] = current_disp;
- current_disp += send_counts[i];
- }
- }
- MPI_Bcast(&header.width, 1, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_Bcast(&header.height, 1, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_Bcast(&header.max_val, 1, MPI_INT, 0, MPI_COMM_WORLD);
- int base_rows = header.height / size;
- int remainder = header.height % size;
- int my_rows = base_rows + (rank < remainder ? 1 : 0);
- int my_pixels_count = my_rows * header.width;
- std::vector<unsigned char> local_data(my_pixels_count);
- MPI_Scatterv(full_image.data(), send_counts.data(), displacements.data(), MPI_UNSIGNED_CHAR,
- local_data.data(), my_pixels_count, MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD);
- std::vector<unsigned char> local_buffer((my_rows + 2) * header.width, 0);
- std::copy(local_data.begin(), local_data.end(), local_buffer.begin() + header.width);
- int vec_sup = (rank == 0) ? MPI_PROC_NULL : rank - 1;
- int vec_inf = (rank == size - 1) ? MPI_PROC_NULL : rank + 1;
- 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);
- 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);
- if (rank == 0) std::copy(local_buffer.begin() + header.width, local_buffer.begin() + 2 * header.width, local_buffer.begin());
- 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);
- std::vector<unsigned char> local_result(my_pixels_count, 0);
- if (filtro == 1) { // EROSIÓN
- for (int i = 1; i <= my_rows; ++i) {
- for (int j = 0; j < header.width; ++j) {
- unsigned char min_val = 255;
- for (int ky = -1; ky <= 1; ++ky) {
- for (int kx = -1; kx <= 1; ++kx) {
- int n_col = j + kx;
- if (n_col >= 0 && n_col < header.width) {
- unsigned char p = local_buffer[(i + ky) * header.width + n_col];
- if (p < min_val) min_val = p;
- }
- }
- }
- local_result[(i - 1) * header.width + j] = min_val;
- }
- }
- }
- else if (filtro == 2) { // DILATACIÓN
- for (int i = 1; i <= my_rows; ++i) {
- for (int j = 0; j < header.width; ++j) {
- unsigned char max_val = 0;
- for (int ky = -1; ky <= 1; ++ky) {
- for (int kx = -1; kx <= 1; ++kx) {
- int n_col = j + kx;
- if (n_col >= 0 && n_col < header.width) {
- unsigned char p = local_buffer[(i + ky) * header.width + n_col];
- if (p > max_val) max_val = p;
- }
- }
- }
- local_result[(i - 1) * header.width + j] = max_val;
- }
- }
- }
- else if (filtro == 3) { // DETECCIÓN DE NÓDULOS
- std::vector<unsigned char> eroded(my_pixels_count, 255);
- for (int i = 1; i <= my_rows; ++i) {
- for (int j = 0; j < header.width; ++j) {
- unsigned char min_val = 255;
- for (int ky = -1; ky <= 1; ++ky) {
- for (int kx = -1; kx <= 1; ++kx) {
- int n_col = j + kx;
- if (n_col >= 0 && n_col < header.width) {
- unsigned char p = local_buffer[(i + ky) * header.width + n_col];
- if (p < min_val) min_val = p;
- }
- }
- }
- eroded[(i - 1) * header.width + j] = min_val;
- }
- }
- std::copy(eroded.begin(), eroded.end(), local_buffer.begin() + header.width);
- 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);
- 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);
- for (int i = 1; i <= my_rows; ++i) {
- for (int j = 0; j < header.width; ++j) {
- unsigned char max_val = 0;
- for (int ky = -1; ky <= 1; ++ky) {
- for (int kx = -1; kx <= 1; ++kx) {
- int n_col = j + kx;
- if (n_col >= 0 && n_col < header.width) {
- unsigned char p = local_buffer[(i + ky) * header.width + n_col];
- if (p > max_val) max_val = p;
- }
- }
- }
- int diff = local_data[(i - 1) * header.width + j] - max_val;
- local_result[(i - 1) * header.width + j] = (diff > 15) ? 255 : local_data[(i - 1) * header.width + j] / 4;
- }
- }
- }
- std::vector<unsigned char> final_image;
- if (rank == 0) final_image.resize(header.width * header.height);
- MPI_Gatherv(local_result.data(), my_pixels_count, MPI_UNSIGNED_CHAR,
- final_image.data(), send_counts.data(), displacements.data(), MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD);
- if (rank == 0) {
- std::string archivo_salida;
- if (filtro == 1) {
- archivo_salida = "resultado_erosion.pgm";
- writePGM(archivo_salida, header, final_image);
- } else if (filtro == 2) {
- archivo_salida = "resultado_dilatacion.pgm";
- writePGM(archivo_salida, header, final_image);
- } else if (filtro == 3) {
- archivo_salida = "resultado_nodulos.ppm";
- std::vector<unsigned char> rgb_image(header.width * header.height * 3);
- for (int i = 0; i < header.width * header.height; ++i) {
- if (final_image[i] == 255) {
- rgb_image[i * 3] = 255; rgb_image[i * 3 + 1] = 0; rgb_image[i * 3 + 2] = 0;
- } else {
- rgb_image[i * 3] = final_image[i]; rgb_image[i * 3 + 1] = final_image[i]; rgb_image[i * 3 + 2] = final_image[i];
- }
- }
- writePPM(archivo_salida, header, rgb_image);
- }
- std::cout << "\n[HPC] Procesamiento listo. Creando imagen comparativa integrada..." << std::endl;
- // 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.
- std::string cmd_comparar = "montage -geometry +2+2 -resize x500 mamografia.pgm " + archivo_salida + " comparacion.png && xdg-open comparacion.png &";
- std::system(cmd_comparar.c_str());
- }
- }
- int main(int argc, char** argv) {
- int prop;
- MPI_Init_thread(&argc, &argv, MPI_THREAD_SINGLE, &prop);
- int rank, size;
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- std::string archivo_objetivo = "mamografia.pgm";
- if (rank == 0) {
- int opcion_principal = 0;
- do {
- std::cout << "\n=======================================================" << std::endl;
- std::cout << " SISTEMA DE PROCESAMIENTO PARALELO DE IMAGENES MEDICAS " << std::endl;
- std::cout << "=======================================================" << std::endl;
- std::cout << "1. Cargar/Subir imagen desde carpeta 'Downloads'" << std::endl;
- std::cout << "2. Salir" << std::endl;
- std::cout << "Seleccione una opcion: "; std::cin >> opcion_principal;
- MPI_Bcast(&opcion_principal, 1, MPI_INT, 0, MPI_COMM_WORLD);
- if (opcion_principal == 1) {
- std::string nombre_archivo;
- std::cout << "Ingrese el nombre del archivo en Downloads: ";
- std::cin >> nombre_archivo;
- std::string comando_mover = "cp ~/Downloads/" + nombre_archivo + " ./" + archivo_objetivo + " 2>/dev/null";
- std::system(comando_mover.c_str());
- std::ifstream test(archivo_objetivo);
- if (!test.good()) {
- std::cout << "Error: No se encontro el archivo." << std::endl;
- int flag_error = -1; MPI_Bcast(&flag_error, 1, MPI_INT, 0, MPI_COMM_WORLD);
- continue;
- }
- test.close();
- std::cout << "¡Imagen cargada con exito al sistema!" << std::endl;
- int opcion_filtro = 0;
- do {
- std::cout << "\n--- SUBMENU: FILTROS MORFOLOGICOS DISPONIBLES ---" << std::endl;
- std::cout << "1. Aplicar Erosion Morfologica" << std::endl;
- std::cout << "2. Aplicar Dilatacion Morfologica" << std::endl;
- std::cout << "3. Aplicar Deteccion de Nodulos (Marcado en Rojo)" << std::endl;
- std::cout << "4. Volver al menu principal" << std::endl;
- std::cout << "Seleccione un filtro: "; std::cin >> opcion_filtro;
- MPI_Bcast(&opcion_filtro, 1, MPI_INT, 0, MPI_COMM_WORLD);
- if (opcion_filtro >= 1 && opcion_filtro <= 3) {
- procesarFiltro(opcion_filtro, rank, size, archivo_objetivo);
- }
- } while (opcion_filtro != 4);
- }
- } while (opcion_principal != 2);
- } else {
- int opcion_principal = 0;
- do {
- MPI_Bcast(&opcion_principal, 1, MPI_INT, 0, MPI_COMM_WORLD);
- if (opcion_principal == 1) {
- int opcion_filtro = 0;
- do {
- MPI_Bcast(&opcion_filtro, 1, MPI_INT, 0, MPI_COMM_WORLD);
- if (opcion_filtro == -1) break;
- if (opcion_filtro >= 1 && opcion_filtro <= 3) {
- procesarFiltro(opcion_filtro, rank, size, archivo_objetivo);
- }
- } while (opcion_filtro != 4);
- }
- } while (opcion_principal != 2);
- }
- MPI_Finalize();
- return 0;
- }
Add Comment
Please, Sign In to add comment