Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,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.

What I have tried:

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);
            }

        }

    }
}
Posted
Comments
Richard MacCutchan 22-Jul-17 4:01am    
Which line causes the error?
vino.A 24-Jul-17 2:52am    
double hullArea=contourArea(matOfInt); //this line
Richard MacCutchan 24-Jul-17 3:05am    
Then you need to check the documentation to see what is missing or incorrect in the parameters that you are passing to the contourArea method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900