Ziva
Ziva

Reputation: 3521

OpenCV and JavaCv on Android - detect shape on image

I'm starting using openCV and JavaCV and I try to detect shapes on my Image. I want to detect circles. I have an Image as a bitmap (and only like this). I have read that first I need to take my Image as IplImage, then convert it to grayscale and smooth the edges. Ok, so I started doing something like this: because first my image is as the bitmap i creating an object of IplImage (I should mention that everything is happing after pressing the button):

IplImage image = new IplImage();

Then I'm converting my bitmap to IplImage:

bitmap.copyPixelsToBuffer(image.getByteBuffer());

And i should know make other thinks, but I can't, because after pressing the button application is crashing on the first line : IplImage image = new IplImage(); I tried to use other constructors like: IplImage image = new IplImage(size);but it also didn't work. If some can help me how should I do that (I mean create the IplImage and convert a bitmap to IplImage), I will be very grateful.

---EDIT---- I tried again. And now I'm not adding jar using Project->properties->Java Built Path->... But I'm setting jars inside 'lib' folder. But still everything is crashing when I'm tring to create object of IplImage class. Does anybody know why?

--EDIT-- In my lib folder i have: android-support-v4.jar, javacpp.jar, javacv.jar, opencv library - 2.4.3.jar. My Activity class:

import com.googlecode.javacv.cpp.opencv_core.IplImage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IplImage image = new IplImage();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

} And when I'm starting my activity it' crashing and the log:

02-09 12:18:59.207: E/AndroidRuntime(7652): Caused by: java.lang.UnsatisfiedLinkError: Library jniopencv_core not found
02-09 12:18:59.207: E/AndroidRuntime(7652):     at java.lang.Runtime.loadLibrary(Runtime.java:461)
02-09 12:18:59.207: E/AndroidRuntime(7652):     at java.lang.System.loadLibrary(System.java:557)
02-09 12:18:59.207: E/AndroidRuntime(7652):     at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:448)
02-09 12:18:59.207: E/AndroidRuntime(7652):     at com.googlecode.javacpp.Loader.load(Loader.java:372)
02-09 12:18:59.207: E/AndroidRuntime(7652):     at com.googlecode.javacpp.Loader.load(Loader.java:319)
02-09 12:18:59.207: E/AndroidRuntime(7652):     at com.googlecode.javacv.cpp.opencv_core.<clinit>(opencv_core.java:136)
02-09 12:18:59.207: E/AndroidRuntime(7652):     ... 19 more

I can mention that if I tried to define Imgproc img = new Imgproc();everything was ok. Maybe I can detect shape (circles) using Imgproc, can someone recommend a good tutorial?

Upvotes: 2

Views: 4148

Answers (1)

iTech
iTech

Reputation: 18460

You can create the image directly as following:

IplImage image = IplImage.createFrom(bitmapBuffer);

Or

IplImage image = IplImage.create(width, height, IPL_DEPTH_8U, 4); // Change the parameter as you need
bitmap.copyPixelsToBuffer(image.getByteBuffer());

Or load the image directly into LplImage object

IplImage image = cvLoadImage("image.png");

Edit:

See this question, it should solve your error.

Based on your error, it seems that you did not include the native libraries (e.g. libjniopencv_core.so) of OpenCV in your libs folder.

Follow the instruction guide to install and use the library properly as well as the README.txt file

See this question

Upvotes: 2

Related Questions