Welcome to My Blog 👋

Java, Spring Framework, Microservices, Docker, Kubernetes, AWS and Others 🚀
Follow Me

Görüntü İşleme Dersleri Hafta 11 - ÇOMÜ



  December 15, 2017    Labels:,, 

6
import matplotlib.pyplot as plt
7
import numpy as np
8
import math
9
from scipy.misc import imsave
10
get_ipython().magic('matplotlib inline')
11
12
13
def convert_RGB_to_monochrome_BW(image_1,threshold=100):
14
img_1=plt.imread(image_1)
15
img_2=np.zeros((img_1.shape[0],img_1.shape[1]))
16
for i in range(img_2.shape[0]):
17
for j in range(img_2.shape[1]):
18
if(img_1[i,j,0]/3+img_1[i,j,1]/3+img_1[i,j,1]/3)>threshold:
19
img_2[i,j]=1
20
else:
21
img_2[i,j]=0
22
return img_2
23
24
25
def create_mask_internal():
26
i_m_1=np.array([[1,0],[0,0]])
27
i_m_2=np.array([[0,1],[0,0]])
28
i_m_3=np.array([[0,0],[1,0]])
29
i_m_4=np.array([[0,0],[0,1]])
30
i_m_l=[i_m_1,i_m_2,i_m_3,i_m_4]
31
32
return i_m_l
33
34
def create_mask_external():
35
36
e_m_1=np.array([[0,1],[1,1]])
37
e_m_2=np.array([[1,0],[1,1]])
38
e_m_3=np.array([[1,1],[0,1]])
39
e_m_4=np.array([[1,1],[1,0]])
40
e_m_l=[e_m_1,e_m_2,e_m_3,e_m_4]
41
42
return e_m_l
43
44
def count_object(image_name_with_path,threshold=150):
45
img_file_1=image_name_with_path
46
img_file_2=convert_RGB_to_gray_level(img_file_1)
47
img_file_3=convert_RGB_to_monochrome_BW(img_file_1,threshold)
48
image=img_file_3
49
c_1=0
50
c_2=0
51
m,n=image.shape
52
for i in range(m-1):
53
for j in range(n-1):
54
for mask in create_mask_internal():
55
if False not in (img_file_3[i:i+2,j:j+2]==mask):
56
#print("e mask bulundu")
57
c_1=c_1+1
58
for mask in create_mask_external():
59
if False not in (img_file_3[i:i+2,j:j+2]==mask):
60
#print("i mask bulundu")
61
c_2=c_2+1
62
number_of_objects=math.fabs((c_2-c_1)/4)
63
print("resimde toplam T sayısı : ",number_of_objects)
64
return number_of_objects
65
66
def convert_RGB_to_gray_level(image_1):
67
68
img_1=plt.imread(image_1)
69
70
img_2=np.zeros((img_1.shape[0],img_1.shape[1]))
71
72
for i in range(img_2.shape[0]):
73
74
for j in range(img_2.shape[1]):
75
76
img_2[i,j]=img_1[i,j,0]/3+img_1[i,j,1]/3+img_1[i,j,1]/3
77
return img_2
78
79
image_file_1=r"GG.jpg"
80
image_file_2=r"GG.jpg"
81
82
image_file_1=convert_RGB_to_monochrome_BW(image_file_1,threshold=100)
83
plt.imshow(image_file_1,cmap='gray')
84
plt.show()
85
count_object(image_file_2)


No comments:

Post a Comment