📄 pgm2dctbindata.cc
字号:
const char *help = "\progname: pgm2dctbindata.cc\n\code2html: This program reads a pgm image and decomposes it in DCT/DCTmod2 blocks.\n\version: Torch3 vision2.0, 2004-2005\n\(c) Sebastien Marcel (marcel@idiap.ch)\n";#include "ImageGray.h"#include "ipDCT2D.h"#include "ipBlock.h"#include "ipDCTmod2.h"#include "DiskXFile.h"#include "CmdLine.h"using namespace Torch;// Allowed dimension for DCT mod2 (from Conrad Sanderson)const int allowed_dim[] = {6, 10, 15, 21, 28, 36, 43, 49, 54, 58, 61, 63, 64};int main(int argc, char *argv[]){ char *filename_in; char *filename_out; int dct_dim; int delta_neighbour; int delta_keep; int block_size; int block_overlap; bool block_to_frame; bool dctmod2; bool overide; bool verbose; bool display; CmdLine cmd; cmd.setBOption("write log", false); cmd.info(help); cmd.addText("\nArguments:"); cmd.addSCmdArg("imagefile in", &filename_in, "image file in"); cmd.addICmdArg("DCT dim", &dct_dim, "DCT 2D dimension"); cmd.addSCmdArg("output filename", &filename_out, "output bindata filename"); cmd.addText("\nOptions:"); cmd.addBCmdOption("-blocktoframe", &block_to_frame, false, "a block becomes a frame"); cmd.addICmdOption("-block", &block_size, 8, "block size"); cmd.addICmdOption("-overlap", &block_overlap, 4, "block overlap"); cmd.addICmdOption("-delta", &delta_neighbour, 1, "delta neighbour"); cmd.addICmdOption("-deltakeep", &delta_keep, 3, "delta keep"); cmd.addBCmdOption("-dctmod2", &dctmod2, false, "apply dctmod2"); cmd.addBCmdOption("-overide", &overide, false, "overide 8x8 dctmod2"); cmd.addBCmdOption("-verbose", &verbose, false, "verbose"); cmd.addBCmdOption("-display", &display, false, "display dct features"); cmd.read(argc, argv); // if(verbose) print("DCT 2D feature extraction.\n"); if((dctmod2 == true) && (block_size != 8)) { warning("DCTmod2 is usually designed only for 8x8 blocks."); if(overide == false) { print("Disabling dctmod2.\n"); dctmod2 = false; } } if(dctmod2 == true) { int dim_ok = 0; int max_ = sizeof(allowed_dim)/sizeof(int); for(int i = 0 ; i < max_ ; i++) if(dct_dim == allowed_dim[i]) dim_ok = 1; if(!dim_ok) { error("DCT dim (%d) is incorrect.", dct_dim); print(" Correct values are: "); for(int i = 0 ; i < max_ ; i++) print("%d ", allowed_dim[i]); print("\n"); exit(0); } } // int n_inputs; int n_patterns; DiskXFile *pf_out; // Load the image ImageGray *grayimage; grayimage = new ImageGray(); grayimage->load(filename_in); // feature image machine to apply for each block ipCore *ip = NULL; int n_block, n_output; // Choose the block by block convolution if(dctmod2 == true) { ip = new ipDCTmod2(grayimage->width, grayimage->height, "gray", dct_dim, block_size, block_overlap, delta_neighbour, delta_keep); ipDCTmod2 *ip_ = (ipDCTmod2 *)ip; n_block = ip_->n_block; n_output = ip_->output_size; } else { ipCore *ip_dct = new ipDCT2D(block_size, "gray", dct_dim); ip_dct->setBOption("verbose", verbose); ip = new ipBlock(grayimage->width, grayimage->height, "gray", ip_dct, dct_dim, block_size, block_overlap); ipBlock *ip_ = (ipBlock *)ip; n_block = ip_->n_block; n_output = dct_dim; } ip->setBOption("verbose", verbose); if(verbose) { print("-> n_block = %d\n", n_block); print("-> n_output = %d\n", n_output); } // pf_out = new DiskXFile(filename_out, "w"); if(pf_out == NULL) { error("Opening bindata file %s", filename_out); return 0; } // if(block_to_frame) { if(verbose) print("Block to frame: each block becomes a frame.\n"); n_inputs = n_output; n_patterns = n_block; } else { n_inputs = n_block * n_output; n_patterns = 1; } if(verbose) { print("Output file ...\n"); print(" n_inputs = %d\n", n_inputs); print(" n_patterns = %d\n", n_patterns); } pf_out->write(&n_patterns, sizeof(int), 1); pf_out->write(&n_inputs, sizeof(int), 1); // float *data_out = new float [n_inputs]; // Apply the pattern machine (DCT) to each block of the image ip->process(grayimage); if(block_to_frame) { for(int b = 0 ; b < n_block ; b++) { if(display == true) print("Block %d: ", b); real *output_ = ip->seq_out->frames[b]; for(int i = 0 ; i < n_output ; i++) { if(display == true) print("%g ", output_[i]); data_out[i] = output_[i]; } if(display == true) print("\n"); for(int i = 0 ; i < n_inputs ; i++) pf_out->write(&data_out[i], sizeof(float), 1); } } else { int j = 0; for(int b = 0 ; b < n_block ; b++) { if(display == true) print("Block %d: ", b); real *output_ = ip->seq_out->frames[b]; for(int i = 0 ; i < n_output ; i++) { if(display == true) print("%g ", output_[i]); data_out[j] = output_[i]; j++; } if(display == true) print("\n"); } for(int i = 0 ; i < n_inputs ; i++) pf_out->write(&data_out[i], sizeof(float), 1); } delete [] data_out; delete pf_out; // Free all delete ip; delete grayimage; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -