Ver Mensaje Individual
  #4 (permalink)  
Antiguo 11/01/2013, 02:10
reethok
 
Fecha de Ingreso: abril-2011
Mensajes: 224
Antigüedad: 13 años
Puntos: 8
Respuesta: ejercicio de c++ con arrays

Veo que ya te respondieron.

Bueno, sólo me gustaría agregar que usé tu ejercicio (adaptado a mis necesidades) para trabajar con pixeles, te dejo el código por si quieres revisarlo. Requiere que haya una imagen llamada "image.png" en la misma carpeta, y crea una imagen llamada "resultado.png", dónde se resaltan las células madres en blanco y el resto se pone de negro.

Código C++:
Ver original
  1. #include <SFML/Graphics.hpp>
  2.  
  3. #include <string>
  4. using std::string;
  5.  
  6. sf::Image &cargarImagen(const string &filename)
  7. {
  8.     sf::Image img;
  9.     img.LoadFromFile(filename);
  10.     return img;
  11. }
  12.  
  13. void guardarImagen(const sf::Image &imagen, const string &filename)
  14. {
  15.     imagen.SaveToFile(filename);
  16. }
  17.  
  18.  
  19. void procesarImagen(const sf::Image &imagen)
  20. {
  21.     sf::Image resultado;
  22.     sf::Color blanco(255, 255, 255);
  23.     int x = imagen.GetWidth();
  24.     int y = imagen.GetHeight();
  25.     short arreglo[2000][2000] = {0};
  26.  
  27.     resultado.Create(imagen.GetWidth(), imagen.GetHeight(), sf::Color(0, 0, 0, 255));
  28.  
  29.     for(int i = 1; i < x-1; i++)
  30.     {
  31.         for(int j = 1; j < y-1; j++)
  32.         {
  33.             if(arreglo[i][j] == 0 && arreglo[i][j+1] == 0 && arreglo[i+1][j] == 0
  34.                 && arreglo[i+1][j+1] == 0)
  35.             {
  36.                 sf::Color a1 = imagen.GetPixel(i, j);
  37.                 sf::Color a2 = imagen.GetPixel(i, j+1);
  38.                 sf::Color a3 = imagen.GetPixel(i+1, j);
  39.                 sf::Color a4 = imagen.GetPixel(i+1, j+1);
  40.  
  41.                     // saturación de colores,
  42.                     // Negro = 0, 0, 0, por ende,
  43.                     // 240 de tu ejercicio = 15 en pixeles
  44.                     // y 15 * 3 colores * 4 pixeles = 180
  45.                 if((a1.r + a1.g + a1.b + a2.r + a2.g + a2.b +
  46.                     a3.r + a3.g + a3.b + a4.r + a4.g + a4.b <= 180))
  47.  
  48.                 {
  49.                     arreglo[i][j] = arreglo[i][j+1] = arreglo[i+1][j] = arreglo[i+1][j+1] = 1;
  50.                     resultado.SetPixel(i, j, blanco);
  51.                     resultado.SetPixel(i, j+1, blanco);
  52.                     resultado.SetPixel(i+1, j, blanco);
  53.                     resultado.SetPixel(i+1, j+1, blanco);
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     resultado.SaveToFile("resultado.png");
  60. }
  61.  
  62. int main()
  63. {
  64.     sf::Image imagen;
  65.  
  66.     imagen.LoadFromFile("image.png");
  67.     procesarImagen(imagen);
  68.     return 0;
  69. }