Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----ALGOL.HPP------
- #ifndef ALGOL_HPP
- #define ALGOL_HPP
- #include <cstdlib>
- #include <list>
- #include <mln/core/image/image2d.hh>
- #include <mln/morpho/all.hh>
- #include <mln/value/all.hh>
- //#include <tests/data.hh>
- //#include <doc/tools/sample_utils.hh>
- #define erossion -1 //used for erossion
- #define dilation 1 //used for dilation
- #define vertical 2 //v-way connectivity
- #define horizontal 3 //h-way connectivity
- #define star 4 //4-way connectivity
- #define box 8 //8-way connectivity
- #define reg_min -1
- #define reg_max 1
- int comparator(mln::value::int_u8 a, mln::value::int_u8 b);
- mln::value::int_u8 four_way(const mln::image2d<mln::value::int_u8> *img, int op , int size, int row, int col);
- mln::value::int_u8 eight_way(const mln::image2d<mln::value::int_u8> *img, int op , int size, int row, int col);
- 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);
- mln::image2d<mln::value::int_u8> *imLoad(const char *path);
- void imSave(const char *path, const mln::image2d<mln::value::int_u8> *img);
- mln::image2d<mln::value::int_u8> imErossion(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
- mln::image2d<mln::value::int_u8> imDilation(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
- mln::image2d<mln::value::int_u8> imOpening(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
- mln::image2d<mln::value::int_u8> imClosing(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
- mln::image2d<mln::value::int_u8> imOpeningClosing(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
- mln::image2d<mln::value::int_u8> imClosingOpening(const mln::image2d<mln::value::int_u8> *img, int size, int nbh);
- //Ejercicio 1
- mln::value::int_u8 imMaxValue (const mln::image2d<mln::value::int_u8> *img); /* devuelve el valor m·ximo de imageIn */
- mln::value::int_u8 imMinValue (const mln::image2d<mln::value::int_u8> *img); /* devuelve el valor mÌnimo de imageIn */
- #endif
- -----ALGOL.CPP------
- #include "algol.hpp"
- //#define WORKING_DIR "./src/"
- int comparator(const mln::value::int_u8 a, const mln::value::int_u8 b){
- return a == b ? 0 : a < b ? -1 : 1;
- }
- mln::value::int_u8 h_way(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
- const int row, const int col){
- mln::value::int_u8 result,act;
- result = mln::opt::at(*img,row,col);
- for (int i = 1; i <= size; i++) { //TODO maybe -size to size? then if col+i is enough
- if(col-i >= mln::geom::min_col(*img)){
- act = mln::opt::at(*img,row,col-i);
- result = (op*comparator(result,act)) == 1 ? result : act;
- }
- if(col+i <= mln::geom::max_col(*img)){
- act = mln::opt::at(*img,row,col+i);
- result = (op*comparator(result,act)) == 1 ? result : act;
- }
- }
- return result;
- }
- mln::value::int_u8 v_way(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
- const int row, const int col){
- mln::value::int_u8 result,act;
- result = mln::opt::at(*img,row,col);
- for (int i = 1; i <= size; i++) { //TODO maybe -size to size? then if row+i is enough
- if(row-i >= mln::geom::min_row(*img)){
- act = mln::opt::at(*img,row-i,col);
- result = (op*comparator(result,act)) == 1 ? result : act;
- }
- if(row+i <= mln::geom::max_row(*img)){
- act = mln::opt::at(*img,row+i,col);
- result = (op*comparator(result,act)) == 1 ? result : act;
- }
- }
- return result;
- }
- mln::value::int_u8 four_way(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
- const int row, const int col){
- mln::value::int_u8 result1 = v_way(img,op,size,row,col), result2 = h_way(img,op,size,row,col);
- return (op*comparator(result1,result2)) == 1 ? result1 : result2;
- }
- mln::value::int_u8 eight_way(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
- const int row, const int col){
- mln::value::int_u8 result,act;
- result = (op == erossion) ? 255 : 0;
- for(int i=-size;i<=size;i++){
- if(row+i >= mln::geom::min_row(*img) && row+i <= mln::geom::max_row(*img)){
- act = h_way(img,op,size,row+i,col);
- result = (op*comparator(result,act)) == 1 ? result : act;
- }
- }
- return result;
- }
- /*
- * nway_operator: apply an operator on an structuring element
- * @param int op -- select operator 0=min 1=max
- * @param int size -- select size
- * @param int n_way -- select connectivity star,box,h_way,v_way
- * @param int row -- actual pixel row
- * @param int col -- actual pixel col
- */
- mln::value::int_u8 nway_operator(const mln::image2d<mln::value::int_u8> *img, const int op, const int size,
- const int n_way, const int row, const int col){
- if (n_way==star)
- return four_way(img, op, size, row, col);
- if(n_way==box)
- return eight_way(img, op, size, row, col);
- if(n_way==vertical)
- return v_way(img, op, size, row, col);
- if(n_way==horizontal)
- return h_way(img, op, size, row, col);
- std :: cerr << "Operación no válida" << std :: endl;
- return -1;
- }
- mln::image2d<mln::value::int_u8> imErossionSquare3x3(const mln::image2d<mln::value::int_u8> *img){
- return imErossion(img,3,box);
- }
- mln::image2d<mln::value::int_u8> imDilationSquare3x3(const mln::image2d<mln::value::int_u8> *img){
- return imDilation(img,3,box);
- }
- mln::image2d<mln::value::int_u8> imErossion(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
- mln::image2d<mln::value::int_u8> out(img->domain());
- mln::data::fill(out,177);
- for (mln::def::coord row = mln::geom::min_row(*img); row <= mln::geom::max_row(*img); ++row)
- for (mln::def::coord col = mln::geom::min_col(*img); col <= mln::geom::max_col(*img);++col)
- out(mln::point2d(row,col)) = (mln::value::int_u8) nway_operator(img,erossion,size,nbh,row,col);
- return out;
- }
- mln::image2d<mln::value::int_u8> imDilation(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
- mln::image2d<mln::value::int_u8> out(img->domain());
- mln::data::fill(out,177);
- for (mln::def::coord row = mln::geom::min_row(*img); row <= mln::geom::max_row(*img); ++row)
- for (mln::def::coord col = mln::geom::min_col(*img); col <= mln::geom::max_col(*img);++col)
- out(mln::point2d(row,col)) = (mln::value::int_u8) nway_operator(img,dilation,size,nbh,row,col);
- return out;
- }
- mln::image2d<mln::value::int_u8> imOpening(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
- mln::image2d<mln::value::int_u8> tmp = imErossion(img, size, nbh);
- return imDilation(&tmp, size, nbh);
- }
- mln::image2d<mln::value::int_u8> imClosing(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
- mln::image2d<mln::value::int_u8> tmp = imDilation(img, size, nbh);
- return imErossion(&tmp, size, nbh);
- }
- mln::image2d<mln::value::int_u8> imOpeningClosing(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
- mln::image2d<mln::value::int_u8> tmp = imClosing(img, size, nbh);
- return imOpening(&tmp, size, nbh);
- }
- mln::image2d<mln::value::int_u8> imClosingOpening(const mln::image2d<mln::value::int_u8> *img, const int size, const int nbh){
- mln::image2d<mln::value::int_u8> tmp = imOpening(img, size, nbh);
- return imClosing(&tmp, size, nbh);
- }
- mln::value::int_u8 imMaxValue (const mln::image2d<mln::value::int_u8> *img){
- mln::value::int_u8 result = (mln::value::int_u8) 0;
- for (mln::def::coord row = mln::geom::min_row(*img); row <= mln::geom::max_row(*img); ++row)
- for (mln::def::coord col = mln::geom::min_col(*img); col <= mln::geom::max_col(*img);++col)
- result = (result < mln::opt::at(*img,row,col)) ? mln::opt::at(*img,row,col) : result;
- return result;
- }
- mln::value::int_u8 imMinValue (const mln::image2d<mln::value::int_u8> *img){
- mln::value::int_u8 result = (mln::value::int_u8) 255;
- for (mln::def::coord row = mln::geom::min_row(*img); row <= mln::geom::max_row(*img); ++row)
- for (mln::def::coord col = mln::geom::min_col(*img); col <= mln::geom::max_col(*img);++col)
- result = (result > mln::opt::at(*img,row,col)) ? mln::opt::at(*img,row,col) : result;
- return result;
- }
- ---- SAMPLE MAIN ----
- mln::image2d<mln::value::int_u8> img, out, cmp1;
- mln::io::pgm::load(img, WORKING_DIR "immed_gray_inv.pgm");
- out = imErossion(&img, 1, box);
- mln::io::pgm::save(out, WORKING_DIR "erossion1.pgm");
- mln::io::pgm::load(cmp1, WORKING_DIR "immed_gray_inv_20051123_ero1.pgm");
- std :: cout << "imAreEqual(&cmp1,&Erossion(&img, 1, box)) " << imAreEqual(&cmp1,&out) << std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement