欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#中WinForm用OpencvSharp實現(xiàn)ROI區(qū)域提取的示例

 更新時間:2022年05月06日 10:11:31   作者:Iawfy_  
已經(jīng)自學OpencvSharp一段時間了,現(xiàn)在就分享一下我的學習過程,本文主要介紹了c#中WinForm用OpencvSharp實現(xiàn)ROI區(qū)域提取的示例,具有一定的參考價值,感興趣的可以了解一下

已經(jīng)自學OpencvSharp一段時間了(目前工作用的是C#,就學了Opencvsharp了,vs2015,opencvsharp3),收獲也有一些,現(xiàn)在就將我在學習過程中的收獲分享出來吧。

圖像處理,很常見的問題,但對于大多數(shù)時候而言,我們往往不需要去處理整張圖片,而是只需要處理一部分,這就涉及到了ROI(Region of interest)的提取了。我目前提取ROI的方法是采用掩膜Mask的方法。具體的思路就是:在圖像操作的時候,定義一張同等大小的空的Mask,也就是全部是0,然后將我們想要的ROI輪廓畫在Mask上,并填充內(nèi)部,就會得到新的Mask,這個新的Mask就只有在ROI區(qū)域非0,其余地方元素都是0,再把用Cv2.BoundingRect()將包含ROI區(qū)域的輪廓的最小矩形找出來,分別將原圖與Mask這兩幅圖像的這個最小矩形部分提出來 ,最后再調(diào)用Cv2.BitwiseAnd()這個方法,通常情況而言,一副圖像與自己本身進行與運算,輸出的還是本身圖像,帶上掩膜Mask后,就只會輸出圖像在Mask非0區(qū)域部分(也就是我們所需要的ROI)的圖像了,這樣就實現(xiàn)了我們的ROI提取了。

接下來,就分享幾種常見ROI區(qū)域提取吧。

部分代碼如下:

主要使用的變量

         /// 放在yVars.ImgOptions中 
   public struct ROIMatt   
            {
                public static string Image;//原圖  
                public static bool IsSelectRegion = false;
 
                public static int step; //ROI區(qū)域移動步長
                public static int angel; // 旋轉一次 angel±=step; 旋轉角度 
 
                public static yDirections direct = yDirections.NULL;
                public static yROIRegionType ROIType = yROIRegionType.Rectangle;
 
                // 矩形ROI
                // 矩形四個點坐標 都是相對于圖像的坐標 而不是相對于picturebox的坐標
                // 矩形四個點相對位置 剛開始確定矩形時就這樣 經(jīng)過旋轉后位置變 但相對位置還是這樣
                //  1  2      // 按順時針數(shù)的點   1-->2-->4-->3-->1-->2-->4-->3-->1
                //  3  4
                public static OpenCvSharp.Point rectFirstPoint = new OpenCvSharp.Point();
                public static OpenCvSharp.Point rectSecondPoint = new OpenCvSharp.Point();
                public static OpenCvSharp.Point rectThirdPoint = new OpenCvSharp.Point();
                public static OpenCvSharp.Point rectFourthPoint = new OpenCvSharp.Point(); 
                public static double rectWidth = 0;
                public static double rectHeight = 0;
 
                // 圓形ROI
                public static OpenCvSharp.Point cirCenter = new OpenCvSharp.Point(0.0,0.0); // 圓心
                public static int cirRadious = 0; // 半徑
                // 橢圓ROI
                public static OpenCvSharp.Point elpCenter = new OpenCvSharp.Point(0.0,0.0); // 橢圓中心點
                public static double elpAngel = 0.0;//橢圓傾斜角度
                public static double elpLongAxis = 0.0; // 長軸
                public static double elpShortAxis = 0.0; // 短軸
            }

我的picturebox的SizeMode是StretchImage的,可能看起來跟想要的結果有點差異,但是實際上是一樣的。

首先是最常見的矩形。

對于正矩形而言,我們可以直接定義出圖像的ROI區(qū)域

public Mat(Mat m, Rect roi);

這樣定義的圖像就是原圖m的指定區(qū)域了。但對于傾斜的矩陣,RotatedRect,而言,就得需要使用掩膜了,提取ROI的方法以及結果如下:

        public static void ImgMattingRect()
        {
            Mat pic = new Mat(yVars.ImgOptions.ROIMatt.Image);
            Mat mask = Mat.Zeros(pic.Size(), MatType.CV_8UC1);
            OpenCvSharp.Point2f[] coutours = new OpenCvSharp.Point2f[4];
            coutours[0] = yVars.ImgOptions.ROIMatt.rectFirstPoint;
            coutours[1] = yVars.ImgOptions.ROIMatt.rectSecondPoint;
            coutours[2] = yVars.ImgOptions.ROIMatt.rectFourthPoint;
            coutours[3] = yVars.ImgOptions.ROIMatt.rectThirdPoint;
 
            List<OpenCvSharp.Point> listt = new List<OpenCvSharp.Point>();
            for (int i = 0; i < coutours.Count(); i++)
            {
                listt.Add(new OpenCvSharp.Point(coutours[i].X, coutours[i].Y));
            }
 
            List<List<OpenCvSharp.Point>> pp = new List<List<OpenCvSharp.Point>>() { listt };
 
            Cv2.FillPoly(mask, pp, new Scalar(255, 255, 255)); 
            OpenCvSharp.Rect rect = Cv2.BoundingRect(coutours);
            Mat src = new Mat(pic, rect);
            Mat maskROI = new Mat(mask, rect);
            Mat picOut = new Mat();
            Cv2.BitwiseAnd(src, src, picOut, maskROI);
            Form1.Instance.pbxMattImage.Image = yImgConvert.MatToBitmap(picOut);
 
            yVars.ImgOptions.ROIMatt.rectFirstPoint = new OpenCvSharp.Point(0, 0);
            yVars.ImgOptions.ROIMatt.rectSecondPoint = new OpenCvSharp.Point(0, 0);
            yVars.ImgOptions.ROIMatt.rectThirdPoint = new OpenCvSharp.Point(0, 0);
            yVars.ImgOptions.ROIMatt.rectFourthPoint = new OpenCvSharp.Point(0, 0);
        }

縮放平移和旋轉就只要改變矩形的四個頂點坐標就行了。方法都一樣就不贅述了。

圓形ROI區(qū)域,

方法如下:

        public static void ImgMattingCircle()
        {
            Mat mm = new Mat(yVars.ImgOptions.ROIMatt.Image);
            Mat mask = Mat.Zeros(mm.Size(), MatType.CV_8UC3);
 
            Cv2.Circle(mask, yVars.ImgOptions.ROIMatt.cirCenter, yVars.ImgOptions.ROIMatt.cirRadious, Scalar.Red, 1, LineTypes.AntiAlias);
            Cv2.FloodFill(mask, yVars.ImgOptions.ROIMatt.cirCenter, Scalar.Red);
 
            mask.ConvertTo(mask, MatType.CV_8UC1);
 
            int xx = yVars.ImgOptions.ROIMatt.cirCenter.X - yVars.ImgOptions.ROIMatt.cirRadious;
            int yy = yVars.ImgOptions.ROIMatt.cirCenter.Y - yVars.ImgOptions.ROIMatt.cirRadious;
            int rr = 2 * yVars.ImgOptions.ROIMatt.cirRadious;
            // 圓的外接正方形
            Rect rect = new Rect(new OpenCvSharp.Point(xx, yy), new OpenCvSharp.Size(rr, rr));
 
            Mat src = new Mat(mm, rect);
            Mat maskRoI = new Mat(mask, rect);
 
            Cv2.CvtColor(maskRoI, maskRoI, ColorConversionCodes.BGR2GRAY);
 
            Mat picOut = new Mat();
            Cv2.BitwiseAnd(src, src, picOut, maskRoI);
 
            Form1.Instance.pbxMattImage.Image = yImgConvert.MatToBitmap(picOut);
 
            yVars.ImgOptions.ROIMatt.cirCenter = new OpenCvSharp.Point(0, 0);
            yVars.ImgOptions.ROIMatt.cirRadious = 0;
        }

效果展示:

 圓形ROI的移動時就只有圓心坐標變 半徑不變,而縮放時只改變半徑,圓心不變,注意移動時別超出圖像界限就行。

橢圓ROI

在Opencvsharp中繪制橢圓有兩種方式

        //
        // 摘要:
        //     Draws simple or thick elliptic arc or fills ellipse sector
        //
        // 參數(shù):
        //   img:
        //     Image.
        //
        //   box:
        //     The enclosing box of the ellipse drawn
        //
        //   color:
        //     Ellipse color.
        //
        //   thickness:
        //     Thickness of the ellipse boundary. [By default this is 1]
        //
        //   lineType:
        //     Type of the ellipse boundary. [By default this is LineType.Link8]
        public static void Ellipse(InputOutputArray img, RotatedRect box, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8);
        //
        // 摘要:
        //     Draws simple or thick elliptic arc or fills ellipse sector
        //
        // 參數(shù):
        //   img:
        //     Image.
        //
        //   center:
        //     Center of the ellipse.
        //
        //   axes:
        //     Length of the ellipse axes.
        //
        //   angle:
        //     Rotation angle.
        //
        //   startAngle:
        //     Starting angle of the elliptic arc.
        //
        //   endAngle:
        //     Ending angle of the elliptic arc.
        //
        //   color:
        //     Ellipse color.
        //
        //   thickness:
        //     Thickness of the ellipse arc. [By default this is 1]
        //
        //   lineType:
        //     Type of the ellipse boundary. [By default this is LineType.Link8]
        //
        //   shift:
        //     Number of fractional bits in the center coordinates and axes' values. [By default
        //     this is 0]
        public static void Ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0);

