Python OpenCV draw contour only on the outside border

Nimrod Shory picture Nimrod Shory · Nov 19, 2015 · Viewed 8.6k times · Source

When drawing a contour using OpenCV's drawContours the borders is drawn centered to the contour, I want to draw the border only on the outside of the contour.

This image (taken from the SketchUp documentation) explains it best: enter image description here

drawContours draws the contour like in the first circle (the contour is in the middle of the drawn border). I need to have the border only on the outside of the contour, like in the last circle.

Anyone have an idea as to how can I achieve this kind of behaviour?

Thanks.

Answer

AdityaIntwala picture AdityaIntwala · Nov 19, 2015

use the code as

  _ret, contours, hierarchy = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 
  cv2.drawContours(img,contours , -1, (255,0,0), 1)

here cv2.RETR_EXTERNAL gives only the external detected contour.