如何在opencv或emgu cv中检测三角形边缘? [英] How to detect triangle edge in opencv or emgu cv?
本文介绍了如何在opencv或emgu cv中检测三角形边缘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Emgu CV,我想检测图片中的两个锐度,首先将图像转换为灰色,然后调用cvCanny,然后调用FindContours,但是只找到一个轮廓,找不到三角形.
I am using Emgu CV, i want to detect two sharp in the picture, first i convert the image to gray,and call cvCanny, then call FindContours, but just one contour found, the triangle not found.
代码:
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,但我会告诉你的想法)
(I don't know emguCV, but i will give you the idea)
您可以按照以下步骤进行操作:
You can do it as follows:
- 使用
split()
功能将图像分割为R,G,B平面. - 对于每个平面,应用Canny边缘检测.
- 然后在其中找到轮廓,并使用
approxPolyDP
函数逼近每个轮廓. - 如果近似轮廓中的坐标数为3,则很可能是三角形,并且该值对应于三角形的3个顶点.
- Split the image to R,G,B planes using
split()
function. - For each plane, apply Canny edge detection.
- Then find the contours in it and approximate each contour using
approxPolyDP
function. - if number of coordinates in the approximated contour is 3, it is most likely a triangle and the values corresponds to 3 vertices of triangles.
下面是python代码:
Below is the python code :
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图:
Below is canny diagram of blue color plane:
下面是最终输出,三角形及其顶点分别用绿色和蓝色标记:
Below is the final output, triangle and its vetices are marked in green and blue colors respectively:
这篇关于如何在opencv或emgu cv中检测三角形边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文