C# OpenCV学习笔记五之图像轮廓
2011-09-22 21:41
791 查看
原始图
轮廓图
相关代码如下
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>((Bitmap)loadPictureBox.Image);
Image<Gray, Byte> imageGrayscale = imageSource.Convert<Gray, Byte>();
Image<Gray, Byte> dest = new Image<Gray, Byte>(imageGrayscale.Width, imageGrayscale.Height);
CvInvoke.cvThreshold(imageGrayscale, dest, 30, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
loadPictureBox.Image = dest.ToBitmap();
IntPtr Dyncontour = new IntPtr();
IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);
MCvContour con = new MCvContour();
int n = CvInvoke.cvFindContours(dest, Dynstorage, ref Dyncontour, Marshal.SizeOf(con), Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_LINK_RUNS, new Point(0, 0));
Seq<Point> DyncontourTemp = new Seq<Point>(Dyncontour, null);
IntPtr dst = CvInvoke.cvCreateImage(CvInvoke.cvGetSize(dest), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);
CvInvoke.cvZero(dst);
int areaTotal = 0;//求轮廓面积
double max = 0;//求轮廓最大矩阵对角线
for (; DyncontourTemp != null && DyncontourTemp.Ptr.ToInt32() != 0; DyncontourTemp = DyncontourTemp.HNext)
{
Rectangle r = DyncontourTemp.BoundingRectangle;
double tmp = Math.Sqrt(r.Height * r.Height + r.Width * r.Width);
if (tmp > max)
{
max = tmp;
}
areaTotal += r.Width * r.Height;
//画出轮廓
CvInvoke.cvRectangle(dst, new Point(r.X, r.Y), new Point(r.X + r.Width, r.Y + r.Height), new MCvScalar(255, 0, 0), 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0);
}
MessageBox.Show("Area Total :" + areaTotal.ToString());
MessageBox.Show("Max Dist : " + max.ToString());
CvInvoke.cvNamedWindow("dst");
CvInvoke.cvShowImage("dst", dst);
IntPtr2Image(dst).Save("001-1.jpg");//保存轮廓图形
轮廓图
相关代码如下
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>((Bitmap)loadPictureBox.Image);
Image<Gray, Byte> imageGrayscale = imageSource.Convert<Gray, Byte>();
Image<Gray, Byte> dest = new Image<Gray, Byte>(imageGrayscale.Width, imageGrayscale.Height);
CvInvoke.cvThreshold(imageGrayscale, dest, 30, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
loadPictureBox.Image = dest.ToBitmap();
IntPtr Dyncontour = new IntPtr();
IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);
MCvContour con = new MCvContour();
int n = CvInvoke.cvFindContours(dest, Dynstorage, ref Dyncontour, Marshal.SizeOf(con), Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_LINK_RUNS, new Point(0, 0));
Seq<Point> DyncontourTemp = new Seq<Point>(Dyncontour, null);
IntPtr dst = CvInvoke.cvCreateImage(CvInvoke.cvGetSize(dest), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);
CvInvoke.cvZero(dst);
int areaTotal = 0;//求轮廓面积
double max = 0;//求轮廓最大矩阵对角线
for (; DyncontourTemp != null && DyncontourTemp.Ptr.ToInt32() != 0; DyncontourTemp = DyncontourTemp.HNext)
{
Rectangle r = DyncontourTemp.BoundingRectangle;
double tmp = Math.Sqrt(r.Height * r.Height + r.Width * r.Width);
if (tmp > max)
{
max = tmp;
}
areaTotal += r.Width * r.Height;
//画出轮廓
CvInvoke.cvRectangle(dst, new Point(r.X, r.Y), new Point(r.X + r.Width, r.Y + r.Height), new MCvScalar(255, 0, 0), 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0);
}
MessageBox.Show("Area Total :" + areaTotal.ToString());
MessageBox.Show("Max Dist : " + max.ToString());
CvInvoke.cvNamedWindow("dst");
CvInvoke.cvShowImage("dst", dst);
IntPtr2Image(dst).Save("001-1.jpg");//保存轮廓图形
相关文章推荐
- opencv学习笔记 在输入图像上寻找并绘制轮廓
- C# OpenCV学习笔记三之图像捕捉及其灰度转换方法
- Python OpenCV学习笔记之:图像轮廓处理
- c# opencvsharp学习笔记(3)兴趣区域ROI,图像叠加,操作像素点
- opencv学习笔记12 图像轮廓的提取和绘制
- C# OpenCV学习笔记四之图像二值化
- C# OpenCV学习笔记二之图像读写的两种方法
- OpenCV2学习笔记(七):使用Canny算子检测轮廓
- OpenCV学习笔记九——访问图像中的像素
- 【opencv学习笔记五】一个简单程序:图像读取与显示
- opencv学习笔记之对灰度图像遍历的三种方法
- opencv学习笔记之两幅图像显式融合
- 【opencv学习笔记七】访问图像中的像素与图像亮度对比度调整
- 【OpenCV学习笔记】之六 手写图像旋转函数---万丈高楼平地起
- opencv学习笔记-加载图像(释疑:非mat类与mat类、头文件引用)
- OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形
- OpenCV学习笔记_图像平滑
- 【OpenCV学习笔记 003】图像的载入、显示和输出
- OpenCV学习笔记(三)——Mat,图像的新容器
- OpenCV 2 学习笔记(8): 利用邻域处理图像与简单的算术图像处理算法:图像滤波与加权和