Ask Your Question
0

Reading an image from drawable folder- android

asked 2014-09-15 06:39:14 -0600

prasanna aarthi gravatar image

I have my image file under drawable folder, I am trying to read the image to process it further.This is the method I am using ,image.width() always return 0,I am not getting any errors in the log cat.

    public Mat loadImageFromFile(String fileName) {

    Mat rgbLoadedImage = null;

    File root = Environment.getExternalStorageDirectory();
    File file = new File(root, fileName);

    // this should be in BGR format according to the
    // documentation.
    Mat image = Highgui.imread(file.getAbsolutePath());

    if (image.width() > 0) {

        rgbLoadedImage = new Mat(image.size(), image.type());

        Imgproc.cvtColor(image, rgbLoadedImage, Imgproc.COLOR_BGR2RGB);

        if (DEBUG)
            Log.d(TAG, "loadedImage: " + "chans: " + image.channels()
                    + ", (" + image.width() + ", " + image.height() + ")");

        image.release();
        image = null;
    }
    return rgbLoadedImage;

}

I am calling this method after open CV has been loaded

 private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully...................");
                loadImageFromFile("pedestrian");

            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

I have also tried Mat m = Highgui.imread("/pedestrian"); here also m.width() is 0.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-09-28 03:51:30 -0600

itay gravatar image

updated 2014-09-28 03:52:03 -0600

You have to initial the image Matrix like this:

Mat image =  new Mat(new Size(Width,Height ),CvType.CV_8U);// Change CvType as you need.

and then :

image = Highgui.imread(file.getAbsolutePath());

Hope it solve your problem.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-09-15 06:39:14 -0600

Seen: 5,678 times

Last updated: Sep 28 '14