コード例 #1
0
        public static OpenCvSharp.IplImage GetSub(this OpenCvSharp.IplImage ipl, OpenCvSharp.CvRect subRect)
        {
            if (ipl == null)
            {
                throw new ArgumentNullException("ipl", "ipl is null.");
            }

            var boundingRect = new CvRect(0, 0, ipl.Width, ipl.Height);

            if (!boundingRect.Contains(subRect))
            {
                throw new InvalidOperationException("subRect is outside of ipl");
            }


            try
            {
                ipl.SetROI(subRect);

                OpenCvSharp.IplImage sub = new IplImage(
                    ipl.GetSize(),
                    ipl.Depth,
                    ipl.NChannels);

                ipl.Copy(sub);
                return(sub);
            }
            finally
            {
                ipl.ResetROI();
            }
        }
コード例 #2
0
ファイル: Inpaint.cs プロジェクト: healtech/opencvsharp
        public Inpaint()
        {
            // cvInpaint
            // 画像の不要な文字列部分に対するマスク画像を指定して文字列を除去する

            Console.WriteLine(
                "Hot keys: \n" +
                "\tESC - quit the program\n" +
                "\tr - restore the original image\n" +
                "\ti or ENTER - run inpainting algorithm\n" +
                "\t\t(before running it, paint something on the image)\n" +
                "\ts - save the original image, mask image, original+mask image and inpainted image to desktop."
            );

            // 原画像の読み込み
            using (IplImage img0 = new IplImage(Const.ImageFruits, LoadMode.AnyDepth | LoadMode.AnyColor))
            {
                // お絵かき用の画像を確保(マスク)
                using (IplImage img = img0.Clone())
                using (IplImage inpaintMask = new IplImage(img0.Size, BitDepth.U8, 1))
                // Inpaintの出力先画像を確保
                using (IplImage inpainted = img0.Clone())
                {
                    inpainted.Zero();
                    inpaintMask.Zero();

                    using (CvWindow wImage = new CvWindow("image", WindowMode.AutoSize, img))
                    {

                        // マウスイベントの処理
                        CvPoint prevPt = new CvPoint(-1, -1);
                        wImage.OnMouseCallback += delegate(MouseEvent ev, int x, int y, MouseEvent flags)
                        {
                            if (ev == MouseEvent.LButtonUp || (flags & MouseEvent.FlagLButton) == 0)
                            {
                                prevPt = new CvPoint(-1, -1);
                            }
                            else if (ev == MouseEvent.LButtonDown)
                            {
                                prevPt = new CvPoint(x, y);
                            }
                            else if (ev == MouseEvent.MouseMove && (flags & MouseEvent.FlagLButton) != 0)
                            {
                                CvPoint pt = new CvPoint(x, y);
                                if (prevPt.X < 0)
                                {
                                    prevPt = pt;
                                }
                                inpaintMask.Line(prevPt, pt, CvColor.White, 5, LineType.AntiAlias, 0);
                                img.Line(prevPt, pt, CvColor.White, 5, LineType.AntiAlias, 0);
                                prevPt = pt;
                                wImage.ShowImage(img);
                            }
                        };

                        for (; ; )
                        {
                            switch ((char)CvWindow.WaitKey(0))
                            {
                                case (char)27:    // ESCキーで終了
                                    CvWindow.DestroyAllWindows();
                                    return;
                                case 'r':   // 原画像を復元
                                    inpaintMask.Zero();
                                    img0.Copy(img);
                                    wImage.ShowImage(img);
                                    break;
                                case 'i':   // Inpaintの実行
                                case '\r':
                                    CvWindow wInpaint = new CvWindow("inpainted image", WindowMode.AutoSize);
                                    img.Inpaint(inpaintMask, inpainted, 3, InpaintMethod.Telea);
                                    wInpaint.ShowImage(inpainted);
                                    break;
                                case 's': // 画像の保存
                                    string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                                    img0.SaveImage(Path.Combine(desktop, "original.png"));
                                    inpaintMask.SaveImage(Path.Combine(desktop, "mask.png"));
                                    img.SaveImage(Path.Combine(desktop, "original+mask.png"));
                                    inpainted.SaveImage(Path.Combine(desktop, "inpainted.png"));
                                    break;
                            }
                        }

                    }

                }
            }

        }
コード例 #3
0
        /// <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;
        }
コード例 #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
        }
