I'm working on an app that detects OpenCV's Aruco markers live using the camera. The below java code is used to preprocess the image, detect markers, and finally highlight and display them on in an ImageView on the screen of the phone.
//Rotate to correct orientation
Mat frame = rotateMat(imageProxyToMat(imageProxy), 270);
List<Mat> markerCorners = new ArrayList<>();
Mat markerIds = new Mat();
Mat greyImage = new Mat();
//Convert to greyscale
Imgproc.cvtColor(frame, greyImage, Imgproc.COLOR_BGR2GRAY);
//Increase contrast
Mat contrastGreyImage = new Mat();
Core.normalize(greyImage, contrastGreyImage, 0, 255, Core.NORM_MINMAX);
//Apply gaussian blur
Mat gaussianBlurImage = new Mat();
Imgproc.GaussianBlur(contrastGreyImage, gaussianBlurImage, new Size(5, 5), 0);
//Apply binary threshold to better find markers
Mat thresholdedFrame = new Mat();
Imgproc.adaptiveThreshold(contrastGreyImage, thresholdedFrame, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 15, 5);
Mat imageToDetect = gaussianBlurImage;
Imgcodecs.imwrite(getFilesDir() + "/imageToDetect.jpg", imageToDetect);
//Detect markers in the current frame
arucoDetector.detectMarkers(imageToDetect, markerCorners, markerIds);
Objdetect.drawDetectedMarkers(frame, markerCorners, markerIds);
//Convert to bitmap
Bitmap bitmap = Bitmap.createBitmap(frame.cols(), frame.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(frame, bitmap);
//Display on the ImageView
liveImageView.setImageBitmap(bitmap);
However when this is displayed in the imageView (layout code below), it simply shows up being coloured entirely green, and does not fit to the screen the way the ImageView does.
ImageView Layout code:
<ImageView
android:id="@+id/liveImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="@+id/previewView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_foreground" />
This is the output I get on my phone screen when I load the app:
I'm working on an app that detects OpenCV's Aruco markers live using the camera. The below java code is used to preprocess the image, detect markers, and finally highlight and display them on in an ImageView on the screen of the phone.
//Rotate to correct orientation
Mat frame = rotateMat(imageProxyToMat(imageProxy), 270);
List<Mat> markerCorners = new ArrayList<>();
Mat markerIds = new Mat();
Mat greyImage = new Mat();
//Convert to greyscale
Imgproc.cvtColor(frame, greyImage, Imgproc.COLOR_BGR2GRAY);
//Increase contrast
Mat contrastGreyImage = new Mat();
Core.normalize(greyImage, contrastGreyImage, 0, 255, Core.NORM_MINMAX);
//Apply gaussian blur
Mat gaussianBlurImage = new Mat();
Imgproc.GaussianBlur(contrastGreyImage, gaussianBlurImage, new Size(5, 5), 0);
//Apply binary threshold to better find markers
Mat thresholdedFrame = new Mat();
Imgproc.adaptiveThreshold(contrastGreyImage, thresholdedFrame, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 15, 5);
Mat imageToDetect = gaussianBlurImage;
Imgcodecs.imwrite(getFilesDir() + "/imageToDetect.jpg", imageToDetect);
//Detect markers in the current frame
arucoDetector.detectMarkers(imageToDetect, markerCorners, markerIds);
Objdetect.drawDetectedMarkers(frame, markerCorners, markerIds);
//Convert to bitmap
Bitmap bitmap = Bitmap.createBitmap(frame.cols(), frame.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(frame, bitmap);
//Display on the ImageView
liveImageView.setImageBitmap(bitmap);
However when this is displayed in the imageView (layout code below), it simply shows up being coloured entirely green, and does not fit to the screen the way the ImageView does.
ImageView Layout code:
<ImageView
android:id="@+id/liveImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="@+id/previewView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_foreground" />
This is the output I get on my phone screen when I load the app:
Share Improve this question edited Jan 19 at 10:52 Christoph Rackwitz 15.6k5 gold badges39 silver badges51 bronze badges asked Jan 18 at 11:48 OliOli 316 bronze badges 1- all the stuff coming after image acquisition is a distraction. debug that first. reduce your code. minimal reproducible example – Christoph Rackwitz Commented Jan 18 at 14:19
1 Answer
Reset to default 0Looks like you are missing some color conversions.
The Utils.matToBitmap()
method expects RGB images, but according to the line:
Imgproc.cvtColor(frame, greyImage, Imgproc.COLOR_BGR2GRAY)
you frame is BGR.
By default an ImageView
scales its contents to ScaleType.FIT_CENTER
. If you want to change the behavior you can set a different scale type.