Imgproc.FindContours doesnt fill List<MatOfPoint> of Contours Opencv Android

asked 2015-10-21 04:22:14 -0500

VeTaLio gravatar image

updated 2018-02-02 17:12:08 -0500

i'm using OpenCV 2.4.11 for Android(Xamarin).I'm trying to do detect rect object(Paper sheet) and to make wrapPerspective,smiliar this tutorial but for Android, like this steps :
Canny edge -> Largest contour -> largest rectangle -> find corners -> perspective change.

using (Bitmap _img = BitmapFactory.DecodeFile(App._file.Path))
                if (_img != null)
                    m = new Mat();
                    grayM = new Mat();

                    Utils.BitmapToMat(_img, m);
                    //apply filter
                    Imgproc.Canny(m, m, 100, 100,3,true);
                    //gaus Blur
                    Imgproc.GaussianBlur(m,m,new Org.Opencv.Core.Size(5,5),5);
                    //list for contours
                    List<MatOfPoint> Contours = new List<MatOfPoint>();
                    Mat hierarcy = new Mat();
                   //our method to find contours,via filling List(Contours)
                    Imgproc.FindContours(m , Contours, hierarcy, Imgproc.RetrList, Imgproc.ChainApproxSimple);

                    System.Console.WriteLine(Contours.Count + " Contours founded");
                   //Dont know why,but Contours list is always empty(no values)
                    if (Contours.Count != 0)
                        MatOfPoint temp = Contours[0];

I'm stuck here, because i don't understand why this method(Imgproc.FindContours) doesnt fill my list of contours(always empty).

Also strange glitch:
this is an source image #1 Source Image
if i using only these methods Imgproc.Canny and Imgproc.GaussianBlur then result is like this(contours marked well) Canny with GaussianBlur

otherwise,when i add Imgproc.FindContours,result is worst than w/o this method(and as i said,doesnt fill my list of contours):
With Imgproc.FindContours

Another image source #2:
Source Image 2
w/o Imgproc.FindContours(only Imgproc.Canny with Imgproc.GaussianBlur) :
w/o FindContours
with FindContours method :
with Contours

Dont understand where is my mistake. Can someone explain for me,where i doing wrong OR how can i achieve my goal?
Any help will be appreciated,thanks!

edit retag flag offensive close merge delete



rgbimg -> grayscale -> Canny -> findContours

it looks like your Canny comes from rgb image ?

berak gravatar imageberak ( 2015-10-21 05:45:28 -0500 )edit

@berak yes,i make simple photo via camera of android phone,then i convert to bitmap-> after that i convert to Mat;

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

try a Imgproc.cvtColor(m,m,Imgproc.COLOR_BGR2GRAY); before the canny

berak gravatar imageberak ( 2015-10-21 06:50:49 -0500 )edit

@berak i tried now(also,tried before). Anyway list is empty(no points of contours founded),but to be honest,now if i use w/ Imgproc.FindContours image look a little bit better(i can see better contours).

VeTaLio gravatar imageVeTaLio ( 2015-10-21 07:07:09 -0500 )edit

I experienced that OpenCV findcontours works best over binary images. So maybe first make a binary image with only 0 and 255 values.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-21 08:09:14 -0500 )edit

@StevenPuttemans i tried and again List is empty...That is sad news :(

VeTaLio gravatar imageVeTaLio ( 2015-10-21 08:36:43 -0500 )edit

Org.Opencv.Core does not even exist, it's org.opencv.core. (and a lot more typos like this above)

don't throw code at us, which won't ever compile. please don't be so sloppy.

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

no, and you probably should avoid xamarin, until you get it to run from straight java

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

again, if it turns out to be a problem with the (3rd party, unsupported by opencv) xamarin lib, all of this is off-topic, and will get closed.

berak gravatar imageberak ( 2015-10-23 02:51:47 -0500 )edit

Use JavaList<matofpoint> instead of List<matofpoint>.

Josa gravatar imageJosa ( 2016-01-19 09:18:13 -0500 )edit