Monday, 23 June 2014

OpenCV4Android, load Images to a Mat

1. In java load a image in /res/drawable. (the extension name is doesn't matter, the image type will be recognized by opencv.)

    mRgba = loadfiles(R.drawable.p2,frameWidth,frameHeight); //load and convert to a given size.

    private Mat loadfiles(int p,int width, int height){
    Mat pmat = null;
    try {
    pmat = Utils.loadResource(this, p);
Imgproc.cvtColor(pmat, pmat, Imgproc.COLOR_GRAY2BGRA);//remove this if your image don't need  to convert. 
Imgproc.resize(pmat, pmat, new Size(width, height), 0, 0, Imgproc.INTER_CUBIC); 
} catch (IOException e) {
    return pmat;

2. In C++ load a image in sdcard.
    Mat groundtruthx = imread("/storage/sdcard0/p2gt.pgm", 1);//same with c++ opencv.

No comments:

Post a comment