Numpyใงใขใซใใฉใญใธใผๆผ็ฎใๅฎ่ฃ ใใฆใฟใพใใ
ใพใใไฝฟ็จใใ็ปๅใ่ชญใฟ่พผใใงใฐใฌใผในใฑใผใซ็ปๅใซๅคๆใใพใใ
import numpy as np
import matplotlib.pyplot as plt
original_image = plt.imread(image_name)
if np.issubdtype(original_image.dtype, np.floating):
original_image = (original_image * 255).astype(np.uint8)
gray_image = (0.2116 * original_image[:,:,0] + 0.7152 * original_image[:,:,1] + 0.0722 * original_image[:,:,2]).astype(np.uint8)
plt.imshow(gray_image, cmap='gray')
ไบๅค็ปๅใไฝๆใใพใใไบๅค็ปๅไฝๆใฎ่ฉณ็ดฐใซใคใใฆใฏไปฅไธใฎ่จไบใๅ็
งใใฆใใ ใใใ
ใ็ปๅๅฆ็ใNumpyใงๅคงๆดฅใฎไบๅคๅ(ๅคๅฅๅๆๆณ) - Qiita
def thresholding(image, threshold):
return np.where(image <= threshold, 0, 255)
binary_image = thresholding(gray_image, 128)
plt.imshow(binary_image, cmap='gray')
ๅ็ธฎ (Erosion)
ๅๆฏ(็ฝ)ใไธๅใๅฐใใใใใขใซใใฉใญใธใผๆผ็ฎใๅ็ธฎ(Erosion)ใจๅผใณใพใใๆณจ็ฎ็ป็ด ใจใใฎ่ฟๅ็ป็ด ใฎๆๅฐๅคใๆณจ็ฎ็ป็ด ใฎๅคใซใใพใใ
def erode(image, boundary='edge'):
pad_image = np.pad(image, 1, boundary)
areas = np.lib.stride_tricks.as_strided(pad_image, image.shape + (3, 3), pad_image.strides * 2)
return np.min(areas, axis=(2, 3))
ๅ็ธฎใไบๅค็ปๅใซ้ฉ็จใใพใใ
erode_binary_image = erode(binary_image)
plt.imshow(erode_binary_image, cmap='gray')
ๅ็ธฎใใฐใฌใผในใฑใผใซ็ปๅใซ้ฉ็จใใพใใ
erode_gray_image = erode(gray_image)
plt.imshow(erode_gray_image, cmap='gray')
่จๅผต (Dilation)
ๅๆฏ(็ฝ)ใไธๅใๅคงใใใใๅฆ็ใ่จๅผต(Dilation)ใจๅผใณใพใใๆณจ็ฎ็ป็ด ใจใใฎ่ฟๅ็ป็ด ใฎๆๅคงๅคใๆณจ็ฎ็ป็ด ใฎๅคใซใใพใใ
def dilate(image, boundary='edge'):
pad_image = np.pad(image, 1, boundary)
areas = np.lib.stride_tricks.as_strided(pad_image, image.shape + (3, 3), pad_image.strides * 2)
return np.max(areas, axis=(2, 3))
่จๅผตใไบๅค็ปๅใซ้ฉ็จใใพใใ
dilate_binary_image = dilate(binary_image)
plt.imshow(dilate_binary_image, cmap='gray')
่จๅผตใใฐใฌใผในใฑใผใซ็ปๅใซ้ฉ็จใใพใใ
dilate_gray_image = dilate(gray_image)
plt.imshow(dilate_gray_image, cmap='gray')
ใชใผใใใณใฐ (Opening)
่คๆฐๅใฎๅ็ธฎใฎใใจใซใๅใๅๆฐ่จๅผตใใๅฆ็ใใชใผใใใณใฐ(Opening)ใจๅผใณใพใใ
def opening(image, n=2, boundary='edge'):
for i in range(n):
image = erode(image, boundary)
for i in range(n):
image = dilate(image, boundary)
return image
ใชใผใใใณใฐใไบๅค็ปๅใซ้ฉ็จใใพใใ
opening_binary_image = opening(binary_image)
plt.imshow(opening_binary_image, cmap='gray')
ใชใผใใใณใฐใใฐใฌใผในใฑใผใซ็ปๅใซ้ฉ็จใใพใใ
opening_gray_image = opening(gray_image)
plt.imshow(opening_gray_image, cmap='gray')
ใฏใญใผใธใณใฐ (Closing)
่คๆฐๅใฎ่จๅผตใฎใใจใซใๅใๅๆฐๅ็ธฎใใๅฆ็ใใฏใญใผใธใณใฐ(Closing)ใจๅผใณใพใใ
def closing(image, n=2, boundary='edge'):
for i in range(n):
image = dilate(image, boundary)
for i in range(n):
image = erode(image, boundary)
return image
ใฏใญใผใธใณใฐใไบๅค็ปๅใซ้ฉ็จใใพใใ
closing_binary_image = closing(binary_image)
plt.imshow(closing_binary_image, cmap='gray')
ใฏใญใผใธใณใฐใใฐใฌใผในใฑใผใซ็ปๅใซ้ฉ็จใใพใใ
closing_gray_image = closing(gray_image)
plt.imshow(closing_gray_image, cmap='gray')
ใใใใใใ (Top-Hat)
ๅ
ใฎ็ปๅใใใชใผใใใณใฐใๆฝใใ็ปๅใๅผใๅฆ็ใใใใใใใ(Top-Hat)ใจๅผใณใพใใ
ใใใใใใ็ปๅใซใฏใชใผใใใณใฐใซใใๅ้คใใใๅคใๆฎใใพใใ
def tophat(image, n=2, boundary='edge'):
opening_image = opening(image, n, boundary)
return image - opening_image
ใใใใใใใไบๅค็ปๅใซ้ฉ็จใใพใใ
tophat_binary_image = tophat(binary_image)
plt.imshow(tophat_binary_image, cmap='gray')
ใใใใใใใใฐใฌใผในใฑใผใซ็ปๅใซ้ฉ็จใใพใใ
tophat_gray_image = tophat(gray_image)
plt.imshow(tophat_gray_image, cmap='gray')
ใใฉใใฏใใใ (Black-Hat)
ใฏใญใผใธใณใฐใๆฝใใ็ปๅใใๅ
็ปๅใๅผใๅฆ็ใใใฉใใฏใใใ(Black-Hat)ใจๅผใณใพใใ
ใใฉใใฏใใใ็ปๅใซใฏใฏใญใผใธใณใฐใใ่ฟฝๅ ใใใๅคใๆฎใใพใใ
def blackhat(image, n=2, boundary='edge'):
closing_image = opening(image, n, boundary)
return closing_image - image
ใใฉใใฏใใใใไบๅค็ปๅใซ้ฉ็จใใพใใ
blackhat_binary_image = blackhat(binary_image)
plt.imshow(blackhat_binary_image, cmap='gray')
ใใฉใใฏใใใใใฐใฌใผในใฑใผใซ็ปๅใซ้ฉ็จใใพใใ
blackhat_gray_image = blackhat(gray_image)
plt.imshow(blackhat_gray_image, cmap='gray')
ๅฎ่ฃ ใใใณใผใใฏGoogle Colabratoryใซ็ฝฎใใฆใใใพใใ














Comments
Let's comment your feelings that are more than good