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.

1
  • 1
    Try looking into HoughCircles Commented Nov 15, 2011 at 6:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 where did you find the javacv tutorials? And could you also tell me how to set it up on windows? Thanks.
Also after declaring the Mat data type what do I have to do to display it?
hi @garak, when I use Mat m = Highgui.imread("/media/path_to_image"); , it returns "!_src.empty()" when converting colors. can you help me?
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();
}

1 Comment

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?
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());

3 Comments

not too late, helped me! getDrawable is deprecated so you can use: Mat BGRMat = Imgcodecs.imread(ResourcesCompat.getDrawable(getResources(), R.drawable.img, null).toString());
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 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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.