-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_threshold.py
More file actions
37 lines (25 loc) · 964 Bytes
/
13_threshold.py
File metadata and controls
37 lines (25 loc) · 964 Bytes
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
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
#图像二值化
def threshold_demo(image):
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
print("threshold value is %s"%ret)
cv.imshow("threshold_demo",binary)
def local_threshold(image):
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
binary = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY_INV,25,10)
cv.imshow("local_threshold",binary)
def custom_threshold(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
mean = gray.mean()
ret, binary = cv.threshold(gray, mean, 255, cv.THRESH_BINARY)
print("threshold value is %s" % ret)
cv.imshow("custom_threshold", binary)
src = cv.imread("C:/1/1.jpg")
cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE)
cv.imshow("input_image",src)
custom_threshold(src)
cv.waitKey(0)
cv.destroyAllWindows()