예제 #1
0
        static void Main(string[] args)
        {
            Stopwatch timer = new Stopwatch();
            IplImage bestMatch = new IplImage();
            CvMat mapMatrix;
            CvPoint2D32f center;
            double[] maxArray = new double[8];
            int numberOfFiles = 50;//Can change the number of input files if I want
            double angle = 0.0, scale = 1.0, bestAngle = 0.0;
            mapMatrix = new CvMat(2, 3, MatrixType.F32C1);

            while (fileReader < numberOfFiles)//Number of files to read in folder
            {

                uavSourceImg = new IplImage("C:\\OpenCvSharp\\SummerPractice13RotateAndScale4DataInfo2\\Testing Different UAV Inputs\\RenameFolder\\Kamien " + fileReader + ".bmp", LoadMode.AnyColor);

                tempImg = new IplImage("C:\\OpenCvSharp\\SummerPractice1\\SummerDatabase\\BigGoogleTemplate2.jpg", LoadMode.AnyColor);//Big template test

                CvRect drawRectangle = new CvRect(200, 200, 300, 300);
                timer.Start();
                while (angle < 15.0)//Angle change while loop. Can change if necessary
                {
                    while (i < 10)//Scaling while loop. Can change this if necessary.
                    {

                        //***********************DECLARATION

                        //tempImg = theBigTemplate.GetSubImage(drawRectangle);
                        CvSize destSize;
                        graySource = new IplImage(uavSourceImg.Size, BitDepth.U8, 1);
                        grayTemp = new IplImage(tempImg.Size, BitDepth.U8, 1);
                        tempDestImg = new IplImage(grayTemp.Size, BitDepth.U8, 1);

                        double minValue, maxValue;
                        CvPoint minLoc, maxLoc;
                        //**********************END DECLARATIONS

                        //**********************CONVERT TO GRAY
                        uavSourceImg.CvtColor(graySource, ColorConversion.BgrToGray);
                        tempImg.CvtColor(grayTemp, ColorConversion.BgrToGray);
                        //**********************END CONVERT TO GRAY
                        //**********************ROTATION
                        center = new CvPoint2D32f(grayTemp.Width * 0.5, grayTemp.Height * 0.5);
                        grayTemp.Copy(tempDestImg);
                        Cv._2DRotationMatrix(center, angle, scale, out mapMatrix);
                        Cv.WarpAffine(grayTemp, tempDestImg, mapMatrix, Interpolation.FillOutliers, Cv.ScalarAll(255));
                        //**********************END ROTATION

                        theRotatedSubTemp = tempDestImg.GetSubImage(drawRectangle);

                        //**********************RESIZE PART
                        CvSize size = new CvSize(graySource.Width / i, graySource.Height / i);//Manipulate the source image size

                        CvSize size2 = new CvSize(theRotatedSubTemp.Width / i, theRotatedSubTemp.Height / i);//theRotatedSubTemp test

                        graySourceHolder = new IplImage(size, BitDepth.U8, 1);//1 for grayholder ORIGINAL
                        grayTempHolder = new IplImage(size2, BitDepth.U8, 1);
                        graySource.Resize(graySourceHolder);//ORIGINAL(resize the grayscale source image before template matching)

                        theRotatedSubTemp.Resize(grayTempHolder);//TEST theRotatedSubTemp
                        //*********************END RESIZE PART

                        //*********************TEMPLATE MATCHING PART
                        destSize = new CvSize(graySourceHolder.Width - grayTempHolder.Width + 1, graySourceHolder.Height - grayTempHolder.Height + 1);
                        //TEST RESIZE BEFORE WITH RESIZED TEMPLATE
                        resizeDestImg = new IplImage(destSize, BitDepth.F32, 1);
                        graySourceHolder.MatchTemplate(grayTempHolder, resizeDestImg, MatchTemplateMethod.CCoeffNormed);
                        resizeDestImg.MinMaxLoc(out minValue, out maxValue, out minLoc, out maxLoc);
                        graySourceHolder.Rectangle(maxLoc.X, maxLoc.Y, maxLoc.X + grayTempHolder.Width, maxLoc.Y + grayTempHolder.Height, CvColor.Red, 3);//Testing resize before with resized template
                        //********************END TEMPLATE MATCHING PART
                        Console.WriteLine("Divided by {0}, there was a {1} percent match", i, maxValue);
                        if (maxValue > theBestMax)
                        {
                            theBestMax = maxValue;
                            bestMatch = graySourceHolder.Clone();
                            bestAngle = angle;
                            iHolder = i;

                        }

                        Cv.NamedWindow("Rotating Template", WindowMode.AutoSize);
                        Cv.NamedWindow("Sub Template", WindowMode.AutoSize);
                        Cv.ShowImage("Rotating Template", tempDestImg);
                        Cv.ShowImage("Sub Template", theRotatedSubTemp);
                        //Cv.WaitKey(0);
                        Cv.WaitKey(1);
                        i++;

                        Cv.ReleaseData(graySourceHolder);
                        Cv.ReleaseData(grayTempHolder);
                        Cv.ReleaseData(tempDestImg);
                        Cv.ReleaseData(graySource);
                        Cv.ReleaseData(grayTemp);
                        Cv.ReleaseData(resizeDestImg);
                        Cv.ReleaseData(theRotatedSubTemp);//Added for big template test
                    }//End 3rd Inner while loop
                    angle += 1.5;
                    //This changes the angle tilt of the template.  Can change if necessary.
                    i = 1;//This changes the scale divider.  Can change if necessary.
                    Console.WriteLine("***************************************SHIFTING TEMPLATE\n");
                    //Cv.DestroyAllWindows();
                }//End 2nd Inner while loop

            //***************SHOWING RESULT INFO
            timer.Stop();
            //This section writes the results to a text file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\OpenCvSharp\\SummerPractice12RotateAndScale3DataInfo\\Data Info Folder\\Best Match Data.txt", true))
            {

                file.WriteLine("{0}\t{1}\t\t{2}\t1/{3}", timer.ElapsedMilliseconds, theBestMax.ToString("#.###"), bestAngle, iHolder);

            }
            Console.WriteLine("---------RESULTS");

            //*************END SHOWING RESULT INFO

            //*************IMPORTANT SAVES
            bestMatch.SaveImage("C:\\OpenCvSharp\\SummerPractice12RotateAndScale3DataInfo\\Data Info Folder\\Best Match" + fileReader.ToString() + ".jpg");

            Console.WriteLine("ITEM SAVED IN DATA INFO FOLDER!");
            //*************END IMPORTANT SAVES
            Cv.WaitKey(1);
            fileReader++;
            theBestMax = double.MinValue;
            iHolder = int.MinValue;
            bestAngle = double.MinValue;
            angle = 0.0;
            i = 1;
            Cv.ReleaseData(bestMatch);//TEST
                if(fileReader < numberOfFiles)
            Console.WriteLine("Switching Input...");
            }//End while loop
        }
