Bitmap to Mat? android opencv - android

Hi i have my Bitmap object that i need to convert in Mat object .I saw that somebody used the bitmaptoMat function,but in my Utils package i have a lot of function with points list input to get Mat output but i have my Bitmap file.I tried another solution: Imgcodecs.imread that need string input,and i guess it needs the images path but i tried giving uri.toString() but it doesnt work.What can i do?
This is my Bitmap:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

Import
import org.opencv.android.Utils;
And then use the function:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
Mat imgMAT = new Mat (bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(bitmap, imgMAT);

You can use pointer to bitmap, to generate a Mat file.
I did this with QT and it looks like this :
QImage img((uchar*)frame->data(), frame->width(), frame->height(), QImage::Format::Format_RGB888);
I did it in opencv, but I don't remember the exact constructor:
try to do something like this:
cv::Mat bitmapMat = cv::Mat(bitmap.height, bitmap.width, CV_8UC1, bitmap.data());
or
cv::Mat bitmapMat = cv::Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);

Related

Getting java.lang.IllegalArgumentException: width and height must be > 0 error while initializing Mat in android

I am trying to initialize Mat and this is my code
Mat imgRgba = new Mat();
final Bitmap bitmap =
Bitmap.createBitmap(imgRgba.width(), imgRgba.height(), Bitmap.Config.RGB_565);
Utils.matToBitmap(imgRgba, bitmap);
I have tried this too
Bitmap.createBitmap(imgRgba.cols(), imgRgba.rows(), Bitmap.Config.RGB_565);
but when I am going to run my app I am getting crash and exception is this
Stack trace:
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:969)
at android.graphics.Bitmap.createBitmap(Bitmap.java:948)
at android.graphics.Bitmap.createBitmap(Bitmap.java:915)
at com.jmr.agency.banking.ui.facerecognition.FRAddPersonPreviewActivity.onCameraFrame(FRAddPersonPreviewActivity.java:156)
Try either mat.create(int rows, int cols, int type) or some other constructor for Mat object, as currently it does not have a size.
You did new instance Mat for size. But it has no size.If you have bitmap or Mat data you can use your code like this:
private Bitmap getBitmap(Mat imgRgb){
final Bitmap bitmap =
Bitmap.createBitmap(imgRgba.width(), imgRgba.height(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imgRgba, bitmap);
return bitmap;
}
Now you can call this method with your existing Mat data for convert your Mat data to bitmap.
Or you can convert your Bitmap to Mat:
private Bitmap getMat(Bitmap bitmap){
Mat mat = new Mat();
mat = Utils.bitmapToMat(bitmap,mat);
return mat;
}
Hope you understand.

Converting Bitmap To Mat through Utils.bitmapToMat changes its color [duplicate]

So I make a bitmap from a blob with the next code:
byte[] blob = contact.getMP();
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Bitmap scalen = Bitmap.createScaledBitmap(bitmap, 320, 240, false);
and it gives back the next output, which is good
Then I do the following to make the bitmap into a Mat, but then my colors just change...
//Mat ImageMat = new Mat();
Mat ImageMat = new Mat(320, 240, CvType.CV_32F);
Utils.bitmapToMat(scalen, ImageMat);
I have no idea why, nor another way to make the bitmap into a Mat. What is wrong?
The format of color channels in Android Bitmap are RGB
But in opencv Mat, the channels are BGR by default.
So when you do Utils.bitmapToMat(), [B,G,R] values are stored in [R,G,B] channels. The red and blue channels are interchanged.
One possible solution is to apply cvtcolor on the opencv Mat you got as below:
Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_BGR2RGB);
It worked for me.

Convert a bitmap to mat and vice versa in OpenCV

I have an Android Bitmap in my code and I would like to run the
cvCanny method on it. However, it needs to be in a Mat first. How do I
convert the data to Mat, and how do I convert it back to Bitmap when
I'm done?
First import org.opencv.android.Utils
Then use:
Mat src = new Mat();
Utils.bitmapToMat(bitmap, src);
To perform edge detection:
Mat dest = new Mat();
Imgproc.Canny(src, dest, min, max);
Bitmap edgeBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dest, edgeBitmap);
//edgeBitmap is ready

Utils.bitmapToMat crashing app

I'm trying to convert a bmp file into a Mat, and then convert it to greyscale. But I'm having trouble getting it working. Here's what I've got:
String filename = "/mnt/sdcard/DCIM/01.bmp";
Bitmap bmp = BitmapFactory.decodeFile(filename);
Mat imgToProcess = null;
Utils.bitmapToMat(bmp, imgToProcess);
But whenever that final line is used, the app just crashes (the rest of the time it continues on just fine).
The rest of the code was going to be:
Imgproc.cvtColor(imgToProcess, imgToProcess, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(imgToProcess, imgToProcess, Imgproc.COLOR_GRAY2RGBA, 4);
Utils.matToBitmap(imgToProcess, bmp);
I've no idea whether or not that works though, since I can't get the file converted to a Mat yet from the earlier part of the code. Looking at the documentation for Utils (found here) I'm using it correctly, but it's still not working.
Can anyone help me out here?
Change line:
Mat imgToProcess = null;
to this:
Mat imgToProcess = new Mat();
or this:
Mat imgToProcess = new Mat(bmp.getHeight(), bmp.getHeight(), CvType.CV_8UC4);
And why don't you just use Highgui.imread instead?
Mat imgToProcess = Highgui.imread(filename);

Android OpenCV Highgui.imread wrong color

i have code to load image from sdcard and post it to ImageView.
Mat mRgba = Highgui.imread(dir);
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mRgba, bmp);
mImage.setImageBitmap(bmp, true, null, 5.0f);
the image is loaded but it's wrong color. Color seem to be inverted (but not inverted).
Here is image comparison
I tried to load image by
Bitmap bmp = BitmapFactory.decodeFile(dir);
It worked correctly. But i have to use Highgui.imread.
What wrong with my code?
You will have to use something like this:
Mat inputImage = Highgui.imread(pathToFile);
Mat tmp = new Mat();
Imgproc.cvtColor(inputImage, tmp, Imgproc.COLOR_BGR2RGB);
Bitmap imageToShow = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(tmp, imageToShow);
You're trying to load a bitmap supposing that the image is 8-bit/color RGBA: are you sure of that?
Also note that ARGB is not RGBA. You may need to re-arrange the bytes of each pixel. Something like
int pixel = get_the_pixel();
int alpha = 0xff & pixel;
pixel = pixel<<8 | alpha;
set_the_pixel(pixel);
You'll want to do something more efficient than accessor methods shown here, but you get the idea.

Categories

Resources