12

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.

vince88
  • 3,269
  • 5
  • 27
  • 27

3 Answers3

16

To load images in Android, you can use

Bitmap bMap=BitmapFactory.decodeResource(getResources(),R.drawable.image1)

where image1 is a image under Resources folder of your Android project. Then convert Bitmap to bytes or Mat and process in C++ (OpenCV) or Java with matToBitmap or MatToBitmap methods in android-opencv.

If you are comfortable in processing using Mat data type, you can use (you need android-opencv project to use this)

Mat m = Highgui.imread("/media/path_to_image");

You can use blobs, hough circles to detect billiard balls. There is no certain way to answer this. You can try using normalized RGB or Lab colorspaces to play around (to remove reflection of the billiard balls) to see if you detect billiard balls (by running hough circles, blob detectors).

The real-time scenario will require you to use machine-learning techniques to detect billiard balls by using certain feature vectors, then do training and testing.

garak
  • 4,713
  • 9
  • 39
  • 56
  • Thank you, I was able to figure it out, but I also found out about javacv which stays more true to the native opencv code, thus making it easier to follow along with other tutorials. – vince88 Nov 17 '11 at 21:53
  • @vince88 where did you find the javacv tutorials? And could you also tell me how to set it up on windows? Thanks. – praxmon Jan 16 '14 at 07:58
  • Also after declaring the Mat data type what do I have to do to display it? – praxmon Jan 16 '14 at 09:02
  • hi @garak, when I use Mat m = Highgui.imread("/media/path_to_image"); , it returns "!_src.empty()" when converting colors. can you help me? – Hack Dawg Dec 19 '19 at 00:25
8

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();
}
Community
  • 1
  • 1
syonip
  • 2,762
  • 25
  • 27
  • Hello, I used this code in order to load an RGB image, but for some reson the `Mat` type was `CV_8UC1`. any ideas why is that? – noamgot Dec 12 '17 at 07:40
5

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());
Mehmet Taha Meral
  • 3,315
  • 28
  • 30
  • not too late, helped me! getDrawable is deprecated so you can use: Mat BGRMat = Imgcodecs.imread(ResourcesCompat.getDrawable(getResources(), R.drawable.img, null).toString()); – syonip Oct 03 '16 at 08:42
  • 1
    actually i couldn't make this work, because a drawable resource doesn't have a path imread can use. toString() just returns the name of the resource... – syonip Oct 03 '16 at 09:27
  • @syonip I see your code above, and there are some mistakes :) Please make sure to copy code correctly. For example after getResources() method, you should put .(dot) instead of ,(comma). Good luck... – Mehmet Taha Meral Oct 03 '16 at 23:03