Emgu-WPF学习使用-Rectangle识别
环境:Win8 64位 Vs2015
Emgu 版本:emgucv-windesktop 3.2.0.2682
示例图上部流程:原图->灰度化->截断阈值化->中值模糊->高斯模糊->膨胀->腐蚀->Ostu二值化。
// 灰度化
Image<Gray, byte> imgGray = new Image<Gray, byte>(imgSrc.Size);
CvInvoke.CvtColor(imgSrc, imgGray, ColorConversion.Bgr2Gray);
//截断阈值化
Image<Gray, byte> imgThresholdTrunc = new Image<Gray, byte>(imgGray.Size);
CvInvoke.Threshold(imgGray, imgThresholdTrunc, 60, 255, ThresholdType.Trunc);
// 中值模糊
Image<Gray, byte> imgMedian = imgThresholdTrunc.SmoothMedian(7); //使用5*5的卷积核
// 高斯模糊
Image<Gray, byte> imgGaussian = imgMedian.SmoothGaussian(5);
// 膨胀,消除杂点
Mat oMat2 = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle,
new System.Drawing.Size(5, 5), new System.Drawing.Point(0, 0));
Image<Gray, byte> imgErode = new Image<Gray, byte>(imgGray.Size);
CvInvoke.Erode(imgGaussian, imgErode, oMat2, new System.Drawing.Point(0, 0), 4,
BorderType.Default, new MCvScalar(255, 0, 0, 255));
// 腐蚀,消除杂点
Image<Gray, byte> imgDilate = new Image<Gray, byte>(imgGray.Size);
CvInvoke.Dilate(imgErode, imgDilate, oMat2, new System.Drawing.Point(0, 0), 4,
BorderType.Default, new MCvScalar(255, 0, 0, 255));
// Otsu二值化
Image<Gray, byte> imgThresholdOtsu = new Image<Gray, byte>(imgGray.Size);
CvInvoke.Threshold(imgDilate, imgThresholdOtsu, 0, 255, ThresholdType.Otsu);
示例图下部流程:原图->灰度化->截断阈值化->消除裂缝->Ostu二值化->识别Contours->绘制Contours.
// 灰度化
Image<Gray, byte> imgGray = new Image<Gray, byte>(imgSrc.Size);
CvInvoke.CvtColor(imgSrc, imgGray, ColorConversion.Bgr2Gray);
//截断阈值化
Image<Gray, byte> imgThresholdTrunc = new Image<Gray, byte>(imgGray.Size);
CvInvoke.Threshold(imgGray, imgThresholdTrunc, 60, 255, ThresholdType.Trunc);
// 消除裂缝
Mat oMat1 = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle,
new System.Drawing.Size(6, 6), new System.Drawing.Point(0, 0));
Image<Gray, byte> imgMorphologyEx = new Image<Gray, byte>(imgGray.Size);
CvInvoke.MorphologyEx(imgThresholdTrunc, imgMorphologyEx, Emgu.CV.CvEnum.MorphOp.Close, oMat1,
new System.Drawing.Point(0, 0), 1, BorderType.Default,
new MCvScalar(255, 0, 0, 255));
// Otsu二值化
Image<Gray, byte> imgThresholdOtsu = new Image<Gray, byte>(imgGray.Size);
CvInvoke.Threshold(imgMorphologyEx, imgThresholdOtsu, 0, 255, ThresholdType.Otsu);
List<RotatedRect> boxList = new List<RotatedRect>(); //a box is a rotated rectangle
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(imgThresholdOtsu, contours, null, RetrType.List,
ChainApproxMethod.ChainApproxSimple);
Image<Bgr, byte> imgResult = new Image<Bgr, byte>(imgGray.Size);
CvInvoke.CvtColor(imgThresholdOtsu, imgResult, ColorConversion.Gray2Bgr);
MCvScalar oScaler = new MCvScalar(40, 255, 255, 255);
int count = contours.Size;
for (int i = 0; i < count; i++)
{
using (VectorOfPoint contour = contours[i])
using (VectorOfPoint approxContour = new VectorOfPoint())
{
CvInvoke.ApproxPolyDP(contour, approxContour, CvInvoke.ArcLength(contour, true) * 0.05, true);
double dArea = CvInvoke.ContourArea(approxContour, false);
if (dArea > imgThresholdOtsu.Rows * imgThresholdOtsu.Cols / 3d)
{
if (approxContour.Size == 4)
{
#region determine if all the angles in the contour are within [80, 100] degree
bool isRectangle = true;
System.Drawing.Point[] pts = approxContour.ToArray();
LineSegment2D[] edges = Emgu.CV.PointCollection.PolyLine(pts, true);
for (int j = 0; j < edges.Length; j++)
{
double angle = Math.Abs(
edges[(j + 1) % edges.Length].GetExteriorAngleDegree(edges[j]));
if (angle < 80 || angle > 100)
{
isRectangle = false;
break;
}
}
#endregion
if (isRectangle)
CvInvoke.DrawContours(imgResult, contours, i, oScaler, 3);
}
}
}
}
Emgu-WPF学习使用-Rectangle识别的更多相关文章
- WPF学习之资源-Resources
WPF学习之资源-Resources WPF通过资源来保存一些可以被重复利用的样式,对象定义以及一些传统的资源如二进制数据,图片等等,而在其支持上也更能体现出这些资源定义的优越性.比如通过Resour ...
- WPF学习:3.Border & Brush
上一章<WPF学习:2.Layout-Panels-Countainers>主要介绍了布局,容器和面板.这一章主要开始介绍Border(边界)和Brush(画刷). 代码地址:http:/ ...
- WPF学习12:基于MVVM Light 制作图形编辑工具(3)
本文是WPF学习11:基于MVVM Light 制作图形编辑工具(2)的后续 这一次的目标是完成 两个任务. 本节完成后的效果: 本文分为三个部分: 1.对之前代码不合理的地方重新设计. 2.图形可选 ...
- WPF学习11:基于MVVM Light 制作图形编辑工具(2)
本文是WPF学习10:基于MVVM Light 制作图形编辑工具(1)的后续 这一次的目标是完成 两个任务. 画布 效果: 画布上,选择的方案是:直接以Image作为画布,使用RenderTarget ...
- WPF学习10:基于MVVM Light 制作图形编辑工具(1)
图形编辑器的功能如下图所示: 除了MVVM Light 框架是一个新东西之外,本文所涉及内容之前的WPF学习0-9基本都有相关介绍. 本节中,将搭建编辑器的界面,搭建MVVM Light 框架的使用环 ...
- 基于深度学习的人脸识别系统(Caffe+OpenCV+Dlib)【二】人脸预处理
前言 基于深度学习的人脸识别系统,一共用到了5个开源库:OpenCV(计算机视觉库).Caffe(深度学习库).Dlib(机器学习库).libfacedetection(人脸检测库).cudnn(gp ...
- 【WPF学习】第五十三章 动画类型回顾
创建动画面临的第一个挑战是为动画选择正确的属性.期望的结果(例如,在窗口中移动元素)与需要使用的属性(在这种情况下是Canvas.Left和Canvas.Top属性)之间的关系并不总是很直观.下面是一 ...
- WPF学习开发客户端软件-任务助手(下 2015年2月4日代码更新)
时光如梭,距离第一次写的 WPF学习开发客户端软件-任务助手(已上传源码) 已有三个多月,期间我断断续续地对该项目做了优化.完善等等工作,现在重新向大家介绍一下,希望各位可以使用,本软件以实用性为主 ...
- WPF学习05:2D绘图 使用Transform进行控件变形
在WPF学习04:2D绘图 使用Shape绘基本图形中,我们了解了如何绘制基本的图形. 这一次,我们进一步,研究如何将图形变形. 例子 一个三角形,经Transform形成组合图形: XAML代码: ...
随机推荐
- USB 3.0规范中译本 第6章 物理层
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 6.1 物理层概览 物理层定义超高速总线的信号技术.本章定义超高速物理层的电气要求. 本节定义超高速组件之间 ...
- target属性值
有 4 个保留的目标名称用作特殊的文档重定向操作: _blank 浏览器总在一个新打开.未命名的窗体中加载目标文档. _self 这个目标的值对全部没有指定目标的 <a> 标签是默认目标, ...
- [tmux] Copy and paste text from a tmux session
One non-obvious, but extremely useful, feature in tmux is copy-pasting text between panes. This also ...
- node转发formdata
router.post('/keUpload', checkLogin, setAccessControlAllow, (req, res, next) => { const busboy = ...
- 数据结构 Tricks(一)—— 父节点和左右孩子索引号之间的关系
如果以第 0 个位置开始标记树根节点,则第 i 个结点的左右孩子分别为: 2i+1 2i+2 反之,如果一个结点的标号为 i,则其父节点为: i/2:i 为左孩子结点: i/2-1:i 为右孩子结点: ...
- svn: is already a working copy for a different url 解决办法
svnX svn: E155000: '/Users/mac/Desktop/SHiosProject/SVNmangerfiles/wuye' is already a working c ...
- NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝
题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...
- VS2012经常使用的快捷方式完成最全面
(1)如何更改设置快捷键 1.转到工具----选项 对话框 2.选 环境---->键盘 3.在 [显示命令包括] 以下的对话框中输入"对齐"keyword,然后就会在这个 ...
- Swift 中的高阶函数和函数嵌套
高阶函数 在Swift中,函数可做为“一等公民”的存在,也就意味着,我们可以和使用 int 以及 String 一样,将函数当做 参数.值.类型来使用. 其中,将函数当作一个参数和值来使用可见下: t ...
- Hexo里如何添加广告
前期先用Hexo做个人网站,模板可以用https://github.com/828768/maupassant-hexo,关于如何加入广告,可以看一下https://sobaigu.com/hexo- ...