Пример #1
0
//----------------------------------------------------------------------
void Tools::save_mat(const MAT& input, const char* name)  {
if(VERBOSE) printf("\nvoid Tools::save_mat(const MAT& input, const char* name=%s)\n",name);

    // Scale the input matrix to between 0 and 255
    float min = input.min();
    float max = input.max();
    float scale = 255.0/(max-min);
    MAT scaled_input = input-min; // Translate to 0
    scaled_input = scaled_input*scale; // Scale between 0, 255

    cv::Mat img(scaled_input.n_rows,scaled_input.n_cols,CV_32FC1,(void*)scaled_input.memptr());
    cv::imwrite( name, img );
}
Пример #2
0
//----------------------------------------------------------------------
void Tools::show_mat(const MAT& input, const char* name, bool wait)  {
if(VERBOSE) printf("\nvoid Tools::show_mat(const MAT& input, const char* name=%s, bool wait=%d)\n",name,wait);

    char winName[50];

    // Scale the input matrix to between 0 and 1 (OpenCV Requirement)
    float min = input.min();
    float max = input.max();
    printf("min = %f\n", min);
    printf("max = %f\n", max);

    MAT trans_input = input-min; // Translate to 0
    float scale = 1.0/(max-min);
    MAT scaled_input = trans_input*scale; // Scale between 0, 1

    //cv::Mat img(input.n_rows,input.n_cols,CV_32FC1,(void*)scaled_input.memptr());
    cv::Mat img(input.n_cols,input.n_rows,CV_32FC1,(void*)scaled_input.memptr());
    img = img.t(); // Armadillo is column-major and OpenCV is row-major
    if( img.empty() ) {
        printf( "Image %s has no data!\n", name );
        exit(EXIT_FAILURE);
    }
    sprintf( winName, "MAT %s", name );
    int winFlag = CV_WINDOW_NORMAL;
    cv::namedWindow( winName, winFlag );
    
// Only to test data... --------
#if 0
    printf("OpenCV Rows = %d\n", img.rows);
    printf("OpenCV Cols = %d\n", img.cols);
    printf("depth = %d\n", img.depth() );
    printf("channels = %d\n", img.channels() );
    printf("0 |");
    for(int y=0; y<img.rows; y++ ) {
        if(y!=0) {
            printf("%d |", y);
        }
        for( int x=0; x<img.cols; x++ ) {
            printf(" %f ", img.at<float>(y,x)*(max-1)+min);
        }
        printf("|\n");
    }
#endif
// -----------------------------


    cv::imshow( winName, img );
    if(wait) {
        cv::waitKey(0);
    }
}