如何使用轮廓检测八角形
How to detect octogonal shapes using contours
我想检测八角形停车标志,如下图所示,但我不知道它是如何完成的
停车标志:
我已经设法检测到三角形
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
但任何一种语言都将不胜感激。
您可以使用 OpenCV 的轮廓进行形状检测。我在 python.
中实现了以下内容
步骤:
- 由于您处理的是交通标志,因此它们中的大多数都是红色的。在 RGB 颜色 space 中,红色的量在红色通道中最高。但由于白色也包含红色,所以我没有使用它。我宁愿使用红色内容最少的蓝色通道。
蓝色通道图像:
- 接下来我对上面的图像应用 Otsu 阈值并将其反转。
Otsu 阈值和反转:
- 最后我找到了等高线并标记了那些大约有 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()
我想检测八角形停车标志,如下图所示,但我不知道它是如何完成的
停车标志:
我已经设法检测到三角形
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
但任何一种语言都将不胜感激。
您可以使用 OpenCV 的轮廓进行形状检测。我在 python.
中实现了以下内容步骤:
- 由于您处理的是交通标志,因此它们中的大多数都是红色的。在 RGB 颜色 space 中,红色的量在红色通道中最高。但由于白色也包含红色,所以我没有使用它。我宁愿使用红色内容最少的蓝色通道。
蓝色通道图像:
- 接下来我对上面的图像应用 Otsu 阈值并将其反转。
Otsu 阈值和反转:
- 最后我找到了等高线并标记了那些大约有 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()