说明
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、车牌分割:(做膨胀处理,使“粤”连通起来)