Opencv Android ConvertTo() doesn't convert to Cv32SC2 type

asked 2015-10-22 15:44:15 -0500

VeTaLio gravatar image

updated 2015-10-22 15:45:13 -0500

i'm using OpenCV 2.4.11 for Android(Xamarin).
I want to find contours of rect object(paper sheet).Unfortunately method FindContours() doesn't fill my List of contours(List<matofpoint> of contours),so i found an alternative: via method HoughLinesP().

Main idea is :

  1. Apply filter,like: Cvtcolor -> Blur -> Canny
  2. Apply method HoughLinesP() and fill new Mat with this founded lines
  3. Convert Mat to List<matofpoint> via Converters

So all going fine,but on step 3 i got this exception:

java.lang.IllegalArgumentException: CvType.CV_32SC2 != m.type() || m.cols()!=1 [MonoDroid] Mat [ 117CV_32SC4, isCont=true, isSubmat=false, nativeObj=0xffffffff970403c0, dataAddr=0xffffffff970bc110 ]

So i checked this two conditions:

  1. cols are > then 1
  2. after HoughLinesP() filled Mat with data have type cv_32sc4

After that,i tried to use ConverterTo and Convert my Mat(filled with data) to cv_32sc2

Lines.ConvertTo(Lines, CvType.Cv32sc2);

And then via Converters, convert from Mat to List<matofpoint>

Converters.Mat_to_vector_vector_Point(Lines, Contours);

Don't know why,but this method ConvertTo() doesnt convert properly to CvType.Cv32sc2,because mat remains with CvType.Cv32sc4

Also,my source code:

Imgproc.CvtColor(SourceImage, SourceImage, Imgproc.ColorBgra2gray, 4); //ColorBgr2gray);

Imgproc.Blur(SourceImage, SourceImage, new Size(3, 3));

Imgproc.Canny(SourceImage, SourceImage, 100, 100, 3, true);

Mat Lines = new Mat();
int treshold = 70;
int minLinsize = 30;
int lineGap = 10;
IList<MatOfPoint> Contours = new List<MatOfPoint>();
Imgproc.HoughLinesP(SourceImage,Lines, 1, Math.PI / 180, treshold, minLinsize, lineGap);

for (int i = 0; i < Lines.Cols(); i++)
    double[] Vector = Lines.Get(0, i);
    double[] Value = new double[4];

    Value[0] = 0;
    Value[1] = ((float) Vector[1] - Vector[3]) / (Vector[0] - Vector[2]) * -Vector[0] + Vector[1];
    Value[2] = SourceImage.Cols();
    Value[3] = ((float)Vector[1] - Vector[3]) / (Vector[0] - Vector[2]) * (SourceImage.Cols() - Vector[2]) + Vector[3];

    Lines.Put(0, i, Value);

double MaxArea = -1;
int MaxAreaIdx = -1;

Lines.ConvertTo(Lines, CvType.Cv32sc2);
Console.WriteLine("Quantity founded {0} Cols and CvType equals {1}!",Lines.Cols(),Lines.Type());
//here's crashes!
Converters.Mat_to_vector_vector_Point(Lines, Contours);

Where is my mistake? Thanks.

edit retag flag offensive close merge delete


your general idea looks quite broken here.

  • whatever you do to the HoughLinesP output, you can't achieve the same result as findContours(), some isolated lines do not make a list of contours.
  • convertTo() only changes the depth, not the number of channels
berak gravatar imageberak ( 2015-10-22 22:34:13 -0500 )edit

again, your code is full of typos, and will never compile as is.

if you want us to help, put more effort into this

berak gravatar imageberak ( 2015-10-23 00:50:54 -0500 )edit

@berak please read more attentively. I'm using XAMARIN(C#) and we have there UpperCase(all similar to java code,but UpperCase) and this code is not broken. See attached image:

VeTaLio gravatar imageVeTaLio ( 2015-10-23 02:22:44 -0500 )edit

apologies, i did not get the c# connection

berak gravatar imageberak ( 2015-10-23 02:43:15 -0500 )edit