Want faster method to write an OpenCV Mat object

1,614 views
Skip to first unread message

Lawrence Tsang

unread,
Dec 30, 2011, 11:53:37 PM12/30/11
to android-opencv
Hi All,

I have recently used the following code to write the content of a
Mat object :

m_yuv.put(0, 0, mFrame);
Imgproc.cvtColor(m_yuv, m_rgba, Imgproc.COLOR_YUV420sp2RGB, 4);
m_bmp = Bitmap.createBitmap(mFrameWidth, mFrameHeight,
Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m_rgba, m_bmp);

// Write the bitmap to external storage.
if ( m_ExternalStorageAvailable && m_ExternalStorageWriteable ) {
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoCalibrationPicture_" + new
SimpleDateFormat ("yyyyMMddHHmmss").format(new Date()) + ".png");
try {
path.mkdirs();
FileOutputStream fOut = new FileOutputStream(file);
m_bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
Log.i(TAG, "m_bmp has been written to external storage.");
} catch (IOException e) {
Log.w(TAG, "Error writing : m_bmp" + file, e);
}
} else {
Log.i(TAG, "External Storage NOT available or writable.");
}

which use the mFrame data in "onPreviewFrame" callback, put the
data into the "m_yuv" Mat object, convert it to an Android Bitmap and
then write the Bitmap to external storage.

However, it takes 1 to 2 seconds to write the Mat object into
external storage which is a bit too slow for my application.

Does anyone know a faster method to write the Mat object to
disk ? Thanks for any suggestion

Lawrence

陈卿

unread,
Dec 31, 2011, 12:06:14 AM12/31/11
to android...@googlegroups.com
I have the same problem before. when I use the android java-api ,it takes 10 seconds. and then I change it to C++ code and build with ndk....it's instant!!!

2011/12/31 Lawrence Tsang <tkw...@gmail.com>

Lawrence Tsang

unread,
Dec 31, 2011, 12:50:59 AM12/31/11
to android-opencv
Hi 陈卿 and All,

Thanks for your suggestion.

If ndk is used, which method do you use to write the Mat object,
"imwrite" or something else.

Lawrence

On Dec 31, 1:06 pm, 陈卿 <chenqing1...@gmail.com> wrote:
> I have the same problem before. when I use the android java-api ,it takes
> 10 seconds. and then I change it to C++ code and build with ndk....it's
> instant!!!
>
> 2011/12/31 Lawrence Tsang <tkwi...@gmail.com>

陈卿

unread,
Dec 31, 2011, 1:07:11 AM12/31/11
to android...@googlegroups.com
I use this method !
if 1 channel mat...
(colorZoomImg.data + colorZoomImg.step * i)[j * colorZoomImg.channels() ];

if 4 channels mat...alpha is ignored!
(colorZoomImg.data + colorZoomImg.step * i)[j * colorZoomImg.channels()+0 ];
(colorZoomImg.data + colorZoomImg.step * i)[j * colorZoomImg.channels()+1 ];
(colorZoomImg.data + colorZoomImg.step * i)[j * colorZoomImg.channels()+2 ];

have a try! good luck!


2011/12/31 Lawrence Tsang <tkw...@gmail.com>

陈卿

unread,
Dec 31, 2011, 1:10:57 AM12/31/11
to android...@googlegroups.com
you mean write to SD card?

Lawrence Tsang

unread,
Dec 31, 2011, 1:36:55 AM12/31/11
to android-opencv
Hi 陈卿 and All,

Yes, I mean writing a Mat object to SD card. I suppose to use

imwrite("/sdcard/Pictures/filename.png", m_yuv);

But I am not sure whether I could use "imwrite" this way in
Android NDK (as Android has some file permission issues, I am not sure
if NDK could write a file directly to an external storage location.)

Lawrence

On Dec 31, 2:10 pm, 陈卿 <chenqing1...@gmail.com> wrote:
> you mean write to SD card?
>
> 在 2011年12月31日 下午2:07,陈卿 <chenqing1...@gmail.com>写道:
>
>
>
>
>
>
>
> > I use this method !
> > if 1 channel mat...
> > (colorZoomImg.data + colorZoomImg.step * i)[j * colorZoomImg.channels() ];
>
> > if 4 channels mat...alpha is ignored!
> > (colorZoomImg.data + colorZoomImg.step * i)[j * colorZoomImg.channels()+0
> > ];
> > (colorZoomImg.data + colorZoomImg.step * i)[j * colorZoomImg.channels()+1
> > ];
> > (colorZoomImg.data + colorZoomImg.step * i)[j * colorZoomImg.channels()+2
> > ];
>
> > have a try! good luck!
>
> > 2011/12/31 Lawrence Tsang <tkwi...@gmail.com>

陈卿

unread,
Dec 31, 2011, 2:00:30 AM12/31/11
to android...@googlegroups.com
of course not.....I use this android java api 

fileUtils.writeToSDFromInput(here is your save path&bitmap);

if using ndk , you should convert C++ mat to jintArray ,then convert it to bitmap.

Lawrence Tsang

unread,
Dec 31, 2011, 2:33:11 AM12/31/11
to android-opencv
Thanks 陈卿.


On Dec 31, 3:00 pm, 陈卿 <chenqing1...@gmail.com> wrote:
> of course not.....I use this android java api
>
> fileUtils.writeToSDFromInput(here is your save path&bitmap);
>
> if using ndk , you should convert C++ mat to jintArray ,then convert it to
> bitmap.
>

陈卿

unread,
Dec 31, 2011, 5:01:02 AM12/31/11
to android...@googlegroups.com
it's my pleasure!!
Reply all
Reply to author
Forward
0 new messages