24-正外接矩形BoundingRectangle(EmguCV學習)

文章目錄

說明

1、BoundingRectangle()尋找出來的最小外接矩形可以根據其寬高對輪廓進行篩選,去除小的干擾輪廓
在這裏插入圖片描述

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Drawing;

namespace lesson24_25
{
    class Program
    {
        static void Main(string[] args)
        {
            ////外接矩形
            //Mat src = CvInvoke.Imread("22.jpg");
            //Mat dst = src.Clone();
            //Mat grayImg = new Mat();

            //CvInvoke.CvtColor(src, grayImg, ColorConversion.Bgr2Gray);  //轉換爲灰度圖
            //CvInvoke.Threshold(grayImg, grayImg, 100, 255, ThresholdType.BinaryInv);//轉換爲二值圖
            //CvInvoke.Imshow("binary image", grayImg);

            //VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            //Mat hierarchy = new Mat();  //n * 4矩陣
            //CvInvoke.FindContours(grayImg, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxNone);
            //CvInvoke.DrawContours(dst, contours, -1, new MCvScalar(255, 0, 0), 2);

            //for(int i = 0; i < contours.Size; i++)
            //{
            //    Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);   //計算最小外接矩形
            //    CvInvoke.Rectangle(dst, rect, new MCvScalar(0, 0, 255), 2); //繪製最小外接矩形
            //    CvInvoke.PutText(dst, "bounding rectangle", new Point(rect.X, rect.Y - 10), FontFace.HersheyPlain, 1.2, new MCvScalar(0, 0, 255), 2);
            //}
            //CvInvoke.Imshow("result", dst);

            ///硬幣分割
            //Mat src = CvInvoke.Imread("33.png");
            //Mat dst = src.Clone();
            //Mat grayImg = new Mat();
            //CvInvoke.CvtColor(src, grayImg, ColorConversion.Bgr2Gray);
            //CvInvoke.Threshold(grayImg, grayImg, 100, 255, ThresholdType.Binary);
            //CvInvoke.Imshow("binary image", grayImg);
            //Mat element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), new Point(-1, -1));//定義掩膜核
            //CvInvoke.Dilate(grayImg, grayImg, element, new Point(-1, -1), 1, BorderType.Default,new MCvScalar(0));
            //CvInvoke.Imshow("dilate image", grayImg);

            //VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            //VectorOfRect hierarchy = new VectorOfRect();
            //CvInvoke.FindContours(grayImg, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxNone);
            ////CvInvoke.DrawContours(dst, contours, -1, new MCvScalar(0, 255, 0), 2);

            //for(int i = 0; i < contours.Size; i++)
            //{
            //    Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
            //    int width = rect.Width;
            //    int height = rect.Height;
            //    if(width > 20 &&  height > 10)
            //    {
            //        CvInvoke.Rectangle(dst, rect, new MCvScalar(0, 0, 255), 2);
            //    }
            //}
            //CvInvoke.Imshow("result", dst);

            //CvInvoke.WaitKey(0);

            ///車牌提取與字符分割
            Mat src = CvInvoke.Imread("car2.jpg");
            Mat dst = src.Clone();
            Mat grayImg = new Mat();
            CvInvoke.Imshow("input", src);
            CvInvoke.CvtColor(src, grayImg, ColorConversion.Bgr2Gray);
            CvInvoke.Threshold(grayImg, grayImg, 100, 255, ThresholdType.Binary);
            CvInvoke.MedianBlur(grayImg, grayImg, 5);
            Mat kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(5, 5), new Point(-1, -1));
            CvInvoke.Dilate(grayImg, grayImg, kernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0));

            CvInvoke.Imshow("binary", grayImg);
            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            VectorOfRect hierarchy = new VectorOfRect();

            CvInvoke.FindContours(grayImg, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxNone);
            //CvInvoke.DrawContours(dst, contours, -1, new MCvScalar(255, 0, 0), 2);

            int count = 0;
            for(int i = 0; i < contours.Size; i++)
            {
                Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
                int w0 = rect.Width;
                int h0 = rect.Height;
                if (w0 > src.Cols / 12 && w0 < src.Cols / 7 && h0 > src.Rows / 6 && h0 < src.Rows * 5 / 6)
                {
                    count++;
                    string name = String.Format("C:\\Users\\hello\\Desktop\\{0}.bmp", count);
                    Mat roi = new Mat(dst, rect);
                    CvInvoke.Imwrite(name, roi);
                    CvInvoke.Rectangle(dst, rect, new MCvScalar(0, 255, 0), 2);
                }
            }

            CvInvoke.Imshow("result", dst);
            CvInvoke.WaitKey(0);
        }
    }
}

效果

1、正外接矩形:
在這裏插入圖片描述

2、硬幣檢測:

在這裏插入圖片描述3、車牌分割:
在這裏插入圖片描述

在這裏插入圖片描述
4、車牌分割:(做膨脹處理,使“粵”連通起來)

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章