コード例 #5
0
	// Update is called once per frame
	void Update () {
	
		if (!close)
		{
			IplImage src = cap.QueryFrame();

            IplImage next = new IplImage(cols, rows, BitDepth.U8, 1);

            IplImage rez = new IplImage(cols, rows, BitDepth.U8, 3);
            src.Copy(rez);

			CvMat velx = Cv.CreateMat(rows, cols, MatrixType.F32C1);
			CvMat vely = Cv.CreateMat(rows, cols, MatrixType.F32C1);

			src.CvtColor(next, ColorConversion.BgrToGray);
			//uu = new CvMat(src.GetRow, src.GetCol, BitDepth.U8,
			//src.CvtColor(uu,ColorConversion.BgrToGray);
			//Cv.AbsDiff(prvs,next, rez);
			//Cv.CalcOpticalFlowFarneback(prvs,next,rez,0.5, 3, 15, 3, 5, 1.2, 0);
			//Cv.Canny(next, dstCanny, 50, 50, ApertureSize.Size3);
			//Cv.Erode(
			//Cv.Threshold(prvs,prvs,100,255,ThresholdType.Mask);
			//Cv.Threshold(next,next,100,255,ThresholdType.Mask);

		//	next.InRangeS(CvScalar(160,120,120), CvScalar(179,255,255),next);
        //    Cv.Erode(prvs, prvs, Cv.CreateStructuringElementEx(15, 15, 7, 7, ElementShape.Ellipse));
        //    Cv.Dilate(prvs, prvs, Cv.CreateStructuringElementEx(15, 15, 7, 7, ElementShape.Ellipse));
         //   Cv.Erode(next, next, Cv.CreateStructuringElementEx(15, 15, 7, 7, ElementShape.Ellipse));
        //    Cv.Dilate(next, next, Cv.CreateStructuringElementEx(15, 15, 7, 7, ElementShape.Ellipse));

       //     Cv.Dilate(prvs, prvs, Cv.CreateStructuringElementEx(15, 15, 7, 7, ElementShape.Ellipse));
       //     Cv.Erode(prvs, prvs, Cv.CreateStructuringElementEx(15, 15, 7, 7, ElementShape.Ellipse));
	
        //    Cv.Dilate(next, next, Cv.CreateStructuringElementEx(15, 15, 7, 7, ElementShape.Ellipse));
        //    Cv.Erode(next, next, Cv.CreateStructuringElementEx(15, 15, 7, 7, ElementShape.Ellipse));


            Cv.Threshold(next, next, 120, 255, ThresholdType.Truncate);
            Cv.Threshold(prvs, prvs, 120, 255, ThresholdType.Truncate);
            
           
			Cv.CalcOpticalFlowLK(prvs,next,Cv.Size(15,15),velx,vely);
	    	CalcPoint(velx,vely,rez);
		//	DrawOpticalFlow(velx,vely,rez);
            FromIplImageToTexture(rez, planeRight);
           // FromIplImageToTexture(src, planeLeft);

			if (Input.GetKey (KeyCode.Space)) {
				src.SaveImage("Assets/1.jpg");
				Debug.Log("Save");
			
			}


			w.Image = rez;
			next.Copy(prvs);

			
		
		}
		//Debug.Log (move.x);
		if (Input.GetKey (KeyCode.Escape)) {
			close = true;
			w.Close();
		}

	}
コード例 #6
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);
        }
コード例 #7
0
	// Update is called once per frame
	void Update () {

        FromTextureToIplImage(src);
        IplImage next = new IplImage(imWidth, imHeight, BitDepth.U8, 1);
        IplImage prvs = new IplImage(imWidth, imHeight, BitDepth.U8, 1);
        CvMat velx = new CvMat(imWidth, imHeight, MatrixType.F32C2);
        CvMat vely = new CvMat(imWidth, imHeight, MatrixType.F32C2);
     
        if (fl)
        {
           
            Cv.CvtColor(src, next, ColorConversion.BgrToGray);
            Cv.Threshold(next, next, 90, 255, ThresholdType.Truncate);
            Cv.Threshold(prvs, prvs, 90, 255, ThresholdType.Truncate);
           //s Cv.AbsDiff
          //  Cv.CalcOpticalFlowFarneback(prvs, next, velx, 0.5, 3, 15, 3, 5, 1.2, 0);
        //    Cv.CalcOpticalFlowLK(prvs, next, Cv.Size(15, 15), velx, vely);
         //   CalcPoint(velx, vely, rez);

            w.Image = rez;
            next.Copy(prvs);
        }
        if (!fl) 
        {
           
            Cv.CvtColor(src, prvs, ColorConversion.BgrToGray);
       //     w.Image = prvs;
            fl = true;
        }


	    
	}