色んなサイトからコピペもとい参考にしつつインスパイアされつつで。
#! /usr/bin/env python # -*- coding: utf-8 -*- import sys, os, re import numpy as np import cv2 import matplotlib.pyplot as plt im = cv2.imread("out.png") im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) th1 = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) image, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(im, contours, -1, (255, 0, 0), 2) for con in contours: x, y, w, h = cv2.boundingRect(con) cv2.rectangle(im, (x, y), (x+w, y+h), (0, 0, 255), 2) plt.subplot(1,2,1),plt.imshow(im_gray, 'gray') plt.title('input image') plt.subplot(1,2,2),plt.imshow(im, 'gray') plt.title('output image') plt.show()
で、
とかとか。
あまりないかもしれないけど、OCRでテキスト認識させたい時にうまく認識しなかったら1文字ずつ別の画像に落として個別で認識させたらいけるんじゃないだろうかとか。
Pythonで画像からテキストを抽出する - Qiitaとかでぐにょーんとね。