【问题标题】:How to detect octogonal shapes using contours如何使用轮廓检测​​八角形
【发布时间】:2018-05-30 19:42:14
【问题描述】:

我想检测带有轮廓的八角形停车标志,如下图所示,但我不知道它是如何完成的

停车标志:

我已经设法检测到三角形

    Mat ROI = new Mat();
    Mat bgrClone = bgr.clone();
    MatOfPoint approxContour = new MatOfPoint();
    MatOfPoint2f approxContour2f = new MatOfPoint2f();
    List<MatOfPoint> contourDraw = new ArrayList<MatOfPoint>();

    for(int i = 0; i < contourList.size(); i++) {
        MatOfPoint2f contour2f = new MatOfPoint2f(contourList.get(i).toArray());
         double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;//0.225
         Imgproc.approxPolyDP(contour2f, approxContour2f, approxDistance, true);

       approxContour2f.convertTo(approxContour, CvType.CV_32S);
    if (approxContour.size().height == 3 && (Imgproc.contourArea(contour2f) > 3000) ) { //&& (Imgproc.contourArea(contour2f) > 5000)

        contourDraw.add(approxContour);
        Imgproc.drawContours(bgr, contourDraw, -1, new Scalar(0,255,0), 1);

        Rect cord = Imgproc.boundingRect(approxContour);
        Core.rectangle(bgr, new Point(cord.x, cord.y), new Point(cord.x+cord.width, cord.y+cord.height),new Scalar(0,255,0), 1);

        ROI = bgrClone.submat(cord.y, cord.y+cord.height, cord.x, cord.x+cord.width);
        showResult(ROI);
    }
  }

我正在使用Java,但任何语言的任何人都将不胜感激。

【问题讨论】:

  • 我用python中的实现总结了我的答案。看看!!

标签: java opencv image-processing computer-vision detection


【解决方案1】:

您可以使用 OpenCV 的轮廓执行形状检测。我在python中实现了以下内容。

步骤:

  1. 由于您正在处理交通标志,因此它们中的大多数都是红色的。在 RGB 颜色空间中,红色通道中的红色量最高。但是由于白色也包含红色,所以我没有使用它。我宁愿使用红色内容最少的蓝色通道。

蓝色通道图片:

  1. 接下来,我将 Otsu 阈值应用于上面的图像并将其反转。

大津阈值和反转:

  1. 最后我找到了轮廓并标记了这些轮廓,它们周围大约有 8 条弧线。

最终结果:

代码:

import numpy as np
import cv2
import os

path = r'C:\Desktop\Stack\contour\shape_detection'
filename = 'stop.png'

img = cv2.imread(os.path.join(path, filename), 1)
cv2.imshow('img1',img[:,:,0])

ret,thresh1 = cv2.threshold(img[:,:,0], 0, 255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
cv2.imshow('thresh1', thresh1)

_, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for cnt in contours:
    approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
    print len(approx)
    if len(approx)==8:
        print "octagon"
        cv2.drawContours(img, [cnt], 0, (0, 255, 0), 6)

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

【讨论】:

  • 是的,请分享代码,非常感谢您的帮助:))
猜你喜欢
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 2020-05-27
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多