實用圖像處理入門 - 3 - 訓練自己的識別器


1 環境准備

1.1 基本工具:

  • Visual Studio 2012 必裝
  • opencv 2.4+ 必裝 ( 目前是2.4.6, 下載鏈接 ). ( 假設已安裝完畢, opencv安裝目錄以下用 {opencv} 表示 )
  • cygwin 必裝 ( 安裝教程, 網上一抓一大把, 就不貼鏈接了. 記得安裝 perl )
  • CMake 必裝 ( 下載鏈接, 用來編譯 opencv )

 

1.2 輔助工具:

  • imageclipper 工具. 此工具能快速的從原始圖片中截取待識別區域, 生成只包含待識別區域的圖片. ( 下載地址 )
  • opencv_createsamples 工具. 在文件夾 {cvbuild}/bin/Release/opencv_createsamples.exe 中.
  • mergevec 工具. 用來把多個 *.vec 文件生成一個 *.vec 文件. ( 下載地址 )
  • createtrainsamples 工具, 用來快速生成大量測試樣本. ( 下載地址 )
  • 如果以上地址無法打開, 這里有以上工具的合集. ( 百度網盤下載 )
  • 此文中用到的所有命令, 均以sh腳本文件保存在這里( 百度網盤下載, 下文用 “腳本附件”表示此附件 ). 您可以省去很多編寫腳本的麻煩

 

1.3 項目文件結構如下:

image

2 詳細步驟

2.1 編譯 opencv

因為這一步驟比較長, 被我單獨開貼到這里了.

此步驟務必完成, 否則后續步驟無法執行

編譯 opencv 的目標位置, 在下文中用 {cvbuild} 表示

2.2 制作樣本

2.2.1 負樣本

目標: 生成 背景描述文件 bg.txt

步驟:

  1. 下載負樣本文件. ( 忘記從哪里下載的了, 事先申明這圖片庫不是我制作的, 請大家不要用於商業用途. 百度盤下載 )
  2. 解壓文件到某處. 進入文件夾, 執行以下命令:
    ?
    1
    2
    cd  . /samples/negtive/
    find  ./ -name "*.png"  > . /bg .txt

得到 bg.txt. 即為負樣本文件. ( 腳本附件/S2-fetchNegtiveImage.sh )

 

2.2.2 正樣本

目標: 生成正樣本的 *.vec 樣本描述文件

2.2.2.1 用 imageclipper 截取待識別區域

運行命令:(  假設源圖片放在 ./samples/positive/src/ 文件夾中 )

如果沒有特殊說明, 文中所有的命令均在 cygwin 終端中執行.

?
1
./imageclipper/imageclipper.exe ./samples/positive/src/ -o  ./samples/positive/%i__ % x__ % y__ % w__ % h__.png

( 腳本附件/S1-clipPositiveImage.sh )

在命令行中輸入

?
1
./imageclipper/imageclipper.exe

可以看到 imageclipper 的使用說明. 咱們主要關心一下 –o 參數:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  -o  <output_format = imgout_format or vidout_format>
        Determine the output file path format.
        This is a syntax sugar for -i  and -v .
        Format Expression)
            %d - dirname of the original
            %i - filename of the original without extension
            %e - filename extension of the original
            %x - upper -left  x coord
            %y - upper -left  y coord
            %w - width
            % h  - height
            % r  - rotation degree
            %. - shear deformation in x coord
            %, - shear deformation in y coord
            %f - frame number (for video)
        Example) ./$i_ % 04x_ % 04y_ % 04w_ % 04h.%e
            Store into software directory and use image type  of the original.

-o 參數主要來控制生成圖片的文件名.

比如咱們這里的 –o 參數為 “%i__%x__%y__%w__%h__.png”.

就表示生成文件的文件名為 “原文件名__x坐標__y坐標__寬度__高度__.png”.

生成文件在文件夾:./samples/positive/ 中:

image

 

2.2.2.2 用 opencv_createsamples 對截取出的區域生成樣本描述文件 *.vec

opencv 官方文檔中對 opencv_createsamples 的使用描述見這里

