Finding Area of Convex Hull Gives an Assertion Error

asked 2017-07-21 01:57:03 -0600

nish gravatar image

updated 2017-07-21 14:43:58 -0600

I'm very new to image processing.I'm trying to calculate the solidity in opencv on contours of an image. But contourArea() method in my code gives an assertion error that is:

"OpenCV Error: Assertion failed (contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || contour.depth() == CV_32S)) in cv::contourArea").

How can I fix it. Thank You.

import static com.googlecode.javacv.cpp.opencv_imgproc.CV_THRESH_BINARY; import java.util.ArrayList; import java.util.List; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfInt; import org.opencv.core.MatOfPoint; import org.opencv.core.Scalar; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; import static org.opencv.imgproc.Imgproc.contourArea; import static org.opencv.imgproc.Imgproc.findContours;

public class Solidity { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat image = Highgui.imread("D:\MyImage.bmp");

    if (image.empty() == true) {
        System.out.println("Error: no image found!");
    } else {

        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat image_2 = new Mat();


        Imgproc.cvtColor(image, image_2, Imgproc.COLOR_BGR2GRAY);
        Imgproc.threshold(image_2, image_2, 150, 255, CV_THRESH_BINARY);

        findContours(image_2, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);           


        for (int i = 0; i < contours.size(); i++) {
            Imgproc.drawContours(image, contours, -1, new Scalar(240, 0, 0), 2);

            double area=contourArea(contours.get(i));

            MatOfInt matOfInt=new MatOfInt();

            Imgproc.convexHull(contours.get(i),matOfInt);

            //Mat conMat=new Mat(contours.get(i).size(),CV_32FC1);
            //Error comes from here
            double hullArea=contourArea(matOfInt);

            double solidity=area/hullArea;

            System.out.println(solidity);
        }

    }

}

}

edit retag flag offensive close merge delete

Comments

opencv version?

LBerger gravatar imageLBerger ( 2017-07-21 02:01:21 -0600 )edit

version 2.4.11

nish gravatar imagenish ( 2017-07-21 02:15:13 -0600 )edit