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);
Related
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.
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
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);
I am new in openCV android image Processing. but i am facing some problem. when i use openCV in android and i use Mat then my apps will crushed.... what is the problem with this.....
My code is here..:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img=(ImageView) findViewById(R.id.imageView);
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.in);
Mat tmp = new Mat (b.getWidth(), b.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(b, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
//there could be some processing
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2RGB, 4);
Utils.matToBitmap(tmp, b);
img.setImageBitmap(b);
}
There are multiple problems:
Mat constructor is Mat(height, width, type), you inverted height and width.
CV_8UC1 is a single channel (grayscale). So if it IS necessary to create the Mat in advance, you should create a Mat with appropriate number of channels. I assume you want to use a RGB image, so use CV_8UC3. But typically, functions re-allocate the Mat if the size doesn't fit. If that's the case with bitmapToMat too, then there is another problem.
Afaik, android uses RGBA as standard color-space. So PROBABLY after Utils.bitmapToMat(b, tmp); tmp is a 4 channel RGBA matrix. So please try Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGBA2GRAY); and later Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2RGBA, 4); if you aren't sure you should add some checks like if Mat.channels isn't 3, don't try to convert RGB2GRAY if the check fails you can try to find out what the real number of channels is and why.
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.