Skip to content

Instantly share code, notes, and snippets.

@soura-b
Created August 27, 2021 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soura-b/5da373d251de551566719d6b8692d270 to your computer and use it in GitHub Desktop.
Save soura-b/5da373d251de551566719d6b8692d270 to your computer and use it in GitHub Desktop.
Contour detection after pre-processing with diltion
import cv2
image = cv2.imread('foot2.jpeg', 1) # 1 for color, 0 for greyscale
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# ******** #
# Dilation #
# ******** #
from skimage.morphology import (erosion, dilation)
from skimage.morphology import disk
footprint = disk(1)
dilated = dilation(img_gray, footprint)
cv2.imshow('gray', img_gray)
cv2.imshow('dilation', dilated)
# ************************************ #
# Binary Thresholding on Dilated image #
# ************************************ #
ret, thresh = cv2.threshold(dilated, 30, 255, cv2.THRESH_BINARY)
cv2.imshow('binary thresholding', thresh)
# detect the contours on the binary image using cv2.CHAIN_APPROX_SIMPLE
contours, hierarchy = cv2.findContours(image=thresh, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)
print(hierarchy)
# result format [Next, Previous, First_Child, Parent]
# draw contours on the original image
image_copy = image.copy()
cv2.drawContours(image=image_copy, contours=contours, contourIdx=-1, color=(0, 255, 0), thickness=2,
lineType=cv2.LINE_AA)
# see the results
cv2.imshow('None approximation', image_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment