Trouble converting mat to bitmap? - java

I am using OpenCV to detect the similarities between two images. Unfortunately, my line of code that converts my Mat back to a bitmap is causing my app to crash. Does anyone have any ideas what would be causing this? Here is my code:
Bitmap centerBit = BitmapFactory.decodeResource(getResources(), R.drawable.center);
Mat center = new Mat();
Utils.bitmapToMat(centerBit,center);
Mat resultCenter = new Mat();
Imgproc.matchTemplate(center,center,resultCenter,Imgproc.TM_SQDIFF_NORMED);
Bitmap bm = Bitmap.createBitmap(resultCenter.cols(), resultCenter.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(resultCenter, bm); //THIS LINE CAUSING APP TO CRASH
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bm);

Related

How to add small bitmap in front of large bitmap image

we need to merge two images with background large image and in front of that a small bitmap and I am using this code then the result is like this what is wrong and how to solve this problem. here the image of the small image is not visible but in small image place a large image is placed
**
Java code
**
Bitmap backgroundimage = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.rec_);
BitmapDrawable mBitmapDrawable= new BitmapDrawable( overlay(bitmap,backgroundimage));
mBitmapDrawable.setGravity(CENTER);
toolbar_layout.setBackground(mBitmapDrawable);
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}

Convert OpenCV mat to Android Bitmap

I am trying to convert an OpenCV mat to android Bitmap but that gives me images with a bluish tint(yellow colour turns to blue)! Even though I am not doing any processing on the image! I have no clue why this is happening. Below is some relevant code:
File file = new File(imgDecodableString);
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
resultBitmap = Bitmap.createBitmap(image.cols(), image.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(image, resultBitmap);
Bitmap mResult = resultBitmap;
ImageView imgView = (ImageView) findViewById(R.id.imgView);
imgView.setImageBitmap(mResult);
//imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
I am new to android app development so I might be missing something simple. Thanks for help!
EDIT:
I am uploading images for reference.
Based on the given suggestions, I made a function which converts Mat to Bitmap. This function works perfectly.!
private static Bitmap convertMatToBitMap(Mat input){
Bitmap bmp = null;
Mat rgb = new Mat();
Imgproc.cvtColor(input, rgb, Imgproc.COLOR_BGR2RGB);
try {
bmp = Bitmap.createBitmap(rgb.cols(), rgb.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgb, bmp);
}
catch (CvException e){
Log.d("Exception",e.getMessage());
}
return bmp;
}
As suspected, the issue is with the RGB color convention, Android follows RGB color convention, but OpenCV follows BGR color convention, You can rectify it using Imgproc.cvtColor(mat, Imgproc.COLOR_BGR2RGBA), before displaying it in the ImageView.

OpenCV Android: Define and save ROI in new Mat

I am trying to slice (crop) part of my image to another so it can be worked on separately. I have found contours and now trying to save every contour in new Mat but it is giving error
Mat crop;
Imgproc.findContours(m, contours, new Mat() ,Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_SIMPLE);
for(int i=0; i <contours.size();i++)
{
Rect rect = Imgproc.boundingRect(contours.get(i));
crop = m.submat(rect);
}
Utils.matToBitmap(crop, bm);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bm);
Here m is my Mat where image is saved
Error:
What I always do in this situation is to create a new mat using the constructor with a rect:
Mat cropped = new Mat(mOriginal, boudingRect);
Edit:
Your bitmap should also have the same size:
bm = Bitmap.createBitmap(crop.size().width,crop.size().height, Bitmap.Config.ARGB_8888);

How to draw detected keypoints on an image in java/javacv?

Can anyone tell me that how can i detect keypoints of an image and draw that keypoints on that image in java?
I tried smt but i couldn't figure out how to draw them?
Any ideas for how should i proceed or any ideas for drawing for my code?
final IplImage image1 = cvLoadImage(
"C:/Users/Can/Desktop/panorama_image1.jpg",
CV_LOAD_IMAGE_GRAYSCALE);
final CanvasFrame canvas1 = new CanvasFrame("Image1");
canvas1.showImage(image1);
canvas1.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
SIFT sift = new SIFT();
KeyPoint keypoint1 = new KeyPoint();
sift.detect(image1, null, keypoint1);
System.out.println("Keypoints for image1: " + keypoint1.capacity());
Assuming you or anyone else still requires this, you can do the following.
Using Java, after you have computed your keypoints you can do the following using the Features2d class in OpenCV.
// draw keypoints on image
Mat outputImage = new Mat();
// Your image, keypoints, and output image
Features2d.drawKeypoints(image, keypoints, outputImage);
String filename = "keypoints.jpg";
System.out.println(String.format("Writing %s...", filename));
Highgui.imwrite(filename, outputImage);
If you or others still need an answer, I believe the possible way of doing that is
opencv_features2d.drawKeypoints(_image1, keypoint1, Mat.EMPTY);
Then you may save your _image1 to file using
ImageIO.write(_image1.getBufferedImage(), "png", new File("image1.png"));
But before that you'll have to open your image1 as a Mat object:
Mat _image1 = new Mat(image1);

How to convert Ipl image to Mat image in java

I am trying to make a mood detection app in opencv, java. But there is a lag in processing the image and then displaying the emoticon. So I want to directly use the image captured , instead of copying the image to the hard disk. For that I need convert iplimage to matimage .
Try
IplImage *ipl_img;
Mat mat_img(ipl_img);
Try this
IplImage img;
bmp = Bitmap.createBitmap(img.width(), img.height(), Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(img.getByteBuffer());
Mat mROI = new Mat(new Size(img.width(), img.height()), CV_8UC4);
Utils.bitmapToMat(bmp, mROI);

Categories

Resources