load image

1,647 views
Skip to first unread message

wei oui

unread,
Aug 4, 2011, 4:46:59 AM8/4/11
to android-opencv
Hi all,

I want to ask that, is android-opencv support imread in opencv and if
it does, then how should we load an image into android platform? Can
some1 provide an example on how tis can be done. I'm ok with opencv
but not so familiar with android platform.

thanks,

wei

Andrey Kamaev

unread,
Aug 4, 2011, 5:36:55 AM8/4/11
to android-opencv
Hi Wei,

Both imread and imwrite are supported.

Java code sample:

import org.opencv.core.Mat;
import org.opencv.highgui.Hughgui;

.................

Mat img = someMethod();
Highgui.imwrite(img, "/mnt/sdcard/image.png");


C++ code sample:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

.................

Mat img = someMethod();
imwrite(img, "/mnt/sdcard/image.png");


And you probably need to add following user-permission to your
AndroidManifest.xml for writing to sdcard:

<users-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

/Andrey

wei oui

unread,
Aug 8, 2011, 1:28:32 AM8/8/11
to android...@googlegroups.com
Hi,

If I'm using imread("image", 1);

then wat path should I put in image. I tried several time with different value but get force close when I run the application. I put the image under "/res/drawable-hdpi/" folder.


thanks,
wei.

Aaron Brown

unread,
Aug 8, 2011, 8:12:06 AM8/8/11
to android-opencv
Hi, Wei,

Objects stored in the /res/drawable-* folders can be accessed by
loading the resource using the given static int in the R.drawable
class:

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.pic1)

Note: it's best practice to ensure that the image is available for
all possible screen sizes or densities. As such, you should probably
put a copy of the image in drawable-ldpi and drawable-mdpi as well.
When you do this, Eclipse should automatically create an entry for you
in the R.drawable class.

If you need to reference the image in .xml (e.g. for building a
layout), then you can use "@drawable/pic1" or "@android:drawable/pic1"
depending on the context.

I hope this helps!

Best Regards,
Aaron M Brown

On Aug 7, 10:28 pm, wei oui <wei17...@gmail.com> wrote:
> Hi,
>
> If I'm using imread("image", 1);
>
> then wat path should I put in image. I tried several time with different
> value but get force close when I run the application. I put the image under
> "/res/drawable-hdpi/" folder.
>
> thanks,
> wei.
>

Andrey Kamaev

unread,
Aug 11, 2011, 4:10:53 AM8/11/11
to android-opencv
Hi All,

Upcoming OpenCV for Android beta2 will include a helper method to read
images directly from resources:

import org.opencv.android;
import org.opencv.core;

Mat img = Utils.loadResource(getContext(),
R.drawable.your_image_name);


It is already available in OpenCV trunk and will be released in a
week.

/Andrey
Reply all
Reply to author
Forward
0 new messages