当前位置: 首页>>代码示例>>C#>>正文


C# IplImage.CvtColor方法代码示例

本文整理汇总了C#中OpenCvSharp.IplImage.CvtColor方法的典型用法代码示例。如果您正苦于以下问题:C# IplImage.CvtColor方法的具体用法?C# IplImage.CvtColor怎么用?C# IplImage.CvtColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OpenCvSharp.IplImage的用法示例。


在下文中一共展示了IplImage.CvtColor方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Snake

 public Snake()
 {
     using (IplImage src = new IplImage(Const.ImageCake, LoadMode.GrayScale))
     using (IplImage dst = new IplImage(src.Size, BitDepth.U8, 3))
     {
         CvPoint[] contour = new CvPoint[100];
         CvPoint center = new CvPoint(src.Width / 2, src.Height / 2);
         for (int i = 0; i < contour.Length; i++)
         {
             contour[i].X = (int)(center.X * Math.Cos(2 * Math.PI * i / contour.Length) + center.X);
             contour[i].Y = (int)(center.Y * Math.Sin(2 * Math.PI * i / contour.Length) + center.Y);
         }
         Console.WriteLine("Press any key to snake\nEsc - quit");
         using (CvWindow w = new CvWindow())
         {
             while (true)
             {
                 src.SnakeImage(contour, 0.45f, 0.35f, 0.2f, new CvSize(15, 15), new CvTermCriteria(1), true);
                 src.CvtColor(dst, ColorConversion.GrayToRgb);
                 for (int i = 0; i < contour.Length - 1; i++)
                 {
                     dst.Line(contour[i], contour[i + 1], new CvColor(255, 0, 0), 2);
                 }
                 dst.Line(contour[contour.Length - 1], contour[0], new CvColor(255, 0, 0), 2);
                 w.Image = dst;
                 int key = CvWindow.WaitKey();
                 if (key == 27)
                 {
                     break;
                 }
             }
         }
     }
 }
开发者ID:healtech,项目名称:opencvsharp,代码行数:34,代码来源:Snake.cs

示例2: SampleFileStorageWriteImage

        /// <summary>
        /// 画像データのファイルストレージへの書き込み
        /// </summary>
        /// <param name="fileName">書きこむXML or YAMLファイル</param>
        private static void SampleFileStorageWriteImage(string fileName)
        {
            // cvWrite, cvWriteComment
            // IplImage構造体の情報をファイルに保存する

            // (1)画像を読み込む
            using (IplImage colorImg = new IplImage(Const.ImageLenna, LoadMode.Color))
            using (IplImage grayImg = new IplImage(colorImg.Size, BitDepth.U8, 1))
            {
                // (2)ROIの設定と二値化処理
                colorImg.CvtColor(grayImg, ColorConversion.BgrToGray);
                CvRect roi = new CvRect(0, 0, colorImg.Width / 2, colorImg.Height / 2);
                grayImg.SetROI(roi);
                colorImg.SetROI(roi);
                grayImg.Threshold(grayImg, 90, 255, ThresholdType.Binary);
                // (3)xmlファイルへの書き出し 
                using (CvFileStorage fs = new CvFileStorage(fileName, null, FileStorageMode.Write))
                {
                    fs.WriteComment("This is a comment line.", false);
                    fs.Write("color_img", colorImg);
                    fs.StartNextStream();
                    fs.Write("gray_img", grayImg);
                }
                // (4)書きこんだxmlファイルを開く
                //using (Process p = Process.Start(fileName)) {
                //    p.WaitForExit();
                //}                
            }
        }
开发者ID:qxp1011,项目名称:opencvsharp,代码行数:33,代码来源:FileStorage.cs

示例3: AllMatching

        /// <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;
        }
开发者ID:nnct-jo-ken,项目名称:SorobanReader,代码行数:66,代码来源:SorobanReader.cs

示例4: createAGrayScaleClone2

        private static void createAGrayScaleClone2()
        {
            using (var src = new IplImage(@"..\..\images\ocv02.jpg", LoadMode.Color))
            //using (var dst = new IplImage(new CvSize(src.Width, src.Height), BitDepth.U8, 1))
            using (var dst = new IplImage(src.Size, BitDepth.U8, 1))
            {
                src.CvtColor(dst, ColorConversion.BgrToGray);

                using (new CvWindow("src", image: src))
                using (new CvWindow("dst", image: dst))
                {
                    Cv.WaitKey();
                }
            }
        }
开发者ID:Muraad,项目名称:OpenCVSharp-Samples,代码行数:15,代码来源:Program.cs

示例5: Threshold

 public Threshold()
 {
     using (IplImage src = new IplImage(Const.ImageLenna, LoadMode.Color))
     using (IplImage srcGray = new IplImage(src.Size, BitDepth.U8, 1))
     using (IplImage dst = new IplImage(src.Size, BitDepth.U8, 1))
     using (CvWindow window = new CvWindow("SampleThreshold"))
     {
         src.CvtColor(srcGray, ColorConversion.BgrToGray);
         srcGray.Smooth(srcGray, SmoothType.Gaussian, 5);
         int threshold = 90;
         window.CreateTrackbar("threshold", threshold, 255, delegate(int pos)
         {
             srcGray.Threshold(dst, pos, 255, ThresholdType.Binary);
             window.Image = dst;
         });
         srcGray.Threshold(dst, threshold, 255, ThresholdType.Binary);
         window.Image = dst;
         CvWindow.WaitKey();
     }
 }
开发者ID:neoxeo,项目名称:opencvsharp,代码行数:20,代码来源:Threshold.cs

