SoFunction
Updated on 2024-10-29

python based opencv to draw image contours

Image Contour Concept

A silhouette is a series of connected points forming a curve that represents the basic shape of an object.
When we talk about contours we can't help but think about edges, and they are indeed very similar. Simply put, contours are continuous, edges are not all continuous (Figure below). In fact, the edge is mainly used as an image feature, such as the edge features can be used to distinguish between the face and hands; while the outline is mainly used to analyze the shape of the object, such as the perimeter and area of the object, etc., it can be said that the edge includes the outline.

The operation of finding contours is generally used for binary images, so it is common to use threshold segmentation or Canny edge detection to get a binary map first.

Note: Finding outlines is for white objects, make sure the object is white and the background is black, otherwise many people will find a box at the very outside of the picture when looking for outlines.

opencv find image contours

Use () to find the outline:

import cv2 as cv
import numpy as np

img = ('')
img_gray = (img, cv.COLOR_BGR2GRAY)
ret, thresh = (img_gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)

# Finding the contours of a binary image
contours, hierarchy = (
  thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

print(len(contours))
  • Parameter 2: the way to find the contour, usually use cv.RETR_TREE, it means to extract all the contours and establish the hierarchy between the contours.
  • Parameter 3: Approximation method of the contour. For example, for a line, we can store all the pixel points of the line, or just the start and end points. Using cv.CHAIN_APPROX_SIMPLE means to represent the contour with as few pixel points as possible.
  • For simplicity, these two parameters can also be directly represented by the truth values 3 and 2.
  • The function has 2 return values, hierarchy is the hierarchical relationship between the contours, this is ignored. We mainly look at contours, it is the contours found, stored in the form of a chain table, recording the coordinates of all the pixel points of each contour (x,y).

opencv draw image outline

Once the contours are found, they can be drawn in red as in the picture:()

(img, contours, -1, (0, 0, 255), 2)
Where parameter 2 is the contours obtained, parameter 3 indicates which contour to draw, -1 means draw all contours, parameter 4 is the color (B/G/R channel, so (0,0,255) means red), and parameter 5 is the line width.

Lesson learned: many people draw with color obviously, but no effect, please check which chart you are drawing on, drawing on grayscale charts and binary charts obviously no color.

Generally, we will first obtain the contour to be manipulated, and then contour draw and analyze it:
cnt = contours[1]
(img, [cnt], 0, (0, 0, 255), 2)

Experiment: Finding and Drawing Image Outlines

import cv2 as cv
import numpy as np

img = ('')
img_gray = (img, cv.COLOR_BGR2GRAY)
ret, thresh = (img_gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)

# Finding the contours of a binary image
contours, hierarchy = (
  thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[1:6]

(img, cnt, -1, (0, 0, 255), 2)

('result',img)
(0)
()

Results

Above is python based on opencv draw image contour details, more information about python draw image contour please pay attention to my other related articles!