在終端中輸入 opencv_createsamples 不加任何參數, 查看使用幫助:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ ./opencvTools/opencv_createsamples.exe
Usage: D:\vsWorkspace\trainClassifier\opencvTools\opencv_createsamples.exe
  [ -info  <collection_file_name>]
  [ -img  <image_file_name>]
  [ -vec  <vec_file_name>]
  [ -bg  <background_file_name>]
  [ -num  <number_of_samples = 1000>]
  [ -bgcolor  <background_color = 0>]
  [ -inv ] [ -randinv ] [ -bgthresh  <background_color_threshold = 80>]
  [ -maxidev  <max_intensity_deviation = 40>]
  [ -maxxangle  <max_x_rotation_angle = 1.100000>]
  [ -maxyangle  <max_y_rotation_angle = 1.100000>]
  [ -maxzangle  <max_z_rotation_angle = 0.500000>]
  [ -show  [<scale = 4.000000>]]
  [ -w  <sample_width = 24>]
  [ -h  <sample_height = 24>]

我們需要用到的參數有:

?
1
2
3
4
5
6
7
8
9
  [ -img  <image_file_name>]                                        //樣本圖片url
  [ -vec  <vec_file_name>]                                          //生成 vec 文件名
  [ -bg  <background_file_name>]                                    // 背景圖片描述文件.( 即一個文本文件, 里面每一行是一個背景文件的url )
  [ -num  <number_of_samples = 1000>]                               // 對每一個圖片生成多少個變形后的圖片.          
  [ -maxxangle  <max_x_rotation_angle = 1.100000>]                  // 最大x方向翻轉
  [ -maxyangle  <max_y_rotation_angle = 1.100000>]                  // 最大y方向翻轉
  [ -maxzangle  <max_z_rotation_angle = 0.500000>]                  // 最大z方向翻轉
  [ -w  <sample_width = 24>]                                                                              
  [ -h  <sample_height = 24>]                                       //這里的w和h在以后會多次用到, 一定要保證和這里的相等. 否則會失敗        

下面使用 openc_createasamples 工具來對每一個目標文件生成對應的 *.vec 樣本描述文件

新建 ./S3-createsamples.sh 文件. 內容如下: ( 腳本附件/S3-createsamples.sh )

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/sh
positive_dir= "./samples/positive/"
list=($(find ./samples/positive/ -name  "*.png"  ))
length of filelist: ${ #list[@]}
 
content= ""
for file in ${list[@]}
do
        echo  ==========================
    name=${file ##*/}
    echo  filename: $name
 
    arr=${name//__/ }
    arr=( $arr )
    echo  ${arr[@]}
    echo  width: ${arr[3]}, height: ${arr[4]}
 
    line= "$name"
    content= "$content$name.vec \n"
     
    vecname=${name//./_}
 
    ./opencvTools/opencv_createsamples.exe -vec  ./samples/positive/ $vecname .vec –bg ./samples/negtive/bg.txt -img  $file  -w  50 -h  25 -num  5
        echo  ==========================
done
 
listfile=$positive_dir "vec_list.txt"
targetfile=$positive_dir "target.vec"
echo  $listfile
echo  $targetfile
 
find $positive_dir -name  "*png.vec"  > $listfile
./imageclipper/mergevec.exe $listfile  $targetfile  -w  50 -h  25

如果需要, 根據 1.3 中本項目的文件結構圖, 將上面的文件路徑改成你需要的路徑.

執行命令:

?
1
$ . /S3-createSamples .sh

打開文件夾, 看到一列 *.vec 文件就對啦~~

image

2.2.2.3 用 mergevec 工具合並 *.vec 文件

細心的您執行完 步驟2.2.2.2 之后, 就會發現上圖已經有合並后的 target.vec 了.

為了方便, 我已經把合並 vec 的步驟寫到 S3-createsamples.sh 文件里去了. 看看這個文件的最后兩行就知道啦.

輸入以下命令可以看到 mergevec 的使用幫助:

?
1
2
3
4
5
$ . /imageclipper/mergevec .exe
Usage: D:\vsWorkspace\trainClassifier\imageclipper\mergevec.exe
  <collection_file_of_vecs>
  <output_vec_filename>
  [-show] [-w <sample_width = 24>] [-h <sample_height = 24>]

 

 

2.2.3 測試樣本

目標: 生成大量帶有待識別區域的測試圖片, 在這些圖片中, 待識別區域的位置都已知.

方法: 把證樣本圖片中的待識別區域, 經過縮放拉伸翻轉后, 按隨機位置貼在背景圖片上, 並記錄下待識別區域在背景圖片上的位置.

假設我們想訓練一個識別嘴巴的識別器. 生成的測試樣本如下:

image

利用我們訓練的識別器, 對這些圖片進行識別. 得到的結果, 跟實際情況相比較, 即可知道識別器的識別率和性能.

創建 S5-createTestSample.sh 內容如下: ( 腳本附件/S5-createTestSample.sh )

?
1
2
3
4
5
6
cd . /samples/test/
find .. /negtive/ -name "*.png" > negtive.dat #正樣本
find .. /positive/ -maxdepth 1 -name "*.png" > positive.dat # 負樣本
perl ../.. /imageclipper/createsamples .pl positive.dat negtive.dat tests 1000 \
"../../opencvTools/opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 -maxzangle 0.5 maxidev 40"
find tests/ -name "info.dat" - exec cat \{\} \; > tests.dat # merge info files

運行腳本文件 S5-createTestSample.sh . 查看 ./samples/test/tests 文件夾, 就能看到各種樣本圖片啦~~

樣本圖片列表存放在:

./samples/test/tests.dat

里. 如果內容如下:

?
1
2
3
4
5
tests /IMG_20130718_150032__228__548__107__58__ .png /0001_0044_0065_0071_0071 .jpg 1 44 65 71 71
tests /IMG_20130718_150032__228__548__107__58__ .png /0002_0068_0232_0125_0125 .jpg 1 68 232 125 125
tests /IMG_20130718_150032__228__548__107__58__ .png /0003_0126_0071_0112_0112 .jpg 1 126 71 112 112
tests /IMG_20130718_150032__228__548__107__58__ .png /0004_0043_0136_0057_0057 .jpg 1 43 136 57 57
...

證明你成功的創建了測試樣本!

2.3 訓練過程

目標: 利用 opencv_haartraining 工具訓練識別器

執行以下命令來訓練識別器:

?
1
2
3
4
5
6
  . /opencvTools/opencv_haartraining .exe  \
                -data . /teeath_haartraining  \
                -vec . /samples/positive/target .vec   \
                - bg  . /samples/negtive/bg .txt   \
                -w 50 -h 25 -nsplits 2 -npos 30 -nneg 200  \
                -nonsym -mem 512 -mode ALL -minhitrate 0.95 -maxfalsealarm 0.5

( 腳本附件/S4-training.sh )

注意: 此步驟可能因為正樣本過少或者負樣本過少而導致錯誤. 如果出現錯誤, 請使用更多樣本進行嘗試.

訓練過程可能要持續好幾天, 請耐心等待.

opencv_haartraining 的官方使用說明見這里

輸入以下命令可查看簡要參數說明:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ . /opencvTools/opencv_haartraining .exe
Usage: D:\vsWorkspace\trainClassifier\opencvTools\opencv_haartraining.exe
  -data <dir_name>
  -vec <vec_file_name>
  - bg  <background_file_name>
  [- bg -vecfile]
  [-npos <number_of_positive_samples = 2000>]
  [-nneg <number_of_negative_samples = 2000>]
  [-nstages <number_of_stages = 14>]
  [-nsplits <number_of_splits = 1>]
  [-mem <memory_in_MB = 200>]
  [-sym (default)] [-nonsym]
  [-minhitrate <min_hit_rate = 0.995000>]
  [-maxfalsealarm <max_false_alarm_rate = 0.500000>]
  [-weighttrimming <weight_trimming = 0.950000>]
  [-eqw]
  [-mode <BASIC (default) | CORE | ALL>]
  [-w <sample_width = 24>]
  [-h <sample_height = 24>]
  [-bt <DAB | RAB | LB | GAB (default)>]
  [-err <misclass (default) | gini | entropy>]
  [-maxtreesplits <max_number_of_splits_in_tree_cascade = 0>]
  [-minpos <min_number_of_positive_samples_per_cluster = 500>]

 

2.4 測試性能

目標: 通過用大量測試數據進行實際測試, 得到識別器的識別率和識別性能

原理: 已在 2.3.3 講過. 可回看參考.

執行命令:

?
1
. /opencvTools/opencv_performance .exe -data teeath_haartraining/ -w 50 -h 25 -info samples /test/tests .dat -ni

( 腳本附件/S6-test.sh )

3 參考

3.1 Tutorial: OpenCV haartraining

3.2 Rapid Object Detection With A Cascade of Boosted Classifiers Based on Haar-like Features Introduction

3.3 Merge vec files created by createsamples

3.4 Cascade Classifier Training ( opencv offical document )

3.5 imageclipper ( A tool to crop images manually fast )


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM