Skip to content
Snippets Groups Projects
cosmos_mask_test.cpp 4.27 KiB
Newer Older
/*
 * Copyright © 2012, Ignacio Ramírez Paulino <nacho@fing.edu.uy>
 * All rights reserved.
 *
 * This file is part of COSMOS.
 *
 * COSMOS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * COSMOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with COSMOS.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * \file cosmos_mask_test.cpp
 * Test masked patch extraction routine
 */
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include "cosmos_image_io.h"   // image reading/writing
#include "cosmos_matrix_io.h"  // load the dictionary
#include "cosmos_image.h"
#include "cosmos_command_line.h"
#include "cosmos_debug.h"
#include "cosmos_display.h"
#include "cosmos_stats.h"
#include <ctime>

typedef double real_t; ///< type used to represent real values
/**
 * Coordinate Descent based coding test
 */

int main ( int argc, const char* argv[] ) {
    std::ofstream out;
    int debug = cosmos_get_debug_level();

    //
    //--------------------------------------------------
    // parse arguments:
    //--------------------------------------------------
    //
    char const* image = "../../data/img/barbara.pgm";
    int w = 32;
    //
    // training images directory
    //
    cosmos_cli_params cli_params ( 20 );
    cli_params.add ( "image",parse_string,&image );
    cli_params.add ( "w",parse_int,&w );
    cli_params.add ( "debug",parse_int,&debug );
    if ( !cli_params.parse ( argc,argv ) ) {
        exit ( 1 );
    }
    //
    // random number generator
    //
    cosmos_init_rng ( RNG_DEFAULT_SEED );
    cosmos_set_debug_level ( debug );

    int w2 = w/2;
    //
    // load image
    //
    sc_matrix<real_t> I;
    sc_matrix<real_t> I2;
    cosmos_pgm_read ( image,I );
    std::cout << "mi=" << I.m << " ni=" << I.n << std::endl;
    //
    // decompose
    //
    cosmos_grid G;
    sc_matrix<real_t> X;
    cosmos_uniform_grid ( w2, I.n-w2, w2, I.m-w2, w, w, G );
    //
    // original get_patches function, no fancy effect
    //
    std::cout << "Original get_patches." << std::endl;
    cosmos_get_patches ( I, w, G, X );
    cosmos_remove_dc ( X );
    I2.free();
    cosmos_dict_display ( X,I2,3,1 );
    cosmos_write_ascii_matrix ( X,"X.ascii" );
    cosmos_pgm_write ( I2,"X.pgm" );
    //
    // extract with Gaussian Mask
    //
    std::cout << "Gaussian mask in get_patches_advanced" << std::endl;
    sc_matrix<real_t> W;
    sc_matrix<short> B;
    B.n = B.m = 0;
    cosmos_gaussian_mask ( w, double ( w ) /2.0, W );
    cosmos_write_ascii_matrix ( W,"W.ascii" );
    cosmos_get_patches_advanced ( I, w, G, W, B, X );
    cosmos_remove_dc ( X );
    cosmos_dict_display ( X,I2,3,1 );
    cosmos_write_ascii_matrix ( X,"X_gauss.ascii" );
    cosmos_pgm_write ( I2,"X_gauss.pgm" );
    //
    // disk + weights
    //
    cosmos_disk_mask ( w,B );
    cosmos_write_ascii_matrix ( B,"B.ascii" );
    cosmos_get_patches_advanced ( I, w, G, W, B, X );
    cosmos_remove_dc ( X );
    cosmos_write_ascii_matrix ( X,"X_gauss_disk.ascii" );
    cosmos_dict_display ( X,B,I2,3,1 );
    cosmos_pgm_write ( I2,"X_gauss_disk.pgm" );
    //
    // laplacian of I, disk + weioghts
    //
    cosmos_laplacian ( I,I2 );
    cosmos_get_patches_advanced ( I2, w, G, W, B, X );
    cosmos_remove_dc ( X );
    cosmos_write_ascii_matrix ( X,"X_lap_gauss_disk.ascii" );
    cosmos_dict_display ( X,B,I2,3,1 );
    cosmos_pgm_write ( I2,"X_lap_gauss_disk.pgm" );
    //
    // extract disk shaped patches
    //
    sc_scale ( 255.0,W );
    cosmos_pgm_write ( W,"W.pgm" );
    W.free();
    W.n = 0;
    W.m = 0;
    cosmos_get_patches_advanced ( I, w, G, W, B, X );
    cosmos_remove_dc ( X );
    cosmos_write_ascii_matrix ( X,"X_disk.ascii" );
    cosmos_dict_display ( X,B,I2,3,1 );

    sc_scale ( short ( 255 ),B );
    cosmos_pgm_write ( B,"B.pgm" );
    cosmos_pgm_write ( I2,"X_disk.pgm" );


    B.free();
    W.free();
    I.free();
    X.free();

}