Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ใ€็”ปๅƒๅ‡ฆ็†ใ€‘Numpyใงใƒขใƒซใƒ•ใ‚ฉใƒญใ‚ธใƒผๆผ”็ฎ—

Posted at

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')

GrayImage.png

ไบŒๅ€ค็”ปๅƒใ‚‚ไฝœๆˆใ—ใพใ™ใ€‚ไบŒๅ€ค็”ปๅƒไฝœๆˆใฎ่ฉณ็ดฐใซใคใ„ใฆใฏไปฅไธ‹ใฎ่จ˜ไบ‹ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚
ใ€็”ปๅƒๅ‡ฆ็†ใ€‘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')

BinaryImage.png

ๅŽ็ธฎ (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')

ErodeBinaryImage.png

ๅŽ็ธฎใ‚’ใ‚ฐใƒฌใƒผใ‚นใ‚ฑใƒผใƒซ็”ปๅƒใซ้ฉ็”จใ—ใพใ™ใ€‚

erode_gray_image = erode(gray_image)
plt.imshow(erode_gray_image, cmap='gray')

ErodeGrayImage.png

่†จๅผต (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')

DilateBinaryImage.png

่†จๅผตใ‚’ใ‚ฐใƒฌใƒผใ‚นใ‚ฑใƒผใƒซ็”ปๅƒใซ้ฉ็”จใ—ใพใ™ใ€‚

dilate_gray_image = dilate(gray_image)
plt.imshow(dilate_gray_image, cmap='gray')

DilateGrayImage.png

ใ‚ชใƒผใƒ—ใƒ‹ใƒณใ‚ฐ (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')

OpeningBinaryImage.png

ใ‚ชใƒผใƒ—ใƒ‹ใƒณใ‚ฐใ‚’ใ‚ฐใƒฌใƒผใ‚นใ‚ฑใƒผใƒซ็”ปๅƒใซ้ฉ็”จใ—ใพใ™ใ€‚

opening_gray_image = opening(gray_image)
plt.imshow(opening_gray_image, cmap='gray')

OpeningGrayImage.png

ใ‚ฏใƒญใƒผใ‚ธใƒณใ‚ฐ (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')

ClosingBinaryImage.png

ใ‚ฏใƒญใƒผใ‚ธใƒณใ‚ฐใ‚’ใ‚ฐใƒฌใƒผใ‚นใ‚ฑใƒผใƒซ็”ปๅƒใซ้ฉ็”จใ—ใพใ™ใ€‚

closing_gray_image = closing(gray_image)
plt.imshow(closing_gray_image, cmap='gray')

ClosingGrayImage.png

ใƒˆใƒƒใƒ—ใƒใƒƒใƒˆ (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')

TophatBinaryImage.png

ใƒˆใƒƒใƒ—ใƒใƒƒใƒˆใ‚’ใ‚ฐใƒฌใƒผใ‚นใ‚ฑใƒผใƒซ็”ปๅƒใซ้ฉ็”จใ—ใพใ™ใ€‚

tophat_gray_image = tophat(gray_image)
plt.imshow(tophat_gray_image, cmap='gray')

TophatGrayImage.png

ใƒ–ใƒฉใƒƒใ‚ฏใƒใƒƒใƒˆ (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')

BlackhatBinaryImage.png

ใƒ–ใƒฉใƒƒใ‚ฏใƒใƒƒใƒˆใ‚’ใ‚ฐใƒฌใƒผใ‚นใ‚ฑใƒผใƒซ็”ปๅƒใซ้ฉ็”จใ—ใพใ™ใ€‚

blackhat_gray_image = blackhat(gray_image)
plt.imshow(blackhat_gray_image, cmap='gray')

BlackhatGrayImage.png


ๅฎŸ่ฃ…ใ—ใŸใ‚ณใƒผใƒ‰ใฏGoogle Colabratoryใซ็ฝฎใ„ใฆใ‚ใ‚Šใพใ™ใ€‚

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Comments

No comments

Let's comment your feelings that are more than good

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar ๐ŸŽ…

Some calendars come with gifts and some gifts are drawn from all calendars ๐Ÿ‘€

Please tie the article to your calendar and let's enjoy Christmas together!

5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address