public static float[] ResizeIplTo(IplImage Face, int width, int height)
        {
            IplImage smallerFace =
                new IplImage(new OpenCvSharp.CvSize(width, height),
                                         Face.Depth, Face.NChannels);

            Face.Resize(smallerFace, Interpolation.Linear);

            unsafe
            {
                byte* smallFaceData = smallerFace.ImageDataPtr;
                float[] currentFace = new float[width * height * smallerFace.NChannels * BytesPerPixel(Face.Depth)];
                for (int i = 0; i < smallerFace.Height; i++)
                {
                    for (int j = 0; j < smallerFace.Width; j++)
                    {
                        currentFace[i * smallerFace.WidthStep + j] =
                            (float)smallFaceData[i * smallerFace.WidthStep + j];
                    }
                }

                smallerFace.Dispose();

                return currentFace;
            }
        }
        /// <summary>
        /// 指定した分割数でそろばんを全て読み取る
        /// </summary>
        /// <param name="source">そろばんの画像</param>
        /// <param name="threshold">しきい値</param>
        /// <param name="process_img">処理画像</param>
        /// <returns>読み取った数値(-1はエラー)</returns>
        public int[] AllMatching(IplImage source, double threshold, out IplImage process_img)
        {
            // グレースケール画像
            IplImage cap_gray = new IplImage(PROCESS_SIZE, BitDepth.U8, 1);

            // キャプチャとリサイズ,グレースケール変換
            using (IplImage tmp = new IplImage(
                PROCESS_SIZE, source.Depth, source.NChannels))
            {
                source.Resize(tmp);
                tmp.CvtColor(cap_gray, ColorConversion.BgrToGray);
            }

            int[] results = new int[DIVIDE_NUM];
            int width = cap_gray.Width / (DIVIDE_NUM + 1);
            int margin = (int)(width * DIVIDE_MARGIN);

            // 領域ごとに処理
            Parallel.For(0, DIVIDE_NUM, i =>
            {
                IplImage tmp = new IplImage(PROCESS_SIZE, BitDepth.U8, 1);
                cap_gray.Copy(tmp);

                int x = (i + 1) * width - width / 2;
                // 領域を指定
                CvRect rect = new CvRect(x - margin, 0, width + margin * 2, PROCESS_SIZE.Height);
                tmp.SetROI(rect);

                // 0-9の画像とMatchTemplateし一番高い値を得る
                results[i] = bestMatchNum(tmp, this.templates[i], threshold);

                // 領域の指定を解除
                tmp.ResetROI();
            });

            // 分割線の描画
            for (int i = 1; i < DIVIDE_NUM + 2; i++)
            {
                int x = i * width - width / 2;
                cap_gray.Line(x, 0, x, PROCESS_SIZE.Height, CvColor.White);
            }

            // 読み取り数値を表示
            CvFont font = new CvFont(FontFace.HersheyDuplex, 1.0, 1.0);
            for (int i = 0; i < DIVIDE_NUM; i++)
            {
                if (results[i] != -1)
                {
                    int x = i * width + width / 2;
                    cap_gray.PutText(results[i].ToString(), new CvPoint(x, 30),
                        font, CvColor.White);
                }
            }

            // 分割線, 読み取り数値画像を返す
            process_img = cap_gray;

            return results;
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            double brightMinVal = 0, brightMaxVal = 0;
            CvPoint brightMinLoc, brightMaxLoc;
            IplImage image5, image4, image3, image2, image1, image0, totalImage, output;
            CvSize size, resize;
            image5 = Cv.LoadImage("C:\\Summer14\\Top Layer\\Camera_5_bmp\\Oct-28-12-14-26_773008-000005-109990.bmp", LoadMode.GrayScale);
            image4 = Cv.LoadImage("C:\\Summer14\\Top Layer\\Camera_4_bmp\\Oct-28-12-14-26_773008-000004-109990.bmp", LoadMode.GrayScale);
            image3 = Cv.LoadImage("C:\\Summer14\\Top Layer\\Camera_3_bmp\\Oct-28-12-14-26_773008-000003-109990.bmp", LoadMode.GrayScale);
            image2 = Cv.LoadImage("C:\\Summer14\\Top Layer\\Camera_2_bmp\\Oct-28-12-14-26_773008-000002-109990.bmp", LoadMode.GrayScale);
            image1 = Cv.LoadImage("C:\\Summer14\\Top Layer\\Camera_1_bmp\\Oct-28-12-14-26_773008-000001-109990.bmp", LoadMode.GrayScale);
            image0 = Cv.LoadImage("C:\\Summer14\\Top Layer\\Camera_0_bmp\\Oct-28-12-14-26_773008-000000-109990.bmp", LoadMode.GrayScale);
            size = new CvSize(image1.Width * 3, image1.Height * 2);
            totalImage = new IplImage(size, image1.Depth, image1.NChannels);
            //****************MAKING IMAGE 5 BRIGHTER
            Cv.MinMaxLoc(image5, out brightMinVal, out brightMaxVal, out brightMinLoc, out brightMaxLoc);
            double scale = 2.9;//255/85 or 2.9
            double shift = -brightMinVal * scale;
            Cv.ConvertScale(image5, image5, scale, shift);
            //***************END MAKING IMAGE 5 BRIGHTER
            //***************STITCHING THE 6 CAMERA VIEWS TOGETHER

            for (int i = 0; i < image3.Height; i++)// i: vertical
                for (int j = 60; j < image3.Width; j++)
                    totalImage[i, j - 60] = image3[i, j];
            //******************************
            for (int i = image2.Height - 74; i < totalImage.Height - 74; i++)
            {
                for (int j = 0; j < image2.Width - 50; j++)
                {

                    totalImage[i - 74, j] = image2[x, j + 50];

                }
                x++;
            }
            x = 0;
            for (int i = 0; i < image1.Height; i++)
            {
                for (int j = image3.Width - 60; j < (totalImage.Width - image3.Width) - 200; j++)//60,200
                {
                    totalImage[i, j - 60] = image1[i, x];
                    x++;
                }
                x = 0;

            }
            x = 0;
            int k = 0;
            //*******************************
            for (int i = image1.Height - 110; i < totalImage.Height - 110; i++)//120
            {
                for (int j = image2.Width - 95; j < (totalImage.Width - image2.Width) - 300; j++)//95,300
                {
                    totalImage[i - 74, j] = image0[k, x];
                    x++;

                }
                x = 0;
                k++;

            }

            x = 0;
            k = 0;
            //*******************************
            for (int i = 0; i < image5.Height - 70; i++)
            {
                for (int j = (image3.Width + image1.Width)-200; j < totalImage.Width-200; j++)//200,200
                {
                    totalImage[i, j - 130] = image5[i + 70, x];
                    x++;
                }
                x = 0;
            }
            x = 0;
            //********************************
            for (int i = image5.Height - 170; i < totalImage.Height - 170; i++)
            {
                for (int j = (image2.Width + image0.Width) - 300; j < totalImage.Width - 300; j++)//300
                {
                    totalImage[i, j] = image4[k, x];
                    x++;
                }
                x = 0;
                k++;
            }
            //******************END STITCHING THE 6 CAMERA VIEWS TOGETHER

            resize = new CvSize(totalImage.Width / 12, totalImage.Height / 12);
            output = new IplImage(resize, totalImage.Depth, totalImage.NChannels);
            totalImage.Resize(output);
            //**************IMPORTANT SAVE!!!!!!!!!
            output.SaveImage("C:\\OpenCvSharp\\FallProgram2LessOverlap\\NewMosaic.bmp");//Saves the scaled down version.
            totalImage.SaveImage("C:\\OpenCvSharp\\FallProgram2LessOverlap\\TrueSizedMosaic.bmp");//Saves the original size
            //*************END IMPORTANT SAVE!!!!!!!!
            Cv.NamedWindow("Output", WindowMode.AutoSize);
            Cv.ShowImage("Output", output);
            Cv.WaitKey(0);
        }
Beispiel #4
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
        }
Beispiel #5
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);
        }