-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv6.py
48 lines (36 loc) · 1.38 KB
/
opencv6.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
#!/usr/bin/env python3
# Thresholding
import numpy as np
import cv2
def read_image(image_name, as_gray):
if as_gray:
image = cv2.imread(image_name,cv2.IMREAD_GRAYSCALE)
else:
image = cv2.imread(image_name,cv2.IMREAD_COLOR)
cv2.imshow("Image",image)
return image
def basic_thresholding(gray_image, threshold_value):
ret, thresh_basic = cv2.threshold(gray_image,
threshold_value,
255,
cv2.THRESH_BINARY_INV)
cv2.imshow("Basic Binary Image",thresh_basic)
def adaptive_thresholding(gray_image, threshold_value):
adaptive_threshold_image = cv2.adaptiveThreshold(gray_image,
255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV,
threshold_value,
2)
cv2.imshow("Adaptive Threshold Image",adaptive_threshold_image)
def main():
image_name = "images/tree.jpg"
as_gray = True
threshold_value=111
gray_image = read_image(image_name,as_gray)
basic_thresholding(gray_image, threshold_value)
adaptive_thresholding(gray_image, threshold_value)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()