OpenCV ハンズオン 2

Page content

下の四文字を分割するタスク

import cv2 # OpenCVライブラリ
import numpy as np
from matplotlib import pyplot as plt # 画像を描画するライブラリ matplotlib

image_bgr = cv2.imread("../images/2A2X.png")
image_gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)

plt.imshow(image_gray, cmap='gray')
plt.show()

png

print(image_bgr.shape)
結果:
(24, 72, 3)
# https://algorithm.joho.info/image-processing/otsu-thresholding/
ret, threshd_img = cv2.threshold(image_gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)

plt.imshow(threshd_img, cmap='gray')

plt.show()

png

image_contour, contours, hierarchy = cv2.findContours(threshd_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

print(type(contours))
print(len(contours)) # 四つ輪郭
結果:
<class 'list'>
4
letter_image_regions = []

for contour in contours:

    (x, y, w, h) = cv2.boundingRect(contour)

    letter_image_regions.append((x, y, w, h))
    
letter_image_regions = sorted(letter_image_regions, key=lambda x: x[0])
letter_image_regions = []
len(letter_image_regions) # x軸でソートした四つの輪郭
結果:
4
print(letter_image_regions[0])
print(letter_image_regions[1])
print(letter_image_regions[2])
print(letter_image_regions[3])
結果:
(8, 9, 9, 12)
(23, 8, 11, 13)
(39, 6, 9, 12)
(53, 8, 12, 12)
letter_images = []

for letter_bounding_box in letter_image_regions:

    x, y, w, h = letter_bounding_box
    
    # 個々の文字を切り出す
    letter_image = image_gray[y - 2:y + h + 2, x - 2:x + w + 2]

    letter_images.append(letter_image)
print(type(letter_images))
print(type(letter_images[0]))
結果:
<class 'list'>
<class 'numpy.ndarray'>
plt.imshow(letter_images[0], cmap='gray')
plt.show()

png

plt.imshow(letter_images[1], cmap='gray')
plt.show()

png

plt.imshow(letter_images[2], cmap='gray')
plt.show()

png

plt.imshow(letter_images[3], cmap='gray')
plt.show()

png

About Wang Zhijun
機械学習好きなプログラマー