How van I convert drawable to Mat?

1,977 views
Skip to first unread message

Csaba Bolyós

unread,
Dec 9, 2011, 5:51:16 AM12/9/11
to android...@googlegroups.com
Hi I was using 
Mat mLogo = new Mat();
mLogo = Highgui.imread("STRING TO FILE ON SDCARD");
But now I want to add that image to drawable, but I m unable to find the path or other method to create Mat from Drawable, or bitmap.

Thanks for any help


--
Bolyós Csaba

Csaba Bolyós

unread,
Dec 9, 2011, 6:03:57 AM12/9/11
to android...@googlegroups.com
I have Find it out  


mLogo = Utils.loadResource(getContext(), R.drawable.slsp);

2011/12/9 Csaba Bolyós <blade...@gmail.com>



--
Bolyós Csaba

Hardik

unread,
Dec 9, 2011, 6:04:18 AM12/9/11
to android...@googlegroups.com
Hi Csaba,


you can create bitmap from Mat using following method,,


Utils.matToBitmap(Mat,bitmap)


Thanks.....
--
Regards,

Pansuria Hardik

Sourcebits  Inc.



Csaba Bolyós

unread,
Dec 9, 2011, 7:26:13 AM12/9/11
to android...@googlegroups.com
thanks

2011/12/9 Hardik <har...@sourcebits.com>



--
Bolyós Csaba

Roman

unread,
Dec 10, 2011, 6:44:59 AM12/10/11
to android-opencv
Hi, do you know any way to keep drawable aplha channel in mat?
Thanks, Roman

On 9 pro, 13:26, Csaba Bolyós <bladesza...@gmail.com> wrote:
> thanks
>
> 2011/12/9 Hardik <har...@sourcebits.com>
>
>
>
>
>
>
>
>
>
> > Hi Csaba,
>
> > you can create bitmap from Mat using following method,,
>
> > Utils.matToBitmap(Mat,bitmap)
>
> > Thanks.....
>

> > On Fri, Dec 9, 2011 at 4:21 PM, Csaba Bolyós <bladesza...@gmail.com>wrote:
>
> >> Hi I was using
>
> >> Mat mLogo = new Mat();
>
> >> mLogo = Highgui.imread("STRING TO FILE ON SDCARD");
>
> >> But now I want to add that image to drawable, but I m unable to find the
> >> path or other method to create Mat from Drawable, or bitmap.
>
> >> Thanks for any help
>
> >> --
> >> Bolyós Csaba
>
> > --
> > Regards,
>

> > *Pansuria Hardik
>
> > Sourcebits  Inc.
> > *
>
> --
> Bolyós Csaba

Hardik

unread,
Dec 10, 2011, 12:36:34 PM12/10/11
to android...@googlegroups.com
Hi roman,

I dont get you,,, Can you explain me ??
--
Regards,

Pansuria Hardik

Android Developer





Roman

unread,
Dec 10, 2011, 5:04:43 PM12/10/11
to android-opencv
Hi Hardik,
I would like to load png with alpha channel into mat and keep the
alpha to show part from original matrix - maybe mask, or i dont know
exactly if its possible.
I detect object in image and wants to show non rectangle shaped image
with full opaque background on this position, if i load resource to
mat, alpha channel is lost and image has white background.

> *Pansuria Hardik
>
> Android Developer
>
> *

Csaba Bolyós

unread,
Dec 10, 2011, 5:21:59 PM12/10/11
to android...@googlegroups.com
It is probably problem with the image, I load the image from drawables like R.drawable.myimage and display it like bitmap and it works fine.

as U can see it in the pictures

2011/12/10 Roman <hrusk...@gmail.com>



--
Bolyós Csaba
device-2011-12-09-132816.png
device-2011-12-09-132848.png

Roman

unread,
Dec 11, 2011, 6:34:21 AM12/11/11
to android-opencv
I use:
mFace = new Mat();
mFace = Utils.loadResource(context, R.drawable.faceicon); // faceicon
is PNG32 with alpha background

mROI = mRgba.submat(r); //r is rectangle with detected object
Imgproc.resize(mFace, mROI, mROI.size());
mFace.copyTo(mROI);

Do you get only position and draw drawable in some view over the
preview, or use mat too?
Thanks Csaba,
Roman

On 10 pro, 23:21, Csaba Bolyós <bladesza...@gmail.com> wrote:
> It is probably problem with the image, I load the image from drawables like
> R.drawable.myimage and display it like bitmap and it works fine.
>
> as U can see it in the pictures
>

> 2011/12/10 Roman <hruskozr...@gmail.com>

>  device-2011-12-09-132816.png
> 235KZobrazitStáhnout
>
>  device-2011-12-09-132848.png
> 264KZobrazitStáhnout

Andrey Pavlenko

unread,
Dec 14, 2011, 8:14:08 AM12/14/11
to android...@googlegroups.com
Mat::copyTo() doesn't interpret the Mat's 4th channel as a mask (like Bitmaps processing functions do).
You need either to use Android API drawing methods instead or to extract alpha channel with Core.extractChannel() and to pass it as the 2nd argument to Mat::copyTo().

Csaba Bolyós

unread,
Jan 3, 2012, 5:09:20 AM1/3/12
to android...@googlegroups.com


2011/12/11 Roman <hrusk...@gmail.com>

I use:
mFace = new Mat();
mFace = Utils.loadResource(context, R.drawable.faceicon); // faceicon
is PNG32 with alpha background

mROI = mRgba.submat(r); //r is rectangle with detected object
Imgproc.resize(mFace, mROI, mROI.size());
mFace.copyTo(mROI);

Do you get only position and draw drawable in some view over the
preview, or use mat too?

Only position
 
Thanks Csaba,
Roman

danphuc2010

unread,
Jan 10, 2012, 5:16:13 AM1/10/12
to android-opencv
help me!
i have code:

import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import android.graphics.Bitmap;

public class Process {


public static Bitmap process1 (Bitmap bsrc){
Mat src = Utils.bitmapToMat(bsrc);
Mat dst = new Mat(src.cols(), src.rows(), CvType.CV_8UC1, new
Scalar(0));
Imgproc.cvtColor(src, dst,Imgproc.COLOR_RGB2GRAY,4);
Bitmap bdst = Bitmap.createBitmap(dst.cols(), dst.rows(),
Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dst, bdst);
return bdst;
}
}
with opencv 2.3.1 and android version 2.2;
what is mistake?
thanks for anyone.

Kirill Kornyakov

unread,
Jan 10, 2012, 10:41:59 AM1/10/12
to android...@googlegroups.com
And what is the problem? Compilation, linking, runtime error?

danphuc2010

unread,
Jan 11, 2012, 2:09:15 AM1/11/12
to android-opencv
umh, sorry.
when running, window must have a gray-picture, but it appear a small
window "sorry!" : The application BlurImage (process
com.example.blurimage) has stopped unexpectedly. Please try again.
what is it?
how to correct?

On Jan 10, 10:41 pm, Kirill Kornyakov <kirill.kornya...@itseez.com>
wrote:

Andrey Pavlenko

unread,
Jan 16, 2012, 8:24:10 AM1/16/12
to android...@googlegroups.com
Mat src = Utils.bitmapToMat(bsrc); 
Mat dst = new Mat(); 
Imgproc.cvtColor(src, dst,Imgproc.COLOR_RGBA2GRAY);
Mat dstC4 = new Mat();  
Imgproc.cvtColor(dst,dstC4, Imgproc.COLOR_GRAY2RGBA, 4); 
Bitmap bdst = Bitmap.createBitmap(dstC4.cols(),  dstC4.rows(), Bitmap.Config.ARGB_8888); 
Utils.matToBitmap(dstC4, bdst); 
return bdst;  
Reply all
Reply to author
Forward
0 new messages