Ask Your Question
-1

Provided data element number (0) should be multiple of the Mat channels count (1) Android OpenCV

asked 2015-10-21 09:32:08 -0500

VeTaLio gravatar image

updated 2018-02-02 17:07:30 -0500

I'm trying to find 4 corners from rect object(paper sheet).

                Mat source = new Mat();

                Org.Opencv.Core.Point center;

               public GetCorners(Bitmap _sourceImg)
                {
                    Utils.BitmapToMat(_sourceImg, source);
                }
               //find corners
                public void FindCorners()
                {
                    center = new Org.Opencv.Core.Point(0, 0);
                    //Mat source = new Mat();

                    if (source == null)
                    {
                        Console.WriteLine("No IMG");
                        return;
                    }

                    Mat BlackWhite = new Mat();

                    Imgproc.CvtColor(source, BlackWhite, Imgproc. ColorBgr2gray);  //ColorBgra2gray, 4);

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

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

                    Mat Lines = new Mat();
                    int treshold = 70;
                    int minLinsize = 30;
                    int lineGap = 10;

                    Imgproc.HoughLinesP(BlackWhite, 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] = source.Cols();
                        Value[3] = ((float)Vector[1] - Vector[3]) / (Vector[0] - Vector[2]) * (source.Cols() - Vector[2]) + Vector[3];

                        Lines.Put(0, i, Value);
                    }


                    Console.WriteLine("##KQuantity {0} Finded##",Lines.Cols());
                    List<Org.Opencv.Core.Point> Corners = new List<Org.Opencv.Core.Point>();

                    for (int i = 0; i < Lines.Cols(); i++)
                    {
                        for (int j = 0 ; i < Lines.Cols(); j++)
                        {
                            Mat m1 = new Mat(),
                            m2 = new Mat();
                            double[] d1 = Lines.Get(0, i);
                            double[] d2 = Lines.Get(0, j);
                            m1.Put(0, j, d1);
                            m2.Put(0, j, d2);
                    try
                    {
                    //i'm getting exception here
                    Org.Opencv.Core.Point pt = ComputeInteresect(Lines.Get(0, i), Lines.Get(0, j)); //(m1, m2);

                        if (pt.X >= 0 && pt.Y >= 0)
                        {
                            Corners.Add(pt);
                            Console.WriteLine ("dobavleno {0} koordinat",Corners.Count);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }     
                        }
                    }

and also i got a method that calculate intersects :

static Org.Opencv.Core.Point ComputeInteresect(double[] a, double[] b) //(Mat es,Mat es2)// 
                {
                    double x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];
                    double denom = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4));
                    Org.Opencv.Core.Point pt = new Org.Opencv.Core.Point();
                    if (denom != 0)
                    {

                        pt.X = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2)
                            * (x3 * y4 - y3 * x4))
                            / denom;
                        pt.Y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2)
                            * (x3 * y4 - y3 * x4))
                            / denom;
                        return pt;
                    } 
                    else
                        return new Org.Opencv.Core.Point(-1, -1);
                }

and i don't understand why,but i'm getting this exception :


java.lang.UnsupportedOperationException: Provided data element number (0) should be multiple of the Mat channels count (1)


I found,that this problem occurs,when image is an RGB Format(and have 4 channels),but at first,i'm converting to gray(1channel), via this method:

Imgproc.CvtColor(source, BlackWhite, Imgproc. ColorBgr2gray);  //ColorBgra2gray, 4);

Any help will be appreciated,thanks!

edit retag flag offensive close merge delete

Comments

please tag your question properly as xamarin / c#, add a warning to noobs, that this is not java code, that they can try.

berak gravatar imageberak ( 2015-10-23 23:49:10 -0500 )edit

@berak again downvote. There is no point to come here....

VeTaLio gravatar imageVeTaLio ( 2015-10-24 06:36:10 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-10-23 14:41:24 -0500

VeTaLio gravatar image

Finally, i changed some part of code and its works:

Console.WriteLine("##Quantity {0} Founded##",Lines.Cols());
List<Org.Opencv.Core.Point> Corners = new List<Org.Opencv.Core.Point>();

for (int i = 0; i < Lines.Cols(); i++)
{
    for (int j = i+1 ; i < Lines.Cols(); j++)
    {

    try
    {

        Org.Opencv.Core.Point pt = ComputeInteresect(Lines.Get(0, i), Lines.Get(0, j));

        if (pt.X >= 0 && pt.Y >= 0)
        {
            Corners.Add(pt);
            Console.WriteLine ("dobavleno {0} koordinat",Corners.Count);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }     
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-10-21 09:32:08 -0500

Seen: 369 times

Last updated: Oct 24 '15