gpt4 book ai didi

opencv - 如何检测opencv或emgu cv中的三角形边缘?

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:57 24 4
gpt4 key购买 nike

enter image description here

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

代码:

 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());
}
}
}

最佳答案

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

您可以按如下方式进行:

  1. 使用 split() 函数将图像拆分为 R、G、B 平面。
  2. 对于每个平面,应用 Canny 边缘检测。
  3. 然后在其中找到轮廓并使用 approxPolyDP 函数逼近每个轮廓。
  4. 如果近似轮廓中的坐标数为3,则很可能是三角形,其值对应于三角形的3个顶点。

下面是python代码:

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()

下面是蓝色平面的canny图:

enter image description here

下面是最终的输出,三角形和它的顶点分别用绿色和蓝色标记:

enter image description here

关于opencv - 如何检测opencv或emgu cv中的三角形边缘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11424466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com