Bu kod maskeleme işlemi yapmaktadır. Yüklenen bir resimde odak noktasından belli bir uzaklıktan sonra resmi siyah yapar.
import numpy as np
import matplotlib.pyplot as plt
def createCircularMask(h, w, center=None, radius=None):
if center is None:
center = [int(w/2), int(h/2)]
if radius is None:
radius = min(center[0], center[1], w-center[0], h-center[1]) y, x = np.ogrid[:h, :w]
dist_from_center = np.sqrt((x - center[0])**2 + (y-center[1])**2)
mask = dist_from_center <= radius
return mask
img = plt.imread("img.jpg")
h, w = img.shape[:2]
radius = h/4
mask = createCircularMask(h, w, radius=radius)
img[~mask] = 0
plt.imsave("img2.jpg", img, cmap="gray")

img.jpg
img2.jpg
Kod 2:
Bu kod yüklenen bir fotoğrafı saat yönünde 90 derece döndürür.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img=mpimg.imread('img.jpeg')
img.ndim
img.shape
plt.imshow(img)
plt.show()
img.ndim,img.shape
img_20=np.zeros((500,375,3))
img_20.shape
for i in range(375):
for j in range(500):
img_20[j,i,:]=img[i,j,:]
plt.subplot(1,2,1),plt.imshow(img)
plt.subplot(1,2,2),plt.imshow(img_20)
plt.show()
img[:,100,:].max()
img_1=img[1:375:2,:,:]
img_1=img[:,1:500:2,:]
plt.subplot(1,2,1),plt.imshow(img)
plt.subplot(1,2,2),plt.imshow(img_1)
plt.show()
img_30=np.zeros((500,375,3))
img_30.shape
for i in range(375):
for j in range(500):
img_30[j,i,:]=1-img[i,j,:]
#img[i,j,:]
img_40=np.zeros((375,500,3))
img_40.shape
for i in range(375):
for j in range(500):
img_40[375-i-1,500-j-1,:]=1-img[i,j,:]
img_50=np.zeros((375,500,3))
img_50.shape
for i in range(375):
for j in range(500):
img_50[375-i-1,j,:]=1-img[i,j,:]
plt.subplot(2,3,1),plt.imshow(img)
plt.subplot(2,3,2),plt.imshow(img_20)
plt.subplot(2,3,3),plt.imshow(img_30)
plt.subplot(2,3,4),plt.imshow(img_30)
plt.subplot(2,3,5),plt.imshow(img_40)
plt.subplot(2,3,5),plt.imshow(img_50)
plt.show()
Kod 3:
import matplotlib.pyplot as plt
import numpy as np
img_1=plt.imread("img.jpg")
img_1.ndim,img_1.shape
plt.imshow(img_1)
pixel_1=[0,0,0]
pixel_1_gray_level=0
pixel_1=[10,0,0]
pixel_1_gray_level=10
pixel_1_rgb=[10,10,10]
pixel_1_gray_level=12
def convertRGBPixelToGrayLevel(RGB_Pixel):
return RGB_Pixel[0]/3+RGB_Pixel[1]/3+RGB_Pixel[2]/3
def convertRGB_to_GrayLevel(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_1.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
convertRGBPixelToGrayLevel([2,5,7])
img_2=np.zeros((img_1.shape[0],img_1.shape[1]))
img_2.shape
for i in range(img_1.shape[0]):
for j in range(img_2.shape[1]):
img_2[i,j]=convertRGBPixelToGrayLevel(img_1[i,j,:])
plt.subplot(1,2,1)
plt.imshow(img_1)
plt.subplot(1,2,2)
plt.imshow(img_2,cmap='gray')
plt.show()
Kod 4:
Bu kod yüklediğim bir resmin belli bir kısmını ayırarak o ayrılmış olan kısmı graylevel'den siyah beyaza çevirir.
import matplotlib.pyplot as plt
import numpy as np
img1 = plt.imread("img.jpg")
img1.ndim
img1.shape
img2 = img1[1:575:2, 1:1024:2]
img2.ndim
img2.shape
plt.imshow(img2)
plt.show()
img3 = np.zeros(img2.shape[0:2])
img3.shape
img4 = np.zeros(img2.shape[0:2])
img4.shape
threshold = 120
for i in range(img2.shape[0]):
for j in range(img2.shape[1]):
n = img2[i,j,0]/3 + img2[i,j,1]/3 + img2[i,j,2]/3
img3[i,j] = n
if n > threshold:
img4[i,j] = 255
else:
img4[i,j] = 0
plt.subplot(1,4,1), plt.imshow(img2)
plt.subplot(1,4,2), plt.imshow(img3, plt.cm.gray)
plt.subplot(1,4,3), plt.imshow(img4, plt.cm.gray)
plt.subplot(1,4,4), plt.imshow(img4, plt.cm.binary)
Kod 5:
Bu kod maskeleme işlemini farklı bir şekilde yaparak sayısal değer döndürür.
import numpy as np
import matplotlib.pyplot as plt
im1=[[1,0],[0,0]]
im2=[[0,1],[0,0]]
im3=[[0,0],[1,0]]
im4=[[0,0],[0,1]]
em1=[[0,1],[1,1]]
em2=[[1,0],[1,1]]
em3=[[1,1],[0,1]]
em4=[[1,1],[1,0]]
im1 = [im1, im2, im3, im4]
em1 = [em1, em2, em3, em4]
img1 = plt.imread("img.jpg")
img1
plt.imshow(img1, plt.cm.binary)
plt.show()
img1.ndim
img1.shape
def count_internal_mask(image):
counter_internal = 0
for mask in im1:
counter_internal = counter_internal + count_mask(image, mask)
return counter_internal
def count_external_mask(image):
counter_external = 0
for mask in em1:
counter_external = counter_external + count_mask(image, mask)
return counter_external
def count_mask(image, mask):
counter = 0
m = img1.shape[0]
n = img1.shape[1]
for i in range(m-1):
for j in range(n-1):
a = b = c = d = False
if(img1[i,j][0] == mask[0][0]):
a = True
if (img1[i,j+1][0] == mask[0][1]):
b = True
if (img1[i+1,j][0] == mask[1][0]):
c = True
if (img1[i+1,j+1][0] == mask[1][1]):
d = True
if (a and b and c and d):
counter = counter + 1
return counter
c1 = count_internal_mask(img1)
c2 = count_external_mask(img1)
c1, c2
Kod 6:
import numpy as np
import matplotlib.pyplot as plt
def defineMask():
mask = [[1,1,1],[1,1,1],[1,1,1]]
return mask
m = img1.shape[0]
n = img1.shape[1]
img2 = np.random.randint(0,1,(img1.shape[0],img1.shape[1]))
for i in range(1,m-1):
for j in range(1,n-1):
x1 = img1[i,j] and mask[1][1] #center
x2 = img1[i-1,j-1] and mask[0][0] #scan
x3 = img1[i-1,j] and mask[0][1]
x4 = img1[i-1,j+1] and mask[0][2]
x5 = img1[i,j-1] and mask[1][0]
x6 = img1[i,j+1] and mask[1][2]
x7 = img1[i+1,j-1] and mask[2][0]
x8 = img1[i+1,j] and mask[2][1]
x9 = img1[i+1,j+1] and mask[2][2]
result1 = x1 or x2 or x3 or x4 or x5
result2 = x6 or x7 or x8 or x9
result = result1 or result2
img2[i,j] = result
return img2
test = plt.imread("test.jpg")
bw = np.zeros(test.shape[0:2])
threshold = 120
for i in range(test.shape[0]):
for j in range(test.shape[1]):
n = test[i,j,0]/3 + test[i,j,1]/3 + test[i,j,2]/3
if n > threshold:
bw[i,j] = 0
else:
bw[i,j] = 255
dilated = myDilation(bw,defineMask()) # print dilated image
plt.imshow(test) #default image
plt.show()
plt.imshow(bw, plt.cm.binary) #bw image
plt.show()
plt.imshow(dilated, plt.cm.binary) #dilated image
plt.show()



Hiç yorum yok:
Yorum Gönder