Here is a small piece of code from my android app using Opencv (for Google Glass). I am trying to turn an image (at location picturePath) from colour to grayscale and then overwrite the original colour image. As it stands, this code saves an image in memory that is half grayscale as it should be, and half completely black:
private void rGBProcessing (final String picturePath, Mat image) {
//BitmapFactory Creates Bitmap objects from various sources,
//including files, streams, and byte-arrays
Bitmap myBitmapPic = BitmapFactory.decodeFile(picturePath);
image = new Mat(myBitmapPic.getWidth(), myBitmapPic.getHeight(), CvType.CV_8UC4);
Mat imageTwo = new Mat(myBitmapPic.getWidth(), myBitmapPic.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(myBitmapPic, image);
Imgproc.cvtColor(image, imageTwo, Imgproc.COLOR_RGBA2GRAY);
//Highgui.imwrite(picturePath, imageTwo);
Utils.matToBitmap(imageTwo, myBitmapPic);
FileOutputStream out = null;
try {
out = new FileOutputStream(picturePath);
myBitmapPic.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Can anybody please explain to me why the image produced is completely black on one half and advise me on a correction. It seems to me that since the process of image manipulation has been partially completed, that perhaps there is an issue with one piece of the code not being completed before the next code starts? Any help is appreciated. Cheers!
Update: Here is what I'm seeing:
The picture on Google Glass was half black but this upload just cuts off the bottom half. Then a little while later, even after the app is no longer on the screen (I don't know how to stop debugging though so it could still be working in the background. I then get the full grayscale image. Can someone explain what is going on and give me a potential fix please?