Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
画像処理ライブラリOpenCVの使い方
OHNO
June 20, 2020
Research
0
540
画像処理ライブラリOpenCVの使い方
OHNO
June 20, 2020
Tweet
Share
More Decks by OHNO
See All by OHNO
外観検査の難しさ
planeta
0
210
儲かるPython
planeta
0
170
画像処理ライブラリOpenCVの使い方0910
planeta
0
870
Pythonによる工業用カメラ画像取得事例
planeta
0
730
SONYのNNC
planeta
0
310
機械学習による動作認識
planeta
0
510
画像類似度計算
planeta
0
1.1k
Tensorflow/Keras(Python)で作ったモデルをC++で使う
planeta
0
1.3k
Other Decks in Research
See All in Research
【IR Reading2022秋】 CPFair: Personalized Consumer and Producer Fairness Re-ranking for Recommender Systems
yamato0811
1
110
【論文紹介】Evaluating the Evaluation of Diversity in Natural Language Generation
ichiroex
2
190
ログ収集入門Elastic_Searchの機能と活用事例
o_hasegawa
0
150
ランダム行列から深層学習へ
clustervr
PRO
0
480
FADEC: FPGA-based Acceleration of Video Depth Estimation by HW/SW Co-design (FPT 2022)
hashi0203
0
170
データ分析の進め方とニュースメディアでのデータ活用事例 / data-analysis-in-kaggle-and-news-media
upura
0
480
2022年度伊藤ゼミ紹介
imash
0
120
Compositional Evaluation on Japanese Textual Entailment and Similarity (JSICK:構成的推論・類似度データセットSICK日本語版の紹介)
verypluming
2
420
最先端NLP論文紹介:Revisiting the Uniform Information Density Hypothesis (EMNLP2021). Linguistic Dependencies and Statistical Dependence (EMNLP2021).
kuribayashi4
3
330
note #買ってよかったものレポート2022 / Best to Buy 2022
noteinc
1
770
世界観を考察するのが好き?Sound Horizonはいいぞ / Introduction to Sound Horizon
hyuyukun
0
790
投資戦略202301
pw
0
420
Featured
See All Featured
In The Pink: A Labor of Love
frogandcode
132
21k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
2
400
Imperfection Machines: The Place of Print at Facebook
scottboms
254
12k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
31
20k
The Brand Is Dead. Long Live the Brand.
mthomps
48
2.9k
Become a Pro
speakerdeck
PRO
6
3.2k
Designing the Hi-DPI Web
ddemaree
273
32k
Streamline your AJAX requests with AmplifyJS and jQuery
dougneiner
128
8.8k
The Mythical Team-Month
searls
210
40k
Design by the Numbers
sachag
271
18k
Large-scale JavaScript Application Architecture
addyosmani
499
110k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
351
21k
Transcript
画像処理ライブラリ OpenCVの使い方 大野 宏 Python機械学習勉強会in新潟 Restart#11
Pythonでよく使われる画像ライブラリ ・matplotlibグラフ作成ライブラリ 学習回数―認識率の グラフや画像の表示 ・Pillow 画像の回転や拡大縮小など基本的な処理
OpenCV ・無料の画像処理用ライブラリ 最新版は4.3.0 ・マルチプラットフォーム Windows、Linux、Mac、iOS、Android C、C++、Python、Java ・Intel社が開発して公開→Willow Garage社が開発・管理 ・matplotlib や
Pillow より高機能 ・ディープラーニングの前処理や後処理に有効 ・OpenCVだけで傷の検査が出来る場合も
OpenCVの基本的な機能 ・画像の読み込みと表示 ・画像の拡大と縮小 ・画像の回転 ・図形の描画 ・フィルタ処理 ・ラベリング ・2値化
OpenCVのインストール方法 ・ターミナルを開いて pip install opencv-python ・確認 ターミナルでPythonを起動し $python [Enter] 下記の通りに入力して何も表示されなければOK
>>import cv2 [Enter] >> ・Numpyもインストールしておくとよい
画像の読み込みと表示 #カラー img1=cv2.imread("lena.jpg",1) #白黒 img1=cv2.imread("lena.jpg",0) #表示 cv2.imshow ( "img1", img1
) #表示時間 1秒 cv2.waitKey(1000) #保存 cv2.imwrite("img.png" , img1) USBカメラの画像も 簡単に表示できる
画像の拡大と縮小 img1=cv2.imread("building.jpg",1) height = img1.shape[0] width = img1.shape[1] #縮小 img2=cv2.resize(img1,(int(width*0.5),
int(height*(0.5))) cv2.imshow( "img2", img2 ) cv2.waitKey(1000)
画像の反転、回転 img1=cv2.imread("building.jpg",1) img2=cv2.flip(img1,0) #上下反転 0 cv2.imshow ( "img2", img2 )
cv2.waitKey(1000) img2=cv2.flip(img1,1) #左右反転 >0 img2=cv2.flip(img1,-1) #左右反転 <0 img2=cv2.rotate(img1, cv2.ROTATE_90_CLOCKWISE) img2=cv2.rotate(img1, cv2.ROTATE_180) img2=cv2.rotate(img1,cv2.ROTATE_90_COUNTERCLOCKWISE)
画像の回転 img1=cv2.imread("building.jpg",1) height = img1.shape[0] width = img1.shape[1] center =
(int(width/2), int(height/2)) angle = 45.0 scale = 1.0 trans = cv2.getRotationMatrix2D(center, angle , scale) img2 = cv2.warpAffine(img1, trans, (width,height)) cv2.imshow ( "img2", img2 ) cv2.waitKey(1000)
四角形、線、円の描画 img1=cv2.imread("building.jpg",1) cv2.rectangle(img1, (200, 200), (500, 500), (255, 0, 0),
thickness=2, lineType=cv2.LINE_4) cv2.line(img1, (100, 100), (600, 100), (0, 255, 0), thickness=2, lineType=cv2.LINE_4) cv2.circle(img1, (440,400), 100, (0,0,255), thickness=2, lineType=cv2.LINE_4, shift=0) cv2.imshow ( "img", img1 ) cv2.waitKey(1000)
フィルタ処理(平均値) img1=cv2.imread("lena.jpg") #平均値フィルタ img2=cv2.blur(img1,(3,3)) cv2.imshow ( "img2", img2 ) cv2.waitKey(1000)
1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9
フィルタ処理(Sobel) img1=cv2.imread("building.jpg",0) img3=cv2.Sobel(img1,cv2.CV_32F,1,0) img3=cv2.convertScaleAbs(img3) cv2.imshow ( "img1", img1 ) cv2.imshow
( "img3", img3 ) cv2.waitKey(1000) 横方向に強調→縦線検出 -1 0 1 -2 0 2 -1 0 1
フィルタ処理(Sobel) img1=cv2.imread("building.jpg",0) img2=cv2.Sobel(img1,cv2.CV_32F,0,1) img2=cv2.convertScaleAbs(img2) cv2.imshow ( "img1", img1 ) cv2.imshow
( "img2", img2 ) cv2.waitKey(1000) img3=img2|img3 cv2.imshow ( "img3", img3 ) cv2.waitKey(1000) 縦方向に強調→横線検出 -1 -2 -1 0 0 0 1 2 1
フィルタ処理(Sobel)の結果
フィルタ処理(Canny) 輪郭線を綺麗に抽出するフィルター img1=cv2.imread("building.jpg",0) img2=cv2.Canny(img1,100,200) cv2.imshow ( "img1", img1 ) cv2.imshow
( "img2", img2 ) cv2.waitKey(1000)
2値化とラベリング1 2値化:グレイスケールの画像を適当な値で0と1に分ける ラベリング:1の塊ごとに番号をつける 元画像 ラベルごとに色付け
2値化とラベリング2 img1=cv2.imread("pic1.png”,1) img2=cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) #2値化 ret,img3=cv2.threshold(img2,100,255,cv2.THRESH_BINARY) #ラベリング n,label=cv2.connectedComponents(img3) height = img3.shape[0]
width = img3.shape[1] str1='height:'+str(height)+' width:'+str(width) print(str1) cv2.imshow ( "img3", img3 )
ラベルの色付け colors=[] colors.append([0,0,0]) colors.append([255,0,0]) colors.append([0,255,0]) colors.append([0,0,255]) colors.append([0,255,255]) for y in
range(0,height): for x in range(0,width): if 0<label[y,x]: img1[y,x]=colors[label[y,x]] else: img1[y,x]=[0,0,0] cv2.imshow ( "img1", img1 ) cv2.waitKey(0)
前処理での利用例 煎餅の良・不良の判別をYOLOで行う場合、学習データとし てアノテーションが必要 → OpenCVを使うと作業が楽 良品 不良品(欠けや割れ)
2値化とラベリングで領域検出 ・2値化とラベリングして煎餅の領域を検出する
良・不良のラベル付け ・面積で良・不良を判別してラベルを付ける 良品 不良品 良品
目視で修正 ・誤った半別を目視で修正し、データ作成完了 良品 不良品
画像処理による傷の検査 ・2値化、ラベリング、大きい塊を傷と認識 → 引っ掻き傷は2値化すると線が途切れる ・傷の画像を集め、画像処理(大きさ、濃淡、傾き)で数を 増し、ディープラーニングで判別 現画像 2値画像 直線なら傷 途切れるとノイズ?
参考図書 ・Python、C++ ・各アルゴリズムがどのよう な計算をしているかも記述 ・ネットで検索すると参考にな るもの多数あり