-1

I would like to find all vertex (e.g. return x, y positions) for the black object. I will use Java and JavaCV to implements. Is there any API or algorithm can help?

Sorry for not enough reputation to post images. I post the link here.

The original image like this:

https://i.stack.imgur.com/geubs.png

The expected result like this:

https://i.stack.imgur.com/MA7uq.png

Web_Designer
  • 72,308
  • 93
  • 206
  • 262

2 Answers2

0

OpenCV allows you to take a binary image and carry out contour analysis.

http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html

You could use findContours to find all of the contours (all of the edge points) then simply average them or pick and choose the ones that suit your purpose.

Here is a good example for JavaCV..

opencv/javacv: How to iterate over contours for shape identification?

Community
  • 1
  • 1
Saharsh Bishnoi
  • 341
  • 2
  • 10
0

Here is what you should do (for explanation, see comments with code),

CODE

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

// Load the image
String path = "/home/bikz05/Desktop/geubs.png";
Mat original = Highgui.imread(path);
Mat image = new Mat();
Imgproc.cvtColor(original, image, Imgproc.COLOR_BGR2GRAY);

// Threshold the image
Mat threshold = new Mat();
Imgproc.threshold(image, threshold, 127, 255, 1);

// Find the contours
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(threshold, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

// Get contour index with largest area
double max_area = -1;
int index = 0;
for(int i=0; i< contours.size();i++) {
    if (Imgproc.contourArea(contours.get(i)) > max_area) {
        max_area = Imgproc.contourArea(contours.get(i));
        index = i;
    }   
}

// Approximate the largest contour
MatOfPoint2f approxCurve = new MatOfPoint2f();
MatOfPoint2f oriCurve = new MatOfPoint2f( contours.get(index).toArray() );
Imgproc.approxPolyDP(oriCurve, approxCurve, 6.0, true);

// Draw contour points on the original image
Point [] array = approxCurve.toArray();
for(int i=0; i < array.length;i++) {
    Core.circle(original, array[i], 2, new Scalar(0, 0 ,255), 2);
}

INPUT IMAGE
enter image description here

OUTPUT IMAGE

enter image description here

bikz05
  • 1,575
  • 12
  • 17
  • I am facing a problem that "MatOfPoint cannot be resolved to a type" & "MatOfPoint2f cannot be resolved to a type". is it caused by different JavaCV version? I am using JavaCV 0.9 and OpenCV 2.3.1 I imported these API import static org.bytedeco.javacpp.opencv_core.*; import static org.bytedeco.javacpp.opencv_highgui.*; import static org.bytedeco.javacpp.opencv_imgproc.*; import static org.bytedeco.javacpp.opencv_ml.*; Thanks for your help. – user3526130 Oct 25 '14 at 13:32
  • I have used the OpenCV Java bindings, not JavaCV. There, you need to `import` - `org.opencv.core.MatOfPoint` and `org.opencv.core.MatOfPoint2f`. – bikz05 Oct 25 '14 at 13:45
  • I found that there are two ways to develop OpenCV in Java. The first way is to follow this guide: http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html The second way is to following this: https://github.com/bytedeco/javacv I don't know what is the difference between them. But, currently, I am using the second way. And I haven't API org.opencv.core – user3526130 Oct 25 '14 at 13:48
  • This code will not work with JavaCV, but the idea remains the same. You can follow the steps I mentioned in the code. This post, http://stackoverflow.com/questions/11388683/opencv-javacv-how-to-iterate-over-contours-for-shape-identification will also be helpful. – bikz05 Oct 25 '14 at 13:56
  • Thanks. I still want to know that which one is better? JavaCV or OpenCV Java bindings. – user3526130 Oct 25 '14 at 14:06
  • I will recommend you to use OpenCV Java bindings. OpenCV Java bindings are used in Android application development. Moreover, I personally prefer them over JavaCV since it is an official release and it is easier to code with OpenCV Java bindings. – bikz05 Oct 25 '14 at 14:13
  • Thanks a lot. Let's me try OpenCV Java Bindings later! – user3526130 Oct 26 '14 at 09:16
  • Your method works very well. Thanks for your help again. – user3526130 Oct 26 '14 at 09:47