EnoxSoftware / OpenCVForUnity

OpenCV for Unity (Untiy Asset Plugin)
https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088
557 stars 175 forks source link

CVException when find contours() #58

Closed albrrt closed 4 years ago

albrrt commented 4 years ago

I am trying to findCountours of a threshold mat using

List <MatOfPoint> contours = new List<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(invertedBinaryMat, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);

But I'm getting the following error:

CvException: CvType.CV_32SC2 != m.type() ||  m.cols()!=1
Mat [ 0*0*CV_8UC1, isCont=False, isSubmat=False, nativeObj=0x105553224793376, dataAddr=0x0 ]
OpenCVForUnity.UtilsModule.Converters.Mat_to_vector_Mat (OpenCVForUnity.CoreModule.Mat m, System.Collections.Generic.List`1 mats)
OpenCVForUnity.UtilsModule.Converters.Mat_to_vector_vector_Point (OpenCVForUnity.CoreModule.Mat m, System.Collections.Generic.List`1 pts)
OpenCVForUnity.ImgprocModule.Imgproc.findContours (OpenCVForUnity.CoreModule.Mat image, System.Collections.Generic.List`1 contours, OpenCVForUnity.CoreModule.Mat hierarchy, Int32 mode, Int32 method)

invertedBinaryMat is a Mat obtained via Imgproc.threshold (srcMat, invertedBinaryMat, 100, 300, Imgproc.THRESH_BINARY_INV);

I am using the OpenCV for Unity trial on Unity 2018.2.20f1 and MacOS 10.14.6 before purchasing the asset.

What does this error mean and what can I do to solve it? Thank you

albrrt commented 4 years ago

I solved it by adding Utils.setDebugMode(true, false); before the issue line and reading the log in the console:findCountours() only accepts CvType.CV_8UC1 and my Mat was CV_8UC4...

Leaving the solution here in case someone runs into a similar issue