Ask Your Question

nish's profile - activity

2020-09-07 04:17:05 -0600 received badge  Popular Question (source)
2020-09-07 04:17:04 -0600 received badge  Popular Question (source)
2017-07-25 02:27:44 -0600 received badge  Enthusiast
2017-07-21 14:43:58 -0600 received badge  Editor (source)
2017-07-21 02:15:13 -0600 commented question Finding Area of Convex Hull Gives an Assertion Error

version 2.4.11

2017-07-21 01:57:03 -0600 asked a question Finding Area of Convex Hull Gives an Assertion Error

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

    }

}

}

2017-07-18 03:38:51 -0600 commented answer OpenCV Error in findContours()

Both are 0 :(

2017-07-18 01:24:02 -0600 commented answer OpenCV Error in findContours()

I tried with that flag also but same error is coming. Thank You @berak for answering my question.

2017-07-18 00:25:25 -0600 asked a question OpenCV Error in findContours()

I'm trying to find the contours of an image. Though I tried in different ways, same error (that is "Unsupported format or combination of formats ([Start] FindContours support only 8uC1 and 32sC1 images) in cvStartFindContours") is given. This is my source code. How can I fix this problem. Thank you.

public class ContourDetection {

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

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

        List<MatOfPoint> contours = new ArrayList<>();
        Mat image_2 = new Mat(image.size(), image.depth());

        //image.convertTo(image_2, CvType.CV_32SC1);
        Imgproc.cvtColor(image, image_2, CvType.CV_8UC1);
        Imgproc.threshold(image_2, image_2, 150, 255, CV_THRESH_BINARY);

        //System.out.println(image_2.channels() + " " + image_2.depth() + CvType.CV_8U+image_2.type());
        findContours(image_2, contours, new Mat(), Imgproc.RETR_FLOODFILL, Imgproc.CHAIN_APPROX_SIMPLE);

        // Draw all the contours such that they are filled in.
        Mat contourImg = new Mat(image.size(), image.type());
        for (int i = 0; i < contours.size(); i++) {
            Imgproc.drawContours(contourImg, contours, i, new Scalar(255, 255, 255), -1);
        }

        Highgui.imwrite("D:\\debug_image.bmp", contourImg); 
    }
}

}