Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an image using OpenCV in Android

I apologize if this question has already been covered on this site, but I can't seem to find a straight forward answer. I'm making an android app that uses OpenCV to take a picture, process it to detect objects, and figure out the coordinates of those objects in the scene.

All the tutorials I find for android seem to be real time processing of the camera preview or using c++, which I would strongly prefer to not have to go down the c++ road. I'm sure I'm missing something simple but I don't know what it is.

On another note, the objects I'm trying to detect are billiards balls on a table. What would be the best pre-processing technique to better detect the balls? I did a quick test using the canny method, and it seems that the light reflecting off the balls breaks up the circle shape.

like image 712
vince88 Avatar asked Nov 15 '11 04:11

vince88


People also ask

Can we use OpenCV for Android app?

The below steps for using Android OpenCV sdk in Android Studio. This is a simplified version of this SO answer. Download latest OpenCV sdk for Android from OpenCV.org and decompress the zip file. Import OpenCV to Android Studio, From File -> New -> Import Module, choose sdk/java folder in the unzipped opencv archive.

What is the OpenCV routine for reading an image from a file?

What is the OpenCV routine for reading an image from a file? imread() Loads an image from a file. The function imread loads an image from the specified file and returns it.


2 Answers

I know it's too late, but someone may use this.

Highgui has been removed from opencv for android.

You can use Imgcodes instead.

Mat BGRMat = Imgcodecs.imread(getResources().getDrawable(R.drawable.img).toString());
like image 136
Mehmet Taha Meral Avatar answered Oct 14 '22 20:10

Mehmet Taha Meral


This works for me (got the answer from here: How to get Mat with a drawable input in Android using OpenCV)

Mat img = null;
try {
    img = Utils.loadResource(this, R.drawable.image_id, CvType.CV_8UC4);
} catch (IOException e) {
    e.printStackTrace();
}
like image 45
syonip Avatar answered Oct 14 '22 19:10

syonip