Mat naiveBlitzToCvMat(blitz::Array<float, 2> a, float multiplier){ int i, j; Mat b = Mat(a.rows(), a.cols(), CV_32FC1); for (i=0; i< a.rows(); i++){ for(j=0; j<a.cols(); j++){ b.at<float>(i,j) = a(i, j) * multiplier; } } return b; }
void mouse( int button, int state, int x, int y) { //#define CLICK_SAVE_COLS #ifdef CLICK_SAVE_COLS int kw = 16; int xmin, xmax, ymin, ymax; xmin = std::max( 1, x - kw); ymin = std::max( 1, y - kw); xmax = std::min( x + kw, image.cols() - 1); ymax = std::min( y + kw, image.rows() - 1); std::ofstream of( "cluster_dat.hpp"); if( !of) return; of << "const int num_pts = " << (xmax - xmin) * (ymax - ymin) * 4 << ";\n"; of << "float dat[num_pts] = {\n"; for( int j=ymin;j<ymax;++j) { for( int i=xmin;i<xmax;++i) { Imath::Color3f col = image( j, i); of << col.x << ", " << col.y << ", " << col.z << ", 1.0, \n"; } } of << "};\n"; #endif }
void gl_init( void) { glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 ); glClearColor ( 0.0F, 0.0F, 0.0F, 0.0F ); glViewport ( 0, 0, image.cols(), image.rows()); // set coordinate frame for graphics in window glMatrixMode ( GL_PROJECTION ); glLoadIdentity(); gluOrtho2D ( 0, image.cols(), image.rows(), 0); glMatrixMode ( GL_MODELVIEW); glLoadIdentity(); glPixelZoom( 1, -1); }
inline double Inverse_Matrix_with_lapack_and_Return_Determinant (blitz::Array<double,2> & M, std::string message="") { assert (M.rows() ==M.cols()); int n = M.rows(); if (n==0) return 1; ref<double,2> refM(M,false);//do not check the ordering since inverse and transposition commutes double * p = refM; return Inverse_Matrix_with_lapack_and_Return_Determinant(p,n,n,message); }
void convert2rgb( blitz::Array<Imath::Color3f,2>& img) { vigra::BasicImageView<vigra::TinyVector<float,3> > img_view( (vigra::TinyVector<float,3> *) img.dataFirst(), img.cols(), img.rows()); for( int j=0;j<img.rows();++j) for( int i=0;i<img.cols();++i) img( j, i) *= lab_scale; vigra::transformImage( vigra::srcImageRange( img_view), vigra::destImage( img_view), vigra::Lab2RGBFunctor<float>( 1.0f)); }
void display( void) { glClear( GL_COLOR_BUFFER_BIT); glRasterPos2i( 0, 0); switch( disp) { case display_a: glDrawPixels( image.cols(), image.rows(), GL_LUMINANCE, GL_FLOAT, alpha.dataFirst()); break; case display_bg: glDrawPixels( image.cols(), image.rows(), GL_RGB, GL_FLOAT, bg.dataFirst()); break; case display_fg: glDrawPixels( image.cols(), image.rows(), GL_RGB, GL_FLOAT, fg.dataFirst()); break; case display_comp: glDrawPixels( image.cols(), image.rows(), GL_RGB, GL_FLOAT, comp.dataFirst()); break; case display_img: glDrawPixels( image.cols(), image.rows(), GL_RGB, GL_FLOAT, image.dataFirst()); break; case display_tri: glDrawPixels( image.cols(), image.rows(), GL_LUMINANCE, GL_UNSIGNED_BYTE, trimap.dataFirst()); break; } glutSwapBuffers(); }
int main( int argc, char **argv) { std::string inpf, trif, outf, backf; adobe::dictionary_t params; bool use_lab = false; try { po::options_description desc("Allowed options"); po::variables_map vm; po::positional_options_description p; p.add( "input" , 1); p.add( "trimap", 1); desc.add_options() ("help", "produce help message") // files ("input", po::value<std::string>(), "input file") ("trimap",po::value<std::string>(), "trimap file") ("output,o", po::value<std::string>(), "output file") ("background,bg", po::value<std::string>(), "clean background") ("save_bg", "save estimated background") // colors ("rgb", "use rgb colorspace") // sampling ("kwin", po::value<int>()->default_value( 8), "known window radius") ("uwin", po::value<int>()->default_value( 8), "unknown window radius") ("mins", po::value<int>()->default_value( 16), "min samples") // clusters ("clusters", po::value<int>()->default_value( 5), "max number of clusters") ("ctheresh", po::value<double>()->default_value( 0.001), "cluster thereshold") // optimize ("cvar", po::value<double>()->default_value( 0.04), "camera variance") // gui ("window,w", "show results in a window") ; // end po::store( po::command_line_parser( argc, argv).options( desc).positional( p).run(), vm); po::notify( vm); if( vm.count( "help") || !vm.count("input") || !vm.count("trimap")) usage(); if( (!vm.count("output")) && (!vm.count("window"))) usage(); inpf = vm["input"].as<std::string>(); trif = vm["trimap"].as<std::string>(); if( vm.count("output")) outf = vm["output"].as<std::string>(); use_lab = !vm.count("rgb"); params[adobe::name_t("use_lab")] = adobe::any_regular_t( use_lab); // load images image.reference( read_image( inpf.c_str())); trimap.reference( read_trimap( trif.c_str())); if( (trimap.rows() != image.rows()) || (trimap.cols() != image.cols())) { std::cout << "Image & trimap dimensions don't match\n"; exit( boost::exit_failure); } if( use_lab) convert2lab( image); if( vm.count( "background")) { backf = vm["background"].as<std::string>(); params[adobe::name_t("use_back")] = adobe::any_regular_t( true); bg.reference( read_image( backf.c_str())); if( use_lab) convert2lab( bg); } else { bg.resize( image.rows(), image.cols()); params[adobe::name_t("use_back")] = adobe::any_regular_t( false); } // sampling params[ adobe::name_t( "kwin_size")] = adobe::any_regular_t( vm["kwin"].as<int>()); params[ adobe::name_t( "uwin_size")] = adobe::any_regular_t( vm["uwin"].as<int>()); params[ adobe::name_t( "min_samples")] = adobe::any_regular_t( vm["mins"].as<int>()); // cluster params[ adobe::name_t( "maxk")] = adobe::any_regular_t( vm["clusters"].as<int>()); params[ adobe::name_t( "ctheresh")] = adobe::any_regular_t( vm["ctheresh"].as<double>()); // optimize params[ adobe::name_t( "cvar")] = adobe::any_regular_t( vm["cvar"].as<double>()); fg.resize( image.rows(), image.cols()); alpha.resize( image.rows(), image.cols()); bmatte bm( image, trimap, fg, bg, alpha, params); bm(); if( use_lab) { convert2rgb( image); convert2rgb( fg); convert2rgb( bg); } if( vm.count( "output")) { save_image( outf.c_str(), fg, &alpha); if( vm.count( "save_bg")) { std::string bgf = "bg_" + outf; save_image( bgf.c_str(), bg); } } if( vm.count( "window")) { comp.resize( image.rows(), image.cols()); for( int j=0;j<image.rows();++j) { for( int i=0;i<image.cols();++i) { float t = alpha( j, i); comp( j, i) = (fg( j, i) * t) + ( Imath::Color3f( .5f, .5f, .5f) * (1.0f - t)); } } glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition( 50, 50); glutInitWindowSize( image.cols(), image.rows()); glutCreateWindow( "BMatte"); glutKeyboardFunc( key); glutMouseFunc( mouse); glutDisplayFunc( display); gl_init(); glutMainLoop(); } } catch( std::exception& e) { std::cerr << "error: " << e.what() << "\n"; return boost::exit_failure; } catch( ...) { std::cerr << "Exception of unknown type!\n";} return boost::exit_success; }