예제 #2
0
        private IplImage DetectImage(IplImage img, out CvRect roi, out CvPoint[] pts)
        {
            CvPoint[] squares = OpenCVSharpHelper.DetectSquares(img);
            storage.Clear();

            roi = new CvRect();
            pts = new CvPoint[4];
            if (squares.Count() >= 4)
            {
                for (int i = 0; i < squares.Length; i += 4)
                {
                    CvPoint[] pt = new CvPoint[4];

                    // read 4 vertices
                    pt[0] = squares[i + 0];
                    pt[1] = squares[i + 1];
                    pt[2] = squares[i + 2];
                    pt[3] = squares[i + 3];
                    CvRect tmproi = Cv.BoundingRect(pt);
                    int tmparea = tmproi.Width * tmproi.Height;
                    if (tmparea > (roi.Width * roi.Height))
                    {
                        roi = tmproi;
                        pts = pt;
                    }
                }

                // Show detected image (rectangle of interest)
                IplImage detected = img.GetSubImage(roi);
                return detected;
            }
            else
            {
                return null;
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            Stopwatch timer = new Stopwatch(),timer2 = new Stopwatch();
             IplImage topLayer, smallerTop, uavLayer, smallerUAV, uavTemplate, destImg;
             CvPoint minLoc, maxLoc, brightMinLoc, brightMaxLoc;
             double minValue, maxValue, bestMatch = 0;
              CvSize makeTopSmaller, makeUAVSmaller, destSize;//scale originalSize x 12
               //CvRect uavRect;
              CvMat mapMatrix;
              CvPoint2D32f center;
             StreamWriter writer2 = new StreamWriter
              (@"C:\\OpenCvSharp\\FallProgram5TopLayerTimeMeasure\\Top Layer Measure.txt");
            double angle = 0.0, scale = 1.0, topScale = 0.09,
                bestScale = 0, bestAngle = 0, brightMinVal = 0, brightMaxVal = 0;
            topLayer = new IplImage("C:\\OpenCvSharp\\FallProgram5TopLayerTimeMeasure\\TrueSizedMosaic.bmp", LoadMode.GrayScale);
            uavLayer = new IplImage("C:\\Summer14\\EO_Run07_s7_301_16_00_51_278385\\Oct-28-12-00-57_523977-Frame-30.bmp", LoadMode.GrayScale);

            //************************Brighten input image section
            Cv.MinMaxLoc(topLayer, out brightMinVal, out brightMaxVal, out brightMinLoc, out brightMaxLoc);
            double brightScale = 255 / 70;//90 smaller denominator makes it brighter
            double brightShift = -brightMinVal * brightScale;
            Cv.ConvertScale(topLayer, topLayer, brightScale, brightShift);
            timer.Start();
            //************************End brighten section
            while (topScale > 0.06)
            {

                /*****************Manipulate the size of top layer and uav*/
                makeTopSmaller = new CvSize((int)(topLayer.Width * topScale), (int)(topLayer.Height * topScale));
                //make top layer original size x0.07
                makeUAVSmaller = new CvSize((int)(uavLayer.Width * .08), (int)(topLayer.Height * .03));
                //***************************************

                while (angle > -361.0)
                {
                    //*********************************
                    /*PART 1 This section makes the top layer and uav layer smaller*/

                    smallerTop = new IplImage(makeTopSmaller, topLayer.Depth, topLayer.NChannels);//Make the top layer smaller
                    smallerUAV = new IplImage(makeUAVSmaller, uavLayer.Depth, uavLayer.NChannels);//Make the uav layer smaller

                    topLayer.Resize(smallerTop);//Resize the top Layer
                    uavLayer.Resize(smallerUAV);//Resize the uav Layer

                    //*******************END 1

                    //******************Part 2 ROTATING SECTION
                    mapMatrix = new CvMat(2, 3, MatrixType.F32C1);
                    center = new CvPoint2D32f(smallerUAV.Width * 0.5, smallerUAV.Height * 0.5);
                    Cv._2DRotationMatrix(center, angle, scale, out mapMatrix);

                    Cv.WarpAffine(smallerUAV, smallerUAV, mapMatrix, Interpolation.Linear, Cv.ScalarAll(255));
                    angle -= 0.1;

                    //***********************END 2
                    //***********************PART 3 This section handles getting the template
                    uavRect = new CvRect((smallerUAV.Width / 2) - 75, (smallerUAV.Height / 2) - 75, 150, 150);//200x200
                    smallerUAV.Rectangle(uavRect, CvColor.Red, 2);
                    uavTemplate = smallerUAV.GetSubImage(uavRect);

                    //**********************END 3
                    //**********************PART 4 This is where template matching begins
                    destSize = new CvSize(smallerTop.Width - uavTemplate.Width + 1, smallerTop.Height - uavTemplate.Height + 1);
                    destImg = new IplImage(destSize, BitDepth.F32, 1);
                    //Recording the time on each step
                    timer2.Start();
                    smallerTop.MatchTemplate(uavTemplate, destImg, MatchTemplateMethod.CCoeffNormed);
                    timer2.Stop();
                    writer2.WriteLine("{0} {1} {2}",topScale,counter, timer2.ElapsedMilliseconds);//scale multiplier,scale count, time
                    timer2.Reset();
                    counter++;
                    //End section
                    destImg.MinMaxLoc(out minValue, out maxValue, out minLoc, out maxLoc);
                    smallerTop.Rectangle(maxLoc.X, maxLoc.Y, maxLoc.X + uavTemplate.Width, maxLoc.Y + uavTemplate.Height, CvColor.Red, 3);
                    //*******Records best match
                    if (maxValue > bestMatch)
                    {
                        x = maxLoc.X;
                        y = maxLoc.Y;
                        bestAngle = angle;
                        bestScale = topScale;
                        bestMatch = maxValue;
                        bestTopLayer = new IplImage(makeTopSmaller, smallerTop.Depth, smallerTop.NChannels);//decaration for the best match
                        smallerTop.Copy(bestTopLayer);
                        Console.WriteLine("New best match: {0}", bestMatch);
                        //******IMPORTANT SAVE!!!
                        uavTemplate.SaveImage("C:\\OpenCvSharp\\SummerPractice24UAVTrack3\\Used Template.bmp");
                        //******END IMPORTANT SAVE!!!
                    }
                    //*******End Record Best Match
                    //*******Output test
                    Cv.ShowImage("Top Layer", smallerTop);
                    Cv.ShowImage("smaller uav", smallerUAV);
                    Cv.ShowImage("Template", uavTemplate);
                    Cv.WaitKey(1);
                    //*******End Output test

                    //*****************END 4
                    Cv.ReleaseData(smallerTop);
                    Cv.ReleaseData(smallerUAV);
                    Cv.ReleaseData(uavTemplate);
                    Cv.ReleaseData(destImg);

                }//End Inner While loop
                angle = 0.0;
                topScale -= 0.01;
                Console.WriteLine("Scaling top layer down.....");
                counter = 0;
            }//End While loop

            timer.Stop();
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter
                (@"C:\\OpenCvSharp\\FallProgram5TopLayerTimeMeasure\\Total Top Measure.txt", true))
            {
                writer.WriteLine
                    ("{0}\t{1}\t\t{2}\t{3}\t{4}", timer.ElapsedMilliseconds, bestMatch.ToString("#.###"),
                    bestAngle.ToString("###.#"), bestScale.ToString(), "(X: " + x.ToString() + ", Y: " + y.ToString() + ")");
            }
            //**********************IMPORTANT SAVE!!!
            bestTopLayer.SaveImage("C:\\OpenCvSharp\\FallProgram5TopLayerTimeMeasure\\TL Match Image.bmp");

            //**********************END IMPORTANT SAVE!!!
            Cv.ShowImage("The Best Match", bestTopLayer);
            Cv.WaitKey(0);
        }