我們采用第一種方式,即可以將橢圓轉化成一個RotatedRect,只要在畫RotatedRect的時候改成畫橢圓即可,就可以回到第一種矩形的ROI提取上面了, 代碼如下:

        public static void ImgMattingEllipse()
        {
            Mat mm = new Mat(yVars.ImgOptions.ROIMatt.Image);
            Mat mask = Mat.Zeros(mm.Size(), MatType.CV_8UC3);
 
            RotatedRect rorect = new RotatedRect(yVars.ImgOptions.ROIMatt.elpCenter, new Size2f(yVars.ImgOptions.ROIMatt.elpLongAxis, yVars.ImgOptions.ROIMatt.elpShortAxis), (float)yVars.ImgOptions.ROIMatt.elpAngel);
            Cv2.Ellipse(mask, rorect, Scalar.Red);
 
            Mat gray = new Mat();
            Cv2.CvtColor(mask, gray, ColorConversionCodes.BGR2GRAY);
            Cv2.Threshold(gray, gray, 100, 255, ThresholdTypes.Otsu);
 
            OpenCvSharp.Point[][] contours;
            HierarchyIndex[] hierarchly;
            Cv2.FindContours(gray, out contours, out hierarchly, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple, new OpenCvSharp.Point(0, 0));
 
            Rect rect = Cv2.BoundingRect(contours[0]);
 
            Cv2.FloodFill(mask, yVars.ImgOptions.ROIMatt.elpCenter, Scalar.Red);
 
            mask.ConvertTo(mask, MatType.CV_8UC1);
 
            Mat src = new Mat(mm, rect);
            Mat maskRoI = new Mat(mask, rect);
            Cv2.CvtColor(maskRoI, maskRoI, ColorConversionCodes.BGR2GRAY);
            Mat picOut = new Mat();
            Cv2.BitwiseAnd(src, src, picOut, maskRoI);
 
            Form1.Instance.pbxMattImage.Image = yImgConvert.MatToBitmap(picOut);
        }

