-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32 Adaptive thresholding.py
38 lines (27 loc) · 1.3 KB
/
32 Adaptive thresholding.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
"""
@author: Osama Shakeel
Adaptive thresholding -
We use it beacuse simple thresholding not able to handle different type of low luminous pixels
this, the algorithm calculate the threshold for a small regions of the image.
so we get multiple threshold for diff. regions in same image.
Adaptive Method - It decides how thresholding value is calculated.
cv2.ADAPTIVE_THRESH_MEAN_C : threshold value is the mean of neighbourhood area.
cv2.ADAPTIVE_THRESH_GAUSSIAN_C : threshold value is the weighted sum of neighbourhood values where weights are a gaussian window.
threshold (img,pixel_thresh,_max_thresh_pixel,method,style,no._of_pixel,contact_mean)
"""
import cv2
import numpy as np
img =cv2.imread("D://music//page.jpg", 0)
img = cv2.resize(img, (300,300))
_, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
#_, th1 = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img, 255,cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,11,2 )
th3 = cv2.adaptiveThreshold(img, 255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,11,2 )
cv2.imshow("orignal", img)
cv2.imshow("Binary threshold ==", th1)
cv2.imshow("Adaptive 1 ==", th2)
cv2.imshow("Adaptive 2 ==", th3)
cv2.waitKey(0)
cv2.destroyAllWindows()