Advertisement
Guest User

Algol

a guest
May 12th, 2012
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.88 KB | None | 0 0
  1. -----ALGOL.HPP------
  2. #ifndef ALGOL_HPP
  3. #define ALGOL_HPP
  4.  
  5. #include <cstdlib>
  6. #include <list>
  7. #include <mln/core/image/image2d.hh>
  8. #include <mln/morpho/all.hh>
  9. #include <mln/value/all.hh>
  10. //#include <tests/data.hh>
  11. //#include <doc/tools/sample_utils.hh>
  12.  
  13. #define erossion -1     //used for erossion
  14. #define dilation 1      //used for dilation
  15.  
  16. #define vertical 2   //v-way connectivity
  17. #define horizontal 3 //h-way connectivity
  18. #define star 4       //4-way connectivity
  19. #define box 8        //8-way connectivity
  20.  
  21. #define reg_min -1
  22. #define reg_max 1
  23.  
  24. int comparator(mln::value::int_u8 a, mln::value::int_u8 b);
  25. mln::value::int_u8 four_way(const mln::image2d<mln::value::int_u8> *img, int op , int size, int row, int col);
  26. mln::value::int_u8 eight_way(const mln::image2d<mln::value::int_u8> *img, int op , int size, int row, int col);
  27. mln::value::int_u8 nway_operator(const mln::image2d<mln::value::int_u8> *img, int op, int n_way, int size, int row, int col);
  28.  
  29. mln::image2d<mln::value::int_u8> *imLoad(const char *path);
  30. void imSave(const char *path, const mln::image2d<mln::value::int_u8> *img);
  31. mln::image2d<mln::value::int_u8> imErossion(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
  32. mln::image2d<mln::value::int_u8> imDilation(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
  33. mln::image2d<mln::value::int_u8> imOpening(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
  34. mln::image2d<mln::value::int_u8> imClosing(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
  35. mln::image2d<mln::value::int_u8> imOpeningClosing(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
  36. mln::image2d<mln::value::int_u8> imClosingOpening(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
  37.  
  38. //Ejercicio 1
  39. mln::value::int_u8 imMaxValue (const mln::image2d<mln::value::int_u8> *img); /* devuelve el valor m·ximo de imageIn */
  40. mln::value::int_u8 imMinValue (const mln::image2d<mln::value::int_u8> *img); /* devuelve el valor mÌnimo de imageIn */
  41. #endif
  42.  
  43. -----ALGOL.CPP------
  44.  
  45. #include "algol.hpp"
  46.  
  47. //#define WORKING_DIR "./src/"
  48.  
  49. int comparator(const mln::value::int_u8 a, const mln::value::int_u8 b){
  50.     return a == b ? 0 : a < b ? -1 : 1;
  51. }
  52.  
  53. mln::value::int_u8 h_way(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
  54.         const int row, const int col){
  55.  
  56.     mln::value::int_u8 result,act;
  57.  
  58.     result = mln::opt::at(*img,row,col);
  59.  
  60.     for (int i = 1; i <= size; i++) {   //TODO maybe -size to size? then if col+i is enough
  61.  
  62.         if(col-i >= mln::geom::min_col(*img)){
  63.             act = mln::opt::at(*img,row,col-i);
  64.             result = (op*comparator(result,act)) == 1 ? result : act;
  65.         }
  66.  
  67.         if(col+i <= mln::geom::max_col(*img)){
  68.             act = mln::opt::at(*img,row,col+i);
  69.             result = (op*comparator(result,act)) == 1 ? result : act;
  70.         }
  71.  
  72.     }
  73.  
  74.     return result;
  75.  
  76. }
  77.  
  78. mln::value::int_u8 v_way(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
  79.         const int row, const int col){
  80.  
  81.     mln::value::int_u8 result,act;
  82.  
  83.     result = mln::opt::at(*img,row,col);
  84.  
  85.     for (int i = 1; i <= size; i++) {   //TODO maybe -size to size? then if row+i is enough
  86.  
  87.         if(row-i >= mln::geom::min_row(*img)){
  88.             act = mln::opt::at(*img,row-i,col);
  89.             result = (op*comparator(result,act)) == 1 ? result : act;
  90.         }
  91.  
  92.         if(row+i <= mln::geom::max_row(*img)){
  93.             act = mln::opt::at(*img,row+i,col);
  94.             result = (op*comparator(result,act)) == 1 ? result : act;
  95.         }
  96.  
  97.     }
  98.  
  99.     return result;
  100.  
  101. }
  102.  
  103. mln::value::int_u8 four_way(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
  104.         const int row, const int col){
  105.  
  106.     mln::value::int_u8 result1 = v_way(img,op,size,row,col), result2 = h_way(img,op,size,row,col);
  107.     return (op*comparator(result1,result2)) == 1 ? result1 : result2;
  108. }
  109.  
  110.  
  111. mln::value::int_u8 eight_way(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
  112.         const int row, const int col){
  113.  
  114.     mln::value::int_u8 result,act;
  115.     result = (op == erossion) ? 255 : 0;
  116.  
  117.     for(int i=-size;i<=size;i++){
  118.         if(row+i >= mln::geom::min_row(*img) && row+i <= mln::geom::max_row(*img)){
  119.             act = h_way(img,op,size,row+i,col);
  120.             result = (op*comparator(result,act)) == 1 ? result : act;
  121.         }
  122.     }
  123.  
  124.     return result;
  125. }
  126.  
  127. /*
  128.  * nway_operator: apply an operator on an structuring element
  129.  * @param int op -- select operator 0=min 1=max
  130.  * @param int size -- select size
  131.  * @param int n_way -- select connectivity star,box,h_way,v_way
  132.  * @param int row -- actual pixel row
  133.  * @param int col -- actual pixel col
  134.  */
  135.  
  136. mln::value::int_u8 nway_operator(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
  137.         const int n_way, const int row, const int col){
  138.  
  139.     if (n_way==star)
  140.         return four_way(img, op, size, row, col);
  141.  
  142.     if(n_way==box)
  143.         return eight_way(img, op, size, row, col);
  144.  
  145.     if(n_way==vertical)
  146.         return v_way(img, op, size, row, col);
  147.  
  148.     if(n_way==horizontal)
  149.         return h_way(img, op, size, row, col);
  150.  
  151.     std :: cerr << "Operación no válida" << std :: endl;
  152.     return -1;
  153.  
  154. }
  155.  
  156. mln::image2d<mln::value::int_u8> imErossionSquare3x3(const mln::image2d<mln::value::int_u8> *img){
  157.     return imErossion(img,3,box);
  158. }
  159.  
  160. mln::image2d<mln::value::int_u8> imDilationSquare3x3(const mln::image2d<mln::value::int_u8> *img){
  161.     return imDilation(img,3,box);
  162. }
  163.  
  164. mln::image2d<mln::value::int_u8> imErossion(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
  165.  
  166.     mln::image2d<mln::value::int_u8> out(img->domain());
  167.     mln::data::fill(out,177);
  168.  
  169.     for (mln::def::coord row = mln::geom::min_row(*img); row <= mln::geom::max_row(*img); ++row)
  170.         for (mln::def::coord col = mln::geom::min_col(*img); col <= mln::geom::max_col(*img);++col)
  171.             out(mln::point2d(row,col)) = (mln::value::int_u8) nway_operator(img,erossion,size,nbh,row,col);
  172.  
  173.     return out;
  174. }
  175.  
  176. mln::image2d<mln::value::int_u8> imDilation(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
  177.  
  178.     mln::image2d<mln::value::int_u8> out(img->domain());
  179.     mln::data::fill(out,177);
  180.  
  181.     for (mln::def::coord row = mln::geom::min_row(*img); row <= mln::geom::max_row(*img); ++row)
  182.         for (mln::def::coord col = mln::geom::min_col(*img); col <= mln::geom::max_col(*img);++col)
  183.             out(mln::point2d(row,col)) = (mln::value::int_u8) nway_operator(img,dilation,size,nbh,row,col);
  184.  
  185.     return out;
  186.  
  187. }
  188.  
  189. mln::image2d<mln::value::int_u8> imOpening(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
  190.     mln::image2d<mln::value::int_u8> tmp = imErossion(img, size, nbh);
  191.     return imDilation(&tmp, size, nbh);
  192. }
  193.  
  194. mln::image2d<mln::value::int_u8> imClosing(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
  195.     mln::image2d<mln::value::int_u8> tmp = imDilation(img, size, nbh);
  196.     return imErossion(&tmp, size, nbh);
  197. }
  198.  
  199. mln::image2d<mln::value::int_u8> imOpeningClosing(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
  200.     mln::image2d<mln::value::int_u8> tmp = imClosing(img, size, nbh);
  201.     return imOpening(&tmp, size, nbh);
  202. }
  203.  
  204. mln::image2d<mln::value::int_u8> imClosingOpening(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
  205.     mln::image2d<mln::value::int_u8> tmp = imOpening(img, size, nbh);
  206.     return imClosing(&tmp, size, nbh);
  207. }
  208.  
  209. mln::value::int_u8 imMaxValue (const mln::image2d<mln::value::int_u8> *img){
  210.  
  211.     mln::value::int_u8 result = (mln::value::int_u8) 0;
  212.  
  213.     for (mln::def::coord row = mln::geom::min_row(*img); row <= mln::geom::max_row(*img); ++row)
  214.         for (mln::def::coord col = mln::geom::min_col(*img); col <= mln::geom::max_col(*img);++col)
  215.             result = (result < mln::opt::at(*img,row,col)) ?  mln::opt::at(*img,row,col) : result;
  216.  
  217.     return result;
  218.  
  219. }
  220.  
  221. mln::value::int_u8 imMinValue (const mln::image2d<mln::value::int_u8> *img){
  222.  
  223.     mln::value::int_u8 result = (mln::value::int_u8) 255;
  224.  
  225.     for (mln::def::coord row = mln::geom::min_row(*img); row <= mln::geom::max_row(*img); ++row)
  226.         for (mln::def::coord col = mln::geom::min_col(*img); col <= mln::geom::max_col(*img);++col)
  227.             result = (result > mln::opt::at(*img,row,col)) ?  mln::opt::at(*img,row,col) : result;
  228.  
  229.     return result;
  230.  
  231. }
  232.  
  233. ---- SAMPLE MAIN ----
  234.  
  235.  
  236.     mln::image2d<mln::value::int_u8> img, out, cmp1;
  237.     mln::io::pgm::load(img, WORKING_DIR "immed_gray_inv.pgm");
  238.  
  239.     out = imErossion(&img, 1, box);
  240.     mln::io::pgm::save(out, WORKING_DIR "erossion1.pgm");
  241.     mln::io::pgm::load(cmp1, WORKING_DIR "immed_gray_inv_20051123_ero1.pgm");
  242.     std :: cout << "imAreEqual(&cmp1,&Erossion(&img, 1, box)) " << imAreEqual(&cmp1,&out) << std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement