-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv8.py
59 lines (47 loc) · 1.79 KB
/
opencv8.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
#!/usr/bin/env python3
import numpy as np
import cv2
# it all comes down to fine tuning of values
def read_rgb_image(image_name, show):
rgb_image = cv2.imread(image_name)
if show:
cv2.imshow("RGB Image",rgb_image)
return rgb_image
def convert_rgb_to_gray(rgb_image,show):
gray_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2GRAY)
if show:
cv2.imshow("Gray Image",gray_image)
return gray_image
def convert_gray_to_binary(gray_image, adaptive, show):
if adaptive:
binary_image = cv2.adaptiveThreshold(gray_image,
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 115, 2)
else:
_,binary_image = cv2.threshold(gray_image,127,255,cv2.THRESH_BINARY_INV)
if show:
cv2.imshow("Binary Image", binary_image)
return binary_image
def getContours(binary_image):
contours, hierarchy = cv2.findContours(binary_image,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
return contours
def draw_contours(image, contours, image_name):
index = -1 #means all contours
thickness = 2 #thickness of the contour line
color = (255, 0, 255) #color of the contour line
cv2.drawContours(image, contours, index, color, thickness)
cv2.imshow(image_name,image)
def main():
image_name = "images/shapes.jpg"
rgb_image = read_rgb_image(image_name, True)
gray_image= convert_rgb_to_gray(rgb_image,True)
binary_image = convert_gray_to_binary(gray_image, True, True)
contours = getContours(binary_image)
draw_contours(rgb_image, contours,"RGB Contours")
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()