-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDers7.py
More file actions
99 lines (68 loc) · 2.3 KB
/
Ders7.py
File metadata and controls
99 lines (68 loc) · 2.3 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# coding: utf-8
# In[1]:
import matplotlib.pyplot as plt
import numpy as np
import math
from scipy.misc import imsave
get_ipython().magic('matplotlib inline')
def convert_RGB_to_gray_level(image_1):
img_1=plt.imread(image_1)
img_2=np.zeros((img_1.shape[0],img_1.shape[1]))
for i in range(img_2.shape[0]):
for j in range(img_2.shape[1]):
img_2[i,j]=img_1[i,j,0]/3+img_1[i,j,1]/3+img_1[i,j,2]/3
# print(img_1.shape)
return img_2
# In[2]:
def convert_RGB_to_monochrome_BW(image_1,threshold=100):
img_1=plt.imread(image_1)
img_2=np.zeros((img_1.shape[0],img_1.shape[1]))
for i in range(img_2.shape[0]):
for j in range(img_2.shape[1]):
if (img_1[i,j,0]/3+img_1[i,j,1]/3+img_1[i,j,2]/3)>threshold:
# img_2[i,j]=img_1[i,j,0]/3+img_1[i,j,1]/3+img_1[i,j,2]/3
img_2[i,j]=1
else:
img_2[i,j]=0
# print(img_1.shape)
return img_2
# In[3]:
def create_mask_internal():
i_m_1=np.array([[1,0],[0,0]])
i_m_2=np.array([[0,1],[0,0]])
i_m_3=np.array([[0,0],[1,0]])
i_m_4=np.array([[0,0],[0,1]])
i_m_l=[i_m_1,i_m_2,i_m_3,i_m_4]
return i_m_l
# In[4]:
def create_mask_external():
e_m_1=np.array([[0,1],[1,1]])
e_m_2=np.array([[1,0],[1,1]])
e_m_3=np.array([[1,1],[0,1]])
e_m_4=np.array([[1,1],[1,0]])
e_m_l=[e_m_1,e_m_2,e_m_3,e_m_4]
return e_m_l
# In[5]:
def count_object(image_name_with_path,threshold=150):
img_file_1=image_name_with_path
img_file_2=convert_RGB_to_gray_level(img_file_1)
img_file_3=convert_RGB_to_monochrome_BW(img_file_1,threshold)
image=img_file_3
c_1=0
c_2=0
m,n=image.shape
for i in range(m-1):
for j in range(n-1):
for mask in create_mask_internal():
if False not in (img_file_3[i:i+2,j:j+2]==mask):
#print("e mask bulundu")
c_1=c_1+1
for mask in create_mask_external():
if False not in (img_file_3[i:i+2,j:j+2]==mask):
#print("i mask bulundu")
c_2=c_2+1
number_of_objects=math.fabs((c_2-c_1)/4)
print("resimde toplam T sayısı : ",number_of_objects)
return number_of_objects
image_file_1="image.jpg"
count_object(image_file_1)