This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
59 lines (42 loc) · 1.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import cv2 as cv
import numpy as np
# Color of a lake [blue green red]
BGR = np.array([255, 218, 170])
upper = BGR + 10
lower = BGR - 10
# Read an image from disk
# @param {path} the path of the image to read
# @returns {image} the image
def read_image(path):
return cv.imread(path)
# applies a threshold to an image based on two boundaries
# @param {image} the image to threshold
# @param {Array[int, int, int]} lower threshold in BGR
# @param {Array[int, int, int]} upper threshold in BGR
def find_mask(image):
return cv.inRange(image, lower, upper)
def find_contours(mask):
(cnts, hierarchy) = cv.findContours(
mask.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
print("Found %d black shapes" % (len(cnts)))
return cnts
# draw contours on an image
# @param {list[[int, int]]} an array of [int, int] points to draw
# @param {image} the image to draw the points on
def show_contours(contours, image):
for contour in contours:
cv.drawContours(image, contour, -1, (0, 0, 255), 2)
# cv.imshow("contours", image)
# cv.waitKey(0)
cv.imwrite('output.png', image)
# def get_main_contour(contours):
# copy = contours.copy()
# copy.sort(key=len, reverse=True)
# return copy[0]
if __name__ == "__main__":
image = read_image("input.png")
mask = find_mask(image)
contours = find_contours(mask)
# main_contour = get_main_contour(contours)
show_contours([contours], image)
key = cv.waitKey(0)