Python'da Görüntü İşleme - Sparse Matrix ve Dictionary Class Yapısı
import matplotlib.pyplot as plt
import numpy as np
import math
from scipy.misc import imsave
%matplotlib inline
import math
from scipy.misc import imsave
%matplotlib inline
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,1]/3)>threshold:
img_2[i,j]=1
else:
img_2[i,j]=0
return img_2
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,1]/3)>threshold:
img_2[i,j]=1
else:
img_2[i,j]=0
return img_2
image_file_1=r"test_t_one.jpg"
img_1=convert_RGB_to_monochrome_BW(image_file_1,threshold=100)
plt.imshow(img_1,cmap='gray')
plt.show()
img_1=convert_RGB_to_monochrome_BW(image_file_1,threshold=100)
plt.imshow(img_1,cmap='gray')
plt.show()
img_1.ndim, img_1.shape
_______________________
_______________________
(2, (372, 243))
____________________________
i2=img_1.reshape(1,img_1.shape[0]*img_1.shape[1])
i2.ndim, i2.shape
i2.ndim, i2.shape
____________________________
(2, (1, 90396))
def get_Histogram(image): #image monochrome olmalı
my_Histogram={}
for i in range(image.shape[0]):
for j in range(image.shape[1]):
item=image[i,j]
if item in my_Histogram.keys():
my_Histogram[item]=my_Histogram[item]+1
else:
my_Histogram[item]=1
return my_Histogram
my_Histogram=get_Histogram(img_1)
my_Histogram
my_Histogram={}
for i in range(image.shape[0]):
for j in range(image.shape[1]):
item=image[i,j]
if item in my_Histogram.keys():
my_Histogram[item]=my_Histogram[item]+1
else:
my_Histogram[item]=1
return my_Histogram
my_Histogram=get_Histogram(img_1)
my_Histogram
______________________
{0.0: 3564, 1.0: 86832}
______________________
x=my_Histogram[0.0]
y=my_Histogram[1.0]
print("siyah beyaz oranı: ",x/y)
y=my_Histogram[1.0]
print("siyah beyaz oranı: ",x/y)
______________________
siyah beyaz oranı: 0.041044776119402986
______________________
class myMatrix():
def __init__(self,x,y):
self.D=x
self.F=y
def __init__(self,x,y):
self.D=x
self.F=y
def create_D_F(img_1):
d={(i,j) for i in range(img_1.shape[0])
for j in range(img_1.shape[1])
if img_1[i,j]==1
}
f={(i,j):1 for i,j in d}
m=myMatrix(d,f)
return m
d={(i,j) for i in range(img_1.shape[0])
for j in range(img_1.shape[1])
if img_1[i,j]==1
}
f={(i,j):1 for i,j in d}
m=myMatrix(d,f)
return m
my_sparce_Matrix_1=create_D_F(img_1)
my_sparce_Matrix_1.D, my_sparce_Matrix_1.F
my_sparce_Matrix_1.D, my_sparce_Matrix_1.F
____________________________
({(252, 36),
(289, 22),
(312, 241),
(111, 227),
(185, 101),
(5, 178),
(208, 66),
(171, 86),
(261, 216),
(194, 237),
(67, 137),
(157, 23),
(360, 157),
(90, 42),
(53, 166),
(143, 36),
(309, 204),
(76, 201),
(369, 81),
(242, 161),
(332, 35),
(9, 0),
(318, 26),
(191, 232),
(289, 22),
(312, 241),
(111, 227),
(185, 101),
(5, 178),
(208, 66),
(171, 86),
(261, 216),
(194, 237),
(67, 137),
(157, 23),
(360, 157),
(90, 42),
(53, 166),
(143, 36),
(309, 204),
(76, 201),
(369, 81),
(242, 161),
(332, 35),
(9, 0),
(318, 26),
(191, 232),
…
Yorumlar
Yorum Gönder