Emgu&openCV二值化图像,对不为零的区域进行检测

1.先是在程序中图像的导入,我是根据图像路径实现,其中path是string类型,是图像路径。

IntPtr img=CvInvoke.cvLoadImage(path, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);

2.图像灰度化处理,先创建一幅尺寸大小为为原图的8位图像GrayImg1:

Rectangle cr = CvInvoke.cvGetImageROI(img1);

                int width = cr.Width;

                int height = cr.Height;

IntPtr GrayImg1 = CvInvoke.cvCreateImage(cr.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);

现在就能使用cvCvtColor函数实现灰度化:

CvInvoke.cvCvtColor(img1, GrayImg1, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);

3.直方图的创建,并获取数据

int[] hist_size = new int[1] { 256 };//建一个数组来存放直方图数据

IntPtr HistImg=CvInvoke.cvCreateHist(1, hist_size, Emgu.CV.CvEnum.HIST_TYPE.CV_HIST_ARRAY, null, 1);//创建了一个空的直方图

CvInvoke.cvCalcHist(inPtr1, HistImg,false,System.IntPtr.Zero);//计算inPtr1指向图像的数据,并传入Histimg中,其中IntPtr[] inPtr1 = new IntPtr[1] { SubImg}。

现在要获取Histimg中的具体数据:

for (int i = 0; i < 256; i++)

            {

                temphist[i] = CvInvoke.cvQueryHistValue_1D(histImg, i);

            }

这样在数组temphist中保存了直方图数据。

4.对第一步中由cvLoadImage导入的图像进行像素点的操作。由于img 是IntPtr类型无法直接进行操作,所以首先要进行格式的转化,把IntPtr型转换成MIplImage:

Emgu.CV.Structure.MIplImage MIpImg =

(Emgu.CV.Structure.MIplImage)System.Runtime.InteropServices.Marshal.PtrToStructure(img, typeof(Emgu.CV.Structure.MIplImage));

然后再C#中使用unsafe中指针操作:npixel = (int)((byte*)img.imageData + img.widthStep * i)[j];

5.在二值话的图像,对不为零的区域经行检测。

IntPtr Dyncontour = new IntPtr();//存放检测到的图像块的首地址

IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);开辟内存区域

int n= CvInvoke.cvFindContours(tempimg, Dynstorage, ref Dyncontour, StructSize.MCvContour, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP,Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, new Point(0, 0));

n表示检测到不为零区域的个数。

6.对第五步检测到的区域绘制轮廓

for(;DyncontourTemp!=null&&DyncontourTemp.Ptr.ToInt32()!=0;DyncontourTemp=DyncontourTemp.HNext)

{

CvInvoke.cvDrawContours(tempContImg, DyncontourTemp,new MCvScalar(255, 255, 255),new MCvScalar(255, 255, 255), 0, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));

 }

其中的DyncontourTemp为

Seq<Point> DyncontourTemp1= new Seq<Point>(Dyncontour, null);//方便对IntPtr类型进行操作

Seq<Point> DyncontourTemp=DyncontourTemp1;

7.对第五步检测出的区域的坐标提取,通过cvFindContours函数的调用在 Dyncontour中存放的是不为零区域坐标的值存储在内存中的首地址指针。

seq<Point> DyncontourTemp1= new Seq<Point>(Dyncontour, null); //方便对IntPtr类型进行操作

int total=contourImg.Total;//contourImg包含的元素的总数

 int TempX = 0;  int TempY = 0;int[,] contourArray = new int[2,total];

 //获得轮廓的坐标值

 for (int i = 0; i < total;i++ )

{

  contourArray[0,i]=contourImg[i].X;

  contourArray[1,i]=contourImg[i].Y;

 }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
非常抱歉,我理解有误,以下是使用 OpenCV 在 C# 中求直线与轮廓的交点的代码示例: ```csharp using System.Collections.Generic; using System.Drawing; using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; // 读取图像并转为灰度图像 Mat img = CvInvoke.Imread("image.jpg", ImreadModes.Grayscale); // Canny边缘检测 Mat edges = new Mat(); CvInvoke.Canny(img, edges, 50, 150); // 寻找轮廓 VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat hierarchy = new Mat(); CvInvoke.FindContours(edges, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxSimple); // 遍历轮廓 for (int i = 0; i < contours.Size; i++) { // 对轮廓进行逼近 VectorOfPoint approx = new VectorOfPoint(); CvInvoke.ApproxPolyDP(contours[i], approx, 0.01 * CvInvoke.ArcLength(contours[i], true), true); // 遍历逼近后的点 for (int j = 0; j < approx.Size; j++) { // 获取直线的两个端点 Point p1 = approx[j]; Point p2 = approx[(j + 1) % approx.Size]; // 判断直线端点是否在轮廓内 if (CvInvoke.PointPolygonTest(contours[i], p1, false) > 0 && CvInvoke.PointPolygonTest(contours[i], p2, false) > 0) { // 直线与轮廓有交点,输出交点坐标 Console.WriteLine("Line intersection point: ({0}, {1})", p1.X, p1.Y); } } } ``` 其中,`CvInvoke.Imread()` 方法用于读取图像并转换为灰度图像,`CvInvoke.Canny()` 方法用于进行 Canny 边缘检测,`CvInvoke.FindContours()` 方法用于寻找轮廓,`CvInvoke.ApproxPolyDP()` 方法用于对轮廓进行逼近,`CvInvoke.PointPolygonTest()` 方法用于判断直线端点是否在轮廓内。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值