實驗結果如下:

 平移縮放旋轉等操作就可以看成對RotatedRect的操作即可。

到此這篇關于c#中WinForm用OpencvSharp實現(xiàn)ROI區(qū)域提取的示例的文章就介紹到這了,更多相關c# ROI區(qū)域提取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C#給文字換行的小技巧

    C#給文字換行的小技巧

    這篇文章主要介紹了C#給文字換行的小技巧,本文直接給出實現(xiàn)代碼,例子蠻簡單,一看就懂啦,需要的朋友可以參考下
    2015-06-06
  • C#選擇排序法實例分析

    C#選擇排序法實例分析

    這篇文章主要介紹了C#選擇排序法,實例分析了C#排序算法的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#中實現(xiàn)在32位、64位系統(tǒng)下自動切換不同的SQLite dll文件

    C#中實現(xiàn)在32位、64位系統(tǒng)下自動切換不同的SQLite dll文件

    這篇文章主要介紹了C#中實現(xiàn)在32位、64位系統(tǒng)下自動切換不同的SQLite dll文件,本文使用C#代碼實現(xiàn)DLL文件的切換,需要的朋友可以參考下
    2014-09-09
  • 5分鐘用C#實現(xiàn)串口助手

    5分鐘用C#實現(xiàn)串口助手

    本文主要介紹了C#實現(xiàn)串口助手,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • WPF快速入門教程之綁定Binding

    WPF快速入門教程之綁定Binding

    初學wpf,經(jīng)常被Binding搞暈,以下記錄寫B(tài)inding的基礎。下面這篇文章主要給大家介紹了關于WPF快速入門教程之綁定Binding的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-10-10
  • C#調(diào)用CMD命令實例

    C#調(diào)用CMD命令實例

    這篇文章主要介紹了C#調(diào)用CMD命令實例本文只是給出一個比較簡單的、入門級的例子,更多高級的操作技巧請參閱相關文章,需要的朋友可以參考下
    2014-08-08
  • C#使用DLLImport調(diào)用外部DLL的方法

    C#使用DLLImport調(diào)用外部DLL的方法

    這篇文章介紹了C#使用DLLImport調(diào)用外部DLL的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • C#自動給文章關鍵字加鏈接實現(xiàn)代碼

    C#自動給文章關鍵字加鏈接實現(xiàn)代碼

    這篇文章主要介紹了C#自動給文章關鍵字加鏈接實現(xiàn)代碼,有需要的朋友可以參考一下
    2013-12-12
  • .Net筆記:System.IO之Stream的使用詳解

    .Net筆記:System.IO之Stream的使用詳解

    本篇文章是對.Net中System.IO之Stream的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • 解析C#設計模式編程中適配器模式的實現(xiàn)

    解析C#設計模式編程中適配器模式的實現(xiàn)

    這篇文章主要介紹了C#設計模式編程中適配器模式的實現(xiàn),分別舉了類的對象適配器與對象的適配器模式的例子,需要的朋友可以參考下
    2016-02-02

最新評論