示例6: GenerateRainbowLine

        /// <summary>
        /// Генерирует радужную линейку заданных размеров
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        private static IplImage GenerateRainbowLine(int width, int height)
        {
            IplImage result = new IplImage(new CvSize(width, height), BitDepth.U8, 3);
            IntPtr ptr = result.ImageData;

            for (int x = 0; x < result.Width; x++)
            {
                for (int y = 0; y < result.Height; y++)
                {
                    int offset = (result.WidthStep * y) + (x * 3);
                    byte val = (byte)Math.Round(180.0 * (x + 1) / result.Width);
                    Marshal.WriteByte(ptr, offset + 0, val);
                    Marshal.WriteByte(ptr, offset + 1, 255);
                    Marshal.WriteByte(ptr, offset + 2, 255);
                }
            }

            result.CvtColor(result, ColorConversion.HsvToRgb);

            return result;
        }
开发者ID:Goshik92,项目名称:RingMeter,代码行数:27,代码来源:RainbowLine.cs

示例7: SampleFileStorageWriteImage

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        private static void SampleFileStorageWriteImage(string fileName)
        {
            // cvWrite, cvWriteComment

            using (IplImage colorImg = new IplImage(FilePath.Image.Lenna, LoadMode.Color))
            using (IplImage grayImg = new IplImage(colorImg.Size, BitDepth.U8, 1))
            {
                colorImg.CvtColor(grayImg, ColorConversion.BgrToGray);
                CvRect roi = new CvRect(0, 0, colorImg.Width / 2, colorImg.Height / 2);
                grayImg.SetROI(roi);
                colorImg.SetROI(roi);
                grayImg.Threshold(grayImg, 90, 255, ThresholdType.Binary);

                using (CvFileStorage fs = new CvFileStorage(fileName, null, FileStorageMode.Write))
                {
                    fs.WriteComment("This is a comment line.", false);
                    fs.Write("color_img", colorImg);
                    fs.StartNextStream();
                    fs.Write("gray_img", grayImg);
                }
            }
        }
开发者ID:0sv,项目名称:opencvsharp,代码行数:26,代码来源:FileStorage.cs

示例8: Edge

        public Edge()
        {
            using (IplImage src = new IplImage(Const.ImageLenna, LoadMode.Color))
            using (IplImage gray = new IplImage(src.Size, BitDepth.U8, 1))
            using (IplImage temp = new IplImage(src.Size, BitDepth.S16, 1))
            using (IplImage dstSobel = new IplImage(src.Size, BitDepth.U8, 1))
            using (IplImage dstLaplace = new IplImage(src.Size, BitDepth.U8, 1))
            using (IplImage dstCanny = new IplImage(src.Size, BitDepth.U8, 1))
            {
                //src.CvtColor(gray, ColorConversion.RgbToGray);
                src.CvtColor(gray, ColorConversion.BgrToGray);

                // Sobel
                Cv.Sobel(gray, temp, 1, 0, ApertureSize.Size3);
                Cv.ConvertScaleAbs(temp, dstSobel);

                // Laplace
                Cv.Laplace(gray, temp);
                Cv.ConvertScaleAbs(temp, dstLaplace);

                // Canny
                Cv.Canny(gray, dstCanny, 50, 200, ApertureSize.Size3);

                using (new CvWindow("src", src)) 
                using (new CvWindow("sobel", dstSobel))
                using (new CvWindow("laplace", dstLaplace)) 
                using (new CvWindow("canny", dstCanny))
                {
                    CvWindow.WaitKey();
                }

                dstSobel.SaveImage("sobel.png");
                dstLaplace.SaveImage("laplace.png");
                dstCanny.SaveImage("canny.png");
            }
        }
开发者ID:healtech,项目名称:opencvsharp,代码行数:36,代码来源:Edge.cs

示例9: separateBackground

        /// <summary>
        /// Отделяет изображение от фона
        /// </summary>
        /// <param name="source">Исходное изображение</param>
        /// <param name="destinatation">Результат разделения</param>
        private void separateBackground(IplImage source, IplImage destinatation)
        {
            // Преобразуем иходное изображение в HSV
            source.CvtColor(hsvImg, ColorConversion.RgbToHsv);

            // Разбиваем изображение на отельные каналы
            hsvImg.CvtPixToPlane(hImg, sImg, vImg, null);

            // Если диапазон Hue состоит из 2х частей
            if (BackgroundRange.HMin > BackgroundRange.HMax)
            {
                hImg.InRangeS(CvScalar.RealScalar(BackgroundRange.HMin), CvScalar.RealScalar(HsvRange.MAX_H), tmpImg);
                hImg.InRangeS(CvScalar.RealScalar(HsvRange.MIN_H), CvScalar.RealScalar(BackgroundRange.HMax), hImg);
                Cv.Or(tmpImg, hImg, hImg);
            }

            // Если диапазон Hue состоит из 1 части
            else hImg.InRangeS(CvScalar.RealScalar(BackgroundRange.HMin), CvScalar.RealScalar(BackgroundRange.HMax), hImg);

            // Ограничиваем значение остальных цветовых компонент
            sImg.InRangeS(CvScalar.RealScalar(BackgroundRange.SMin), CvScalar.RealScalar(BackgroundRange.SMax), sImg);
            vImg.InRangeS(CvScalar.RealScalar(BackgroundRange.VMin), CvScalar.RealScalar(BackgroundRange.VMax), vImg);

            // Формируем окончательный результат
            Cv.And(hImg, sImg, destinatation);
            Cv.And(destinatation, vImg, destinatation);
            Cv.Not(destinatation, destinatation);
        }
开发者ID:Goshik92,项目名称:RingMeter,代码行数:33,代码来源:ContoursFinder.cs


注:本文中的OpenCvSharp.IplImage.CvtColor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。