Java Code Examples for org.opencv.core.CvType.CV_32F

The following are Jave code examples for showing how to use CV_32F of the org.opencv.core.CvType class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jaer   File: TaFileStorage.java   View Source Code Vote up 4 votes
public void writeMat(String tag, Mat mat) {
    try {
        if( isWrite == false) {
            System.err.println("Try write to file with no write flags");
            return;
        }

        Element matrix = doc.createElement(tag);
        matrix.setAttribute("type_id", "opencv-matrix");
        rootElement.appendChild(matrix);

        Element rows = doc.createElement("rows");
        rows.appendChild( doc.createTextNode( String.valueOf(mat.rows()) ));

        Element cols = doc.createElement("cols");
        cols.appendChild( doc.createTextNode( String.valueOf(mat.cols()) ));

        Element dt = doc.createElement("dt");
        String dtStr;
        int type = mat.type();
        if(type == CvType.CV_32F ) { // type == CvType.CV_32FC1
            dtStr = "f";
        }
        else if( type == CvType.CV_32S ) { // type == CvType.CV_32SC1
            dtStr = "i";
        }
        else if( type == CvType.CV_16S  ) { // type == CvType.CV_16SC1
            dtStr = "s";
        }
        else if( type == CvType.CV_8U ){ // type == CvType.CV_8UC1 
            dtStr = "b";
        }
        else {
            dtStr = "unknown";
        }
        dt.appendChild( doc.createTextNode( dtStr ));

        Element data = doc.createElement("data");
        String dataStr = dataStringBuilder( mat );
        data.appendChild( doc.createTextNode( dataStr ));

        // append all to matrix
        matrix.appendChild( rows );
        matrix.appendChild( cols );
        matrix.appendChild( dt );
        matrix.appendChild( data );

    } catch(Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Project: jaer   File: TaFileStorage.java   View Source Code Vote up 4 votes
private String dataStringBuilder(Mat mat) {
    StringBuilder sb = new StringBuilder();
    int rows = mat.rows();
    int cols = mat.cols();
    int type = mat.type();

    if( type == CvType.CV_32F ) {
        float fs[] = new float[1];
        for( int r=0 ; r<rows ; r++ ) {
            for( int c=0 ; c<cols ; c++ ) {
                mat.get(r, c, fs);
                sb.append( String.valueOf(fs[0]));
                sb.append( ' ' );
            }
            sb.append( '\n' );
        }       
    }
    else if( type == CvType.CV_32S ) {
        int is[] = new int[1];
        for( int r=0 ; r<rows ; r++ ) {
            for( int c=0 ; c<cols ; c++ ) {
                mat.get(r, c, is);
                sb.append( String.valueOf(is[0]));
                sb.append( ' ' );
            }
            sb.append( '\n' );
        }
    }
    else if( type == CvType.CV_16S ) {
        short ss[] = new short[1];
        for( int r=0 ; r<rows ; r++ ) {
            for( int c=0 ; c<cols ; c++ ) {
                mat.get(r, c, ss);
                sb.append( String.valueOf(ss[0]));
                sb.append( ' ' );
            }
            sb.append( '\n' );
        }
    }
    else if( type == CvType.CV_8U ) {
        byte bs[] = new byte[1];
        for( int r=0 ; r<rows ; r++ ) {
            for( int c=0 ; c<cols ; c++ ) {
                mat.get(r, c, bs);
                sb.append( String.valueOf(bs[0]));
                sb.append( ' ' );
            }
            sb.append( '\n' );
        }
    }       
    else {
        sb.append("unknown type\n");
    }

    return sb.toString();
}