Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to detect octogonal shapes using contours

I want to detect octagonal stop sign like the following image with contours but i can't figure out how it is done

Stop sign

I already manage to detect triangles

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

}