Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Try this. You may modified to suit your need.

#!/usr/bin/python3.7.3
#OpenCV 4.1.2, Raspberry pi 3/3b/4, Buster ver10
#Date: 2nd February, 2019

import cv2

img = cv2.imread('bean1.jpg')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 85, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

cv2.drawContours(img, contours, -1, (255, 255, 255), cv2.FILLED)
cv2.drawContours(imgray, contours, -1, (255, 255, 255), cv2.FILLED)

cv2.imshow('Image', img)
cv2.imshow('Image GRAY', imgray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output Image:

image description

Output Imgray:

image description