Github : https://github.com/bboz/Image_Processing
1.Hafta Ders İçeriği
import matplotlib.pyplot as plt #import matplotlib.image as mpimg #img_mpimg.imread('test1.jpg') #%matplotlib inline import numpy as np im_1=plt.imread("1.jpg") plt.imshow(im_1) plt.show() def fonk1(image_1): print("Resmin boyutu = ",image_1.ndim,"\n") print("Resmin Shape değeri = ",image_1.shape,"\n") print("Red için min değer = ",image_1[:,:,0].min(),"\n") #':' hangi yerde kullanıldıysa oranın tamamından bahsediyor örneğin burada tüm satırlar ve sütünlar print("Red için max değer = ",image_1[:,:,0].max(),"\n") # şuan kırmızı için olan değerlere bakıyoruz 1 olsa yeşil 2 olursa mavi fonk1(im_1) im_1[:,:,0]=im_1[:,:,0]+50 plt.imshow(im_1) plt.show() def my_function_1(my_img): print("eksen sayisi : ",my_img.ndim) print("eksen degerleri :",my_img.shape) print("en kucuk kirmizi renk degeri : ",np.min(my_img[:,:,0])) print("en kucuk kirmizi renk degeri : ",np.max(my_img[:,:,0])) print("en kucuk yesil renk degeri : ",np.min(my_img[:,:,1])) print("en kucuk yesil renk degeri : ",np.max(my_img[:,:,1])) print("en kucuk mavi renk degeri : ",np.min(my_img[:,:,2])) print("en kucuk mavi renk degeri : ",np.max(my_img[:,:,2])) my_function_1(im_1)
2.Hafta Ders İçeriği
import os cwd = os.getcwd() cwd os.chdir("C:\\imageProcessing2018") import matplotlib.pyplot as plt import numpy as np im_1=plt.imread("1.jpg") plt.imshow(im_1) plt.show() from scipy import stats from scipy.stats import iqr from scipy.stats import skew #mean=sum(img)/len(img) #plt.subplot(1,2,2),plt.imshow(255-img) im_1.setflags(write=1) # resim sadece read-only özelliğine sahipti üzerinde değişiklik yapabilmek için değiştirdim im_1.flags im_1[:,:,0]=im_1[:,:,0]+150 # resim üzerinde kırmızı tonu için renk değişimi plt.imshow(im_1) plt.show() def my_function(x): return 255-x def inverse(image): image[:,:,0]=my_function(image[:,:,0]) image[:,:,1]=my_function(image[:,:,1]) image[:,:,2]=my_function(image[:,:,2]) def mean(image): print("Kirmizi icin renk ortalamasi : ",np.mean(image[:,:,0])) print("Yesil icin renk ortalamasi : ",np.mean(image[:,:,1])) print("Mavi icin renk ortalamasi : ",np.mean(image[:,:,2])) def median(image): print("Kirmizi icin renk ortanca degeri : ",np.median(image[:,:,0])) print("Yesil icin renk ortanca degeri : ",np.median(image[:,:,1])) print("Mavi icin renk ortanca degeri : ",np.median(image[:,:,2])) def mode(image): print("Kirmizi icin renk modu : ",stats.mode(image[:,:,0])) print("Yesil icin renk modu : ",stats.mode(image[:,:,1])) print("Mavi icin renk modu : ",stats.mode(image[:,:,2])) def my_H(image): H={} for i in range (image.shape[0]): for j in range (image.shape[1]): if(image[i,j,0] in H.keys()): H[image[i,j,0]]=H[image[i,j,0]]+1 else: H[image[i,j,0]]=1 if(image[i,j,1] in H.keys()): H[image[i,j,1]]=H[image[i,j,1]]+1 else: H[image[i,j,1]]=1 if(image[i,j,2] in H.keys()): H[image[i,j,2]]=H[image[i,j,2]]+1 else: H[image[i,j,2]]=1 plt.bar(list(H.keys()),list( H.values()), color='r') plt.show() def ceyreklik(image): print("Kirmizi icin Q1 degeri = ",np.percentile(image[:,:,0],25)) print("Kirmizi icin Q2 degeri = ",np.percentile(image[:,:,0],50)) print("Kirmizi icin Q3 degeri = ",np.percentile(image[:,:,0],75)) print("Yesil icin Q1 degeri = ",np.percentile(image[:,:,1],25)) print("Yesil icin Q2 degeri = ",np.percentile(image[:,:,1],50)) print("Yesil icin Q3 degeri = ",np.percentile(image[:,:,1],75)) print("Mavi icin Q1 degeri = ",np.percentile(image[:,:,2],25)) print("Mavi icin Q2 degeri = ",np.percentile(image[:,:,2],50)) print("Mavi icin Q3 degeri = ",np.percentile(image[:,:,2],75)) print("Kirmizi icin iqr degeri = ",iqr(image[:,:,0])) print("Yesil icin iqr degeri = ",iqr(image[:,:,1])) print("Mavi icin iqr degeri = ",iqr(image[:,:,2])) def ss(image): print("Kirmizi icin skewness degeri = ",skew(image[:,:,0])) print("Yesil icin skewness degeri = ",skew(image[:,:,1])) print("Mavi icin skewness degeri = ",skew(image[:,:,2])) def aralik(image): print("Kirmizi icin range araligi = ",image[:,:,0].max()-image[:,:,0].min()) print("Yesil icin range araligi = ",image[:,:,0].max()-image[:,:,1].min()) print("Mavi icin range araligi = ",image[:,:,0].max()-image[:,:,2].min()) inverse(im_1) mean(im_1) median(im_1) mode(im_1) plt.imshow(im_1) plt.show() my_H(im_1) ceyreklik(im_1) ss(im_1) aralik(im_1)
3.Hafta Ders İçeriği
#!/usr/bin/env python # coding: utf-8 import numpy as np import os cwd = os.getcwd() cwd os.chdir("C:\\imageProcessing2018") import matplotlib.pyplot as plt import numpy as np im1=plt.imread("1.jpg") plt.imshow(im1) plt.show() im1.setflags(write=1) # resim sadece read-only özelliğine sahipti üzerinde değişiklik yapabilmek için değiştirdim get_ipython().run_line_magic('matplotlib', 'inline') def get_distance(v,w=[1/3,1/3,1/3]): #w ağırlık değeri a,b,c=v[0],v[1],v[2] w1,w2,w3=w[0],w[1],w[2] #d=((a*w1)**2+(b*w2)**2+(c*w3)**2)**.5 #sqrt işlemi var d=((a**2)*w1+(b**2)*w2+(c**2)*w3)**.5 return d my_RGB=[1,2,3] gray_level=get_distance(my_RGB) print(gray_level) my_RGB=[10,20,3] gray_level=get_distance(my_RGB,[.6,.3,.1]) print(gray_level) def convert_rgb_to_gray_level(im_1): m=im_1.shape[0] n=im_1.shape[1] im_2=np.zeros((m,n)) for i in range(m): for j in range(n): im_2[i,j]=get_distance(im_1[i,j,:]) return im_2 im2=convert_rgb_to_gray_level(im1) type(im2) im1.shape,im2.shape plt.imshow(im2,cmap='gray') plt.show() def convert_gray_to_BW(image_gray_level): m=image_gray_level.shape[0] n=image_gray_level.shape[1] im_bw=np.zeros((m,n)) for i in range(m): for j in range(n): if image_gray_level[i,j]>120: im_bw[i,j]=1 else: im_bw[i,j]=0 return im_bw im3=convert_gray_to_BW(im2) plt.subplot(1,3,1),plt.imshow(im1) plt.subplot(1,3,2),plt.imshow(im2,cmap='gray') plt.subplot(1,3,3),plt.imshow(im3,cmap='gray') plt.imshow(im3,cmap='gray') plt.show()
RGB |
Gray |
BW |
4.Hafta Ders İçeriği
# coding: utf-8 import numpy as np #resim üzerinden bir parça alıp onu mask ile çarpabilir toplayabilir herhangibir işleme tabi tutabiliriz. import matplotlib.pyplot as plt mask_0=np.array([1,1,1,1,1,1,1,1,1]).reshape(3,3) mask_0=(mask_0)/9 mask_1=np.random.randint(5,size=9).reshape(3,3) mask_2=np.random.randint(5,size=9).reshape(3,3) print(mask_1) print("------------") print(mask_2) print("------------") print(mask_1*mask_2) print("------------") print(mask_1*mask_0) def get_distance(v,w=[1/3,1/3,1/3]): #w ağırlık değeri a,b,c=v[0],v[1],v[2] w1,w2,w3=w[0],w[1],w[2] #d=((a*w1)**2+(b*w2)**2+(c*w3)**2)**.5 #sqrt işlemi var d=((a**2)*w1+(b**2)*w2+(c**2)*w3)**.5 return d def convert_rgb_to_gray_level(im_1): m=im_1.shape[0] n=im_1.shape[1] im_2=np.zeros((m,n)) for i in range(m): for j in range(n): im_2[i,j]=get_distance(im_1[i,j,:]) return im_2 sum(sum(mask_1*mask_0)) def get_default_mask_for_mean(): return np.array([1,1,1,1,1,1,1,1,1]).reshape(3,3)/9 def apply_mask(part_of_image): mask=get_default_mask_for_mean() return np.average(part_of_image) im1=plt.imread("1.jpg") #ortalama ile yapılan örnek im1=convert_rgb_to_gray_level(im1) plt.imshow(im1,cmap='gray') plt.show() m=im1.shape[0] n=im1.shape[1] im2=np.zeros((m,n)) for i in range(1,m): for j in range(1,n): poi=im1[i-1:i+2,j-1:j+2] im2[i,j]=apply_mask(poi) #part of image = poi plt.imshow(im2,cmap='gray') plt.show() mask_1=np.random.randint(5,size=9).reshape(3,3) mask_1 mask_1[:,0:1] im2[3:6,10:13] s_1=im2[3:6,10:13].reshape(9) s_1.sort() s_1[5] def get_median(poi): s_1=poi.reshape(1,9) s_1.sort() return s_1[0,4] im1=plt.imread("1.jpg") # ortanca ile yapılan örnek im1=convert_rgb_to_gray_level(im1) plt.imshow(im1,cmap='gray') plt.show() m=im1.shape[0] n=im1.shape[1] im2=np.zeros((m,n)) for i in range(1,m-1): for j in range(1,n-1): poi=im1[i-1:i+2,j-1:j+2] im2[i,j]=get_median(poi) #part of image = poi plt.imshow(im2,cmap='gray') plt.show()
Mask Uygulanmamış Resim |
Mask Uygulanmış Resim |
5.Hafta Ders İçeriği
#!/usr/bin/env python # coding: utf-8 import numpy as np #resmimizi gray level daha sonra bw aktarıcaz t için e - 6 t için i - 2 import matplotlib.pyplot as plt im1=plt.imread("3.png") plt.imshow(im1) plt.show() def get_distance(v,w=[1/3,1/3,1/3]): #w ağırlık değeri a,b,c=v[0],v[1],v[2] w1,w2,w3=w[0],w[1],w[2] #d=((a*w1)**2+(b*w2)**2+(c*w3)**2)**.5 #sqrt işlemi var d=((a**2)*w1+(b**2)*w2+(c**2)*w3)**.5 return d def convert_rgb_to_gray_level(im_1): m=im_1.shape[0] n=im_1.shape[1] im_2=np.zeros((m,n)) for i in range(m): for j in range(n): im_2[i,j]=get_distance(im_1[i,j,:]) return im_2 def convert_rgb_to_BW(image_gray_level): m=image_gray_level.shape[0] n=image_gray_level.shape[1] im_bw=np.zeros((m,n)) for i in range(m): for j in range(n): if image_gray_level[i,j]==0: im_bw[i,j]=0 else: im_bw[i,j]=1 return im_bw im2=convert_rgb_to_gray_level(im1) plt.imshow(im2,cmap='gray') plt.show() im3=convert_rgb_to_BW(im2) plt.imshow(im3,cmap='gray') plt.show() def pixel_compenent(resim): #evde pixel compenenti ayrı fonksiyon yap ayır daha güzel olur m=resim.shape[0] # siyah 0 beyaz 1 n=resim.shape[1] external=0 internal=0 for i in range (1,m-1): for j in range(1,n-1): poi=resim[i:i+2,j:j+2] siyah=0 beyaz=0 for k in range(2): for l in range(2): if poi[k][l]==1: beyaz=beyaz+1 else: siyah=siyah+1 if(siyah>beyaz and beyaz>0): internal=internal+1 elif(beyaz>siyah and siyah>0): external=external+1 print("dış kenar ",external) print("iç kenar ",internal) print("Nesne sayisi",(external-internal)/4) pixel_compenent(im3)
6.Hafta Ders İçeriği
import numpy as np import matplotlib.pyplot as plt import random as random def my_create_m(): return np.random.randint(0,2,(28,28)) def MBR_create_28_by_28_with_0_1(matris_a): m=matris_a.shape[0] n=matris_a.shape[1] x_min=m x_max=0 #başlangıç değerleri olası en olumsuz durum y_min=n y_max=0 for i in range(m): for j in range(n): if (matris_a[i,j]==1 and x_min>i): # resim/matris üzerinden x_min=i # x_min, .... güncelleniyor if (matris_a[i,j]==1 and x_maxj): y_min=j if (matris_a[i,j]==1 and y_maxbenzerlik_max): benzerlik_max=get_similarity(a,b) c=b print("En Yüksek Benzerlik : ",benzerlik_max) plt.imshow(a,interpolation='nearest',cmap='gray') plt.show() plt.imshow(c,interpolation='nearest',cmap='gray') plt.show() get_similarity_for_100_characters()
7.Hafta (Ödev)
import numpy as np import matplotlib.pyplot as plt im1=plt.imread("7.jpg") im1.setflags(write="1") plt.imshow(im1) plt.show() def get_distance(v,w=[1/3,1/3,1/3]): #w ağırlık değeri a,b,c=v[0],v[1],v[2] w1,w2,w3=w[0],w[1],w[2] #d=((a*w1)**2+(b*w2)**2+(c*w3)**2)**.5 #sqrt işlemi var d=((a**2)*w1+(b**2)*w2+(c**2)*w3)**.5 return d def convert_rgb_to_gray_level(im_1): m=im_1.shape[0] n=im_1.shape[1] im_2=np.zeros((m,n)) for i in range(m): for j in range(n): im_2[i,j]=get_distance(im_1[i,j,:]) return im_2 def convert_rgb_to_BW(image_gray_level): m=image_gray_level.shape[0] n=image_gray_level.shape[1] im_bw=np.zeros((m,n)) for i in range(m): for j in range(n): if image_gray_level[i,j]>120: im_bw[i,j]=0 else: im_bw[i,j]=1 return im_bw def flatlist(liste): flat=[] for i in range (len(liste)): for j in range (len(liste)): flat.append(liste[i][j]) return flat def apply_mask(liste,mask=[1,1,1,1,1,1,1,1,1]): liste2=flatlist(liste) result=0 if liste2==mask: result=1 return result def apply_mask2(liste,mask=[0,0,0,0,0,0,0,0,0]): liste2=flatlist(liste) result=1 if liste2==mask: result=0 return result im2=convert_rgb_to_gray_level(im1) im3=convert_rgb_to_BW(im2) def eresion(resim): m=resim.shape[0] # siyah 0 beyaz 1 n=resim.shape[1] im4=np.zeros((m,n)) for i in range (1,m-2): for j in range(1,n-2): b=resim[i-1:i+2,j-1:j+2] im4[i,j]=apply_mask(b) plt.imshow(im4,cmap='gray') plt.show() return im4 def dilation(resim): m=resim.shape[0] # siyah 0 beyaz 1 n=resim.shape[1] im6=np.zeros((m,n)) for i in range (1,m-2): for j in range(1,n-2): b=resim[i-1:i+2,j-1:j+2] im6[i,j]=apply_mask2(b) plt.imshow(im6,cmap='gray') plt.show() return im6 im5=eresion(im3) plt.imshow(im3,cmap='gray') plt.show() print(im5.shape,im3.shape) im7=dilation(im3) plt.imshow(im3,cmap='gray') plt.show() print(im7.shape,im3.shape) im10=plt.imread("1.jpg") im10.setflags(write="1") plt.imshow(im10) plt.show() im11=convert_rgb_to_gray_level(im10) plt.imshow(im11,cmap='gray') plt.show() im12=convert_rgb_to_BW(im11) plt.imshow(im12,cmap='gray') plt.show()
Orjinal Resim |
Eresion Uygulanmış Hali |
Dilation Uygulanmış Hali |
10.Hafta Ders İçeriği
#importing the libraries import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.decomposition import PCA import matplotlib.pyplot as plt from mpl_toolkits import mplot3d iris =datasets.load_iris() setosa=iris.data[0:50] versicolor=iris.data[50:100] virginica=iris.data[100:150] setosa_sepal_length_avg=np.mean(setosa[:,0]) setosa_sepal_width_avg=np.mean(setosa[:,1]) setosa_petal_length_avg=np.mean(setosa[:,2]) setosa_petal_width_avg=np.mean(setosa[:,3]) versicolor_sepal_length_avg=np.mean(versicolor[:,0]) versicolor_sepal_width_avg=np.mean(versicolor[:,1]) versicolor_petal_length_avg=np.mean(versicolor[:,2]) versicolor_petal_width_avg=np.mean(versicolor[:,3]) virginica_sepal_length_avg=np.mean(virginica[:,0]) virginica_sepal_width_avg=np.mean(virginica[:,1]) virginica_petal_length_avg=np.mean(virginica[:,2]) virginica_petal_width_avg=np.mean(virginica[:,3]) #eğer veriler sınıflara ayrılmışsa elimizdeki veri setinde sınıf sayısını bulabiliriz. def sinifsayisibul(): h=[] for i in range(len(iris.target)): if (iris.target[i] in h): continue else: h.append(iris.target[i]) return len(h) sinifsayisibul() fig=plt.figure() ax=plt.axes(projection='3d') xdata=iris.data[:,0] ydata=iris.data[:,2] zdata=iris.data[:,3] label=iris.target ax.scatter3D(xdata[100:149], ydata[100:149], zdata[100:149],marker='o') ax.scatter3D(xdata[50:99], ydata[50:99], zdata[50:99],marker='x') ax.scatter3D(xdata[0:49], ydata[0:49], zdata[0:49],marker='x') def get_mu_s(): mu_0=[5,2,0] mu_1=[4,4,0] mu_2=[3,2,5] return mu_0,mu_1,mu_2 def get_distance(mu,point): x=mu[0]-point[0] y=mu[1]-point[1] z=mu[2]-point[2] return (x**2+y**2+z**2)**0.5 def get_class_for_one_instance(flower): mu_s = get_mu_s() d_0 = get_distance(mu_s[0], flower) d_1 = get_distance(mu_s[1], flower) d_2 = get_distance(mu_s[2], flower) if((d_0 < d_1) and (d_0 < d_2)): return "0" if((d_1 < d_0) and (d_1 < d_2)): return "1" if((d_2 < d_1) and (d_2 < d_0)): return "2" def my_f_1(s_1=125): x=iris.data[s_1][0] y=iris.data[s_1][2] z=iris.data[s_1][3] my_f_1=[x,y,z] r=get_class_for_one_instance(my_f_1) print(r) for i in range(150): my_f_1(i) def get_flower(i): #iris data üzerinde bana i değerinde gelen datanın 0 2 3 sütunun donduruyorum x=iris.data[i][0] y=iris.data[i][2] z=iris.data[i][3] return [x,y,z] def update_mu(): #150 ya kadar her bir çicek için bana class bilgisi ver güncelle ve geriye mu'leri ver hata="yok" mu_0_counter=0.0001 mu_0_sum=0 mu_1_counter=0.0001 mu_1_sum=0 mu_2_counter=0.0001 mu_2_sum=0 c_1=[] c_2=[] c_3=[] for i in range(150): my_flower_data=get_flower(i) f_class = get_class_for_one_instance(my_flower_data) hata="var" if(f_class=="0"): c_1.append(my_flower_data) if(f_class=="1"): c_2.append(my_flower_data) if(f_class=="2"): c_3.append(my_flower_data) return c_1,c_2,c_3 c_1,c_2,c_3 = update_mu() print(len(c_1)) print(len(c_2)) print(len(c_3))
K-Means Sınıflandırma |
Hiç yorum yok:
Yorum Gönder