Find Centroid

Ask question on using Emgu CV with C#
Post Reply
Geaper
Posts: 10
Joined: Tue Dec 22, 2015 10:51 am

Find Centroid

Post by Geaper » Tue Dec 22, 2015 10:53 am

Hello Guys.

I want to detect a blue square and find his contour and centroid. Any idea how can I can do such thing?

Thanks in advance

Geaper
Posts: 10
Joined: Tue Dec 22, 2015 10:51 am

Re: Find Centroid

Post by Geaper » Tue Dec 22, 2015 11:00 am

btw, i dont have emgu 3.0. it's 2.9. I don't have VectorOfPoints function.

is it possible to do it without 3.0?

mcgrotts
Posts: 4
Joined: Tue Jun 30, 2015 2:55 pm

Re: Find Centroid

Post by mcgrotts » Tue Dec 22, 2015 12:09 pm

The best way is to calculate the moments of your image using moments = CvInvoke.Moments(Image.Mat, false); The first parameter is the image the second is if you want it to use binary moments (geometric) or non binary (weighted where higher pixel intensities attract the centroid more).

After getting the moments you can find the centroids X value using xpoint = (moments.M10/moments.00) and Y with ypoint = (moments.M01/moments.M00).

The the centroid is = new Point(xpoint,ypoint)

I hope this helps and here is some code as well:

Code: Select all

moments = CvInvoke.Moments(image.Mat, false);
WeightedCentroid = new Point((int)(moments.M10 / moments.M00), (int)(moments.M01 / moments.M00));

Geaper
Posts: 10
Joined: Tue Dec 22, 2015 10:51 am

Re: Find Centroid

Post by Geaper » Tue Dec 22, 2015 6:17 pm

love you bro (no homo) but which type is variable "moments" and WeightedCentroid?? Images?
Last edited by Geaper on Tue Dec 22, 2015 6:42 pm, edited 2 times in total.

Geaper
Posts: 10
Joined: Tue Dec 22, 2015 10:51 am

Re: Find Centroid

Post by Geaper » Tue Dec 22, 2015 6:29 pm

I am willing to pay around 50€ via Paypal if you help me in the inicial code to make a project. I am new in EMGU and it's very hard for me to start.

Squares:
https://gyazo.com/f48b1a95aafedde2704c458415d58de1

I need to process squares like this images and determine which one is which (maybe comparing to a known database or if square has color blue,yellow,red than it's square 1).

I am already trying to solve this problem.

This s my Application:
https://gyazo.com/5e22e9a489d42266f55c187a7fe59a0e

Code: Select all

     frame11.SmoothMedian(3); //Filtro mediana antes da passagem para HSV
                frame11_processado_hsv = frame11.Convert<Hsv, Byte>(); //Converte a imagem da camera RGB para HSV
                 frame11_processado = frame11_processado_hsv.InRange(new Hsv(hl.Value, sl.Value, vl.Value), new Hsv(hh.Value, sh.Value, vh.Value)); //utiliza as trackbars HSV para ver a cor pretendida 
                frame11_processado.SmoothGaussian(11); //Filtro gaussiano na imagem binaria

                #region Extracting the Contours
                using (MemStorage storage = new MemStorage()) //aloca espaço na memoria
                {

                    for (Contour<Point> contours = frame11_processado.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
                    {

                        Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.015, storage);

                        if (currentContour.Area > area_contornos.Value)
                        {
                            if (currentContour.Total == 4)
                            {                        
                                
                                CvInvoke.cvDrawContours(frame11, contours, new MCvScalar(255), new MCvScalar(255), -1, 3, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));                               
                               frame11.Draw(currentContour.BoundingRectangle, new Bgr(0, 0, 255), 3);

                                Point[] pontos = currentContour.ToArray();
                                foreach (Point p in pontos)
                                {
                                    listbox_contornos.Items.Add(p);
                                }
                            }
                        }
                     
                    }

                }
                #endregion

But if you look the image is very noisy. I will never be able to detect only the squares like this. Maybe I need to improve code? Maybe I need to change the Squares (I can make like bigger outlines or something?Can you share ideas to solve this problem? Like i said i am willing to donate via pyapal (or something else) if you help me. Consider it as a Xmas gift ^^

arman
Posts: 9
Joined: Tue Dec 22, 2015 4:17 pm

Re: Find Centroid

Post by arman » Tue Dec 29, 2015 7:57 am

For detect the colour, you can to use, emgu sample function (TrafficSignRecognition):

Code: Select all

 private static void GetRedPixelMask(IInputArray image, IInputOutputArray mask)
      {
         bool useUMat;
         using (InputOutputArray ia = mask.GetInputOutputArray())
            useUMat = ia.IsUMat;

         using (IImage hsv = useUMat ? (IImage)new UMat() : (IImage)new Mat())
         using (IImage s = useUMat ? (IImage)new UMat() : (IImage)new Mat())
         {
            CvInvoke.CvtColor(image, hsv, ColorConversion.Bgr2Hsv);
            CvInvoke.ExtractChannel(hsv, mask, 0);
            CvInvoke.ExtractChannel(hsv, s, 1);

            //the mask for hue less than 20 or larger than 160
            using (ScalarArray lower = new ScalarArray(20))         // change here
            using (ScalarArray upper = new ScalarArray(160))       // change here
               CvInvoke.InRange(mask, lower, upper, mask);
            CvInvoke.BitwiseNot(mask, mask);

            //s is the mask for saturation of at least 10, this is mainly used to filter out white pixels
            CvInvoke.Threshold(s, s, 10, 255, ThresholdType.Binary);
            CvInvoke.BitwiseAnd(mask, s, mask, null);

         }
      }

So, you call the function: Get[youcolor]PixelMask(Img_original, hsv); / / hsv, is same img_original converto image <hsv, byte>... Example:
using (Image<Hsv, byte> hsv = Img_original.Convert<Hsv, byte>())


results here: https://www.youtube.com/watch?v=Yk3wqClIG9w
Alexander Recalde.

Post Reply