import cv2
import numpy as np
import matplotlib.pyplot as plt
import random
np.random.seed=50
random_points = np.random.randint(100, 400, 20)
random_points = random_points.reshape(10, 2)
# Create a black image
image = np.zeros((512,512,3), np.uint8)
for point in random_points:
cv2.circle(image, (point[0],point[1]),8,(0,255,0),-1)
plt.imshow(image[...,::-1])
<matplotlib.image.AxesImage at 0x1f0d784ab20>
hull = cv2.convexHull(random_points)
cv2.drawContours(image, [hull], -1, (0,255,255), 4)
plt.imshow(image[...,::-1])
<matplotlib.image.AxesImage at 0x1f0d789b7c0>
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load source image
bgr_image = cv2.imread("images/star.png")
# Convert image to gray and blur it
src_gray = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(src_gray, 127, 255, 0)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Find the convex hull object for each contour
hull_list = []
for i in range(len(contours)):
hull = cv2.convexHull(contours[i])
hull_list.append(hull)
# Draw contours + hull results
cv2.drawContours(bgr_image, contours, -1, (0,255,0), 1)
cv2.drawContours(bgr_image, hull_list, -1, (0,0,255),1)
plt.figure(figsize=[7,7])
plt.imshow(bgr_image[...,::-1])
<matplotlib.image.AxesImage at 0x1f0d78f4dc0>
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load source image
bgr_image = cv2.imread("images/shapes.png")
# Convert image to gray and blur it
src_gray = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(src_gray, 127, 255, 0)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Find the convex hull object for each contour
hull_list = []
for i in range(len(contours)):
hull = cv2.convexHull(contours[i])
hull_list.append(hull)
# Draw contours + hull results
cv2.drawContours(bgr_image, contours, -1, (0,255,0), 2)
cv2.drawContours(bgr_image, hull_list, -1, (0,0,255),1)
plt.figure(figsize=[7,7])
plt.imshow(bgr_image[...,::-1])
<matplotlib.image.AxesImage at 0x1f0d6704820>