-
Notifications
You must be signed in to change notification settings - Fork 0
/
task1.py
43 lines (31 loc) · 1.1 KB
/
task1.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
import cv2 as cv
import matplotlib.pyplot as plt
class A:
def __init__(self):
pass
def object(self, image):
image = cv.imread(image, 0)
(thresh, black) = cv.threshold(image, 80, 255, cv.THRESH_BINARY_INV)
color = {"white": 0, "black": 0}
for i in black:
for a in i:
if a == 255:
color["white"] +=1
else:
color["black"] += 1
if color["white"] > color["black"]:
balance = 256 / (int(color["white"] / color["black"]))
else:
balance = 256 / (int(color["black"] / color["white"]))
if balance < 80:
balance = 127
if color["black"] > color["white"]:
(thresh, black) = cv.threshold(image, balance, 255, cv.THRESH_BINARY)
else:
(thresh, black) = cv.threshold(image, balance, 255, cv.THRESH_BINARY_INV)
cv.imwrite('black_image.jpg', black)
image = cv.imread('black_image.jpg')
plt.figure(num='Task 1')
plt.axis("off")
plt.imshow(image)
return image