
その他
ブログ
【記録】OpenCVの画像処理にトライ
1. はじめに
皆さん、おはこんばんにちは!
エンジニアのみやもとです。
現在、クレジットカードの番号認識を実現しようと色々試しています。
画像はRGBという「赤・緑・青」の3光源で構成されており、そのままでは認識が難しいため、画像を認識しやすいようにOpenCVを使用して画像処理を行っていきたいと思います。
今回は、画像処理の方法について記録していきます。
2. OpenCVについて

3. 画像処理
グレースケール
<span class="token keyword">import</span> cv2
image <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'\\test_cre.png'</span><span class="token punctuation">)</span>
gray <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>image<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">'card'</span><span class="token punctuation">,</span> gray<span class="token punctuation">)</span>

グレースケール(低輝度化)
import sys
import cv2
import numpy
gamma22LUT = numpy.array([pow(x/255.0, 2.2) for x in range(256)], dtype='float32')
img_bgr= cv2.imread('\\test_cre.png')
img_bgrL = cv2.LUT(img_bgr, gamma22LUT)
img_grayL = cv2.cvtColor(img_bgrL, cv2.COLOR_BGR2GRAY)
img_gray = pow(img_grayL, 1.0/2.2) * 255
cv2.imshow('card', img_gray)
cv2.waitKey()

2値化
<span class="token keyword">import</span> cv2
image <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'\\test_cre.png'</span><span class="token punctuation">)</span>
gray <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>image<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
thresh <span class="token operator">=</span> cv2<span class="token punctuation">.</span>threshold<span class="token punctuation">(</span>gray<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>THRESH_OTSU <span class="token operator">+</span> cv2<span class="token punctuation">.</span>THRESH_BINARY<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">'card'</span><span class="token punctuation">,</span> thresh<span class="token punctuation">)</span>

白黒反転
<span class="token keyword">import</span> cv2
image <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'\\test_cre.png'</span><span class="token punctuation">)</span>
gray <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>image<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
thresh <span class="token operator">=</span> cv2<span class="token punctuation">.</span>threshold<span class="token punctuation">(</span>gray<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>THRESH_OTSU <span class="token operator">+</span> cv2<span class="token punctuation">.</span>THRESH_BINARY<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
image2 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>bitwise_not<span class="token punctuation">(</span>thresh<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">'card'</span><span class="token punctuation">,</span> image2<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token punctuation">)</span>

4. まとめ
Kaggleでもスコアを上げるためにデータ補正を行いました。
画像処理もこれと同じで認識しやすい形があるようで、処理によっては認識される文字や範囲が異なることもあります。今後もクレジットカードの番号認識を実装するため、画像処理について学んでいこうと思います。