首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在opencv或emgu cv中检测三角形边缘?

如何在opencv或emgu cv中检测三角形边缘?
EN

Stack Overflow用户
提问于 2012-07-11 09:51:13
回答 1查看 8.7K关注 0票数 3

我正在使用Emgu,我想检测图片中的两个锐度,首先我将图像转换为灰色,然后调用cvCanny,然后调用FindContours,但只找到了一个轮廓,找不到三角形。

代码:

代码语言:javascript
运行
复制
 public static void Do(Bitmap bitmap, IImageProcessingLog log)
    {
        Image<Bgr, Byte> img = new Image<Bgr, byte>(bitmap);
        Image<Gray, Byte> gray = img.Convert<Gray, Byte>();
        using (Image<Gray, Byte> canny = new Image<Gray, byte>(gray.Size))
        using (MemStorage stor = new MemStorage())
        {
            CvInvoke.cvCanny(gray, canny, 10, 5, 3);
            log.AddImage("canny",canny.ToBitmap());

            Contour<Point> contours = canny.FindContours(
             Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
             Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE,
             stor);

            for (int i=0; contours != null; contours = contours.HNext)
            {
                i++;
                MCvBox2D box = contours.GetMinAreaRect();

                Image<Bgr, Byte> tmpImg = img.Copy();
                tmpImg.Draw(box, new Bgr(Color.Red), 2);
                log.AddMessage("contours" + (i) +",angle:"+box.angle.ToString() + ",width:"+box.size.Width + ",height:"+box.size.Height);
                log.AddImage("contours" + i, tmpImg.ToBitmap());
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-11 13:39:57

(我不知道emguCV,但我会告诉你这个想法)

您可以按如下方式进行操作:

  1. 使用split()函数将图像分割为R、G、B平面。对于每个平面,应用Canny
  2. detection.
  3. Then查找其中的等高线,并使用approxPolyDP函数近似每个等高线。
  4. 如果近似等高线中的坐标数为3,则最有可能是三角形,并且该值对应于三角形的3个顶点。

以下是python代码:

代码语言:javascript
运行
复制
import numpy as np
import cv2

img = cv2.imread('softri.png')

for gray in cv2.split(img):
    canny = cv2.Canny(gray,50,200)

    contours,hier = cv2.findContours(canny,1,2)
    for cnt in contours:
        approx = cv2.approxPolyDP(cnt,0.02*cv2.arcLength(cnt,True),True)
        if len(approx)==3:
            cv2.drawContours(img,[cnt],0,(0,255,0),2)
            tri = approx

for vertex in tri:
    cv2.circle(img,(vertex[0][0],vertex[0][1]),5,255,-1)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

下面是蓝色平面的精妙示意图:

下面是最终输出,三角形及其顶点分别标记为绿色和蓝色:

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11424466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档