Bruce Bruce - 5 months ago 71
Java Question

Can't load image with OpenCV

I tried to use

Imgcodecs.imread
from OpenCV on Android to an load image which is located in the drawable folder, but when I check the data of the image, it is
null
.

This is my simple code to load image:

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mImageRGBA = inputFrame.rgba();

img_object = Imgcodecs.imread("D:\\Project_Android\\Car_Tracking_1\\app\\src\\main\\res\\drawable\\rear_of_car.png");
Log.i("aaa2", img_object.dump());

return mImageRGBA;
}


This is the logcat info from that function:

06-26 00:27:15.056 24939-25059/com.example.bruce.car_tracking_1 I/aaa2: []


enter image description here

What am I doing wrong here? Am I finding the path incorrectly? Or am I loading the image incorrectly?

Answer

to be sure that your path is valid try to write a test image

Mat test = new Mat(3,3,0);
Imgcodecs.imwrite("D:\\Project_Android\\Car_Tracking_1\\app\\src\\main\\res\\drawable\\test.png",test);

if you can save the image you can also load it.

edit: also you can try like

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mImageRGBA = inputFrame.rgba();

img_object = Imgcodecs.imread("D:\\Project_Android\\Car_Tracking_1\\app\\src\\main\\res\\drawable\\rear_of_car.png");
Imgcodecs.imwrite("D:\\Project_Android\\Car_Tracking_1\\app\\src\\main\\res\\drawable\\test.png",img_object);
Log.i("aaa2", img_object.dump());

return mImageRGBA;

}