Halcon 學習筆記--仿射變換與車牌定位(6)


        

 

 

           

 

 Halcon仿射方式:

 

 vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)其中Row, Column, Phi是所選擇區域中心坐標以及相對於水平方向夾角,rad(180)為要旋轉的角度這個角度是任意值

hom_mat2d_identity( : : : HomMat2DIdentity)形成單位矩陣:

 

hom_mat2d_rotate( : : HomMat2D, Phi, Px, Py : HomMat2DRotate

 

 hom_mat2d_scale( : : HomMat2D, Sx, Sy, Px, Py : HomMat2DScale)

 

 hom_mat2d_translate( : : HomMat2D, Tx, Ty : HomMat2DTranslate)

 

 

 

 

二、車牌識別

dev_close_window()

read_image (Image, 'E:/欣奕華/項目/Halcon/STUDY/網絡課程筆記/3.仿射變換/車牌1.jpg')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
area_center (Rectangle, Area, Row1, Column1)
orientation_region (Rectangle, Phi1)
vector_angle_to_rigid (Row1, Column1, Phi1, Row1, Column1, 3.14, HomMat2D)
affine_trans_region (Rectangle, RegionAffineTrans1, HomMat2D, 'nearest_neighbor')
affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')
reduce_domain (ImageAffinTrans, RegionAffineTrans1, ImageReduced)
rgb1_to_gray (ImageReduced, GrayImage)
dev_display (GrayImage)
threshold (GrayImage, Regions, 0, 66)

connection (Regions, ConnectedRegions)

select_shape (ConnectedRegions, SelectedRegions, ['area','width','height'], 'and', [1701.88,0,0], [5000,53.05,114.71])

sort_region (SelectedRegions, SortedRegions, 'character', 'true', 'column')
area_center (SortedRegions, Area1, Row2, Column2)
read_ocr_class_mlp ( 'Industrial_0-9A-Z_NoRej.omc', OCRHandle)
do_ocr_multi_class_mlp (SortedRegions, GrayImage, OCRHandle, Class, Confidence)
for I := 0  to 5 by 1
    disp_message (WindowHandle, Class[I], 'image', Row2[I], Column2[I], 'black', 'true')
endfor

上述代碼是自己寫的,下面是網絡課程,主要看他的注釋:

*1采集圖像
read_image (Image, 'E:/欣奕華/項目/Halcon/STUDY/Lesson ten_OCR/1.jpg')
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)
*2預處理之車牌定位,一般定位有兩種,一個是blob像素團塊定位,一個是模板匹配定位,然后幾何變換轉正
decompose3 (Image, Red, Green, Blue)
trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv')
*注意這里的顏色通道轉換是為了方便圖像分割,也就是車牌定位,這里用的比較通用簡單的blob,在實際項目中需要考慮光照等的影響進行微調優化
*這里的二值化是進行一個blob車牌定位

threshold (Saturation, Regions, 182, 255)
opening_rectangle1 (Regions, RegionOpening, 6, 6)
shape_trans (RegionOpening, RegionTrans, 'rectangle2')

*接下來求這個區域的角度和中心點,便於仿射變換轉正
orientation_region (RegionTrans, Phi)
area_center (RegionTrans, Area, Row, Column)

*開始求解仿射變換之旋轉矩陣,這里要注意是轉到180度還是0度,需要注意你求解角度時的極軸方向,具體可以看鏈接視頻
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)
*將圖像和區域都做這個旋轉變換,然后摳圖,再進行圖像分割
affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
reduce_domain (ImageAffinTrans, RegionAffineTrans, ImageReduced)

rgb1_to_gray (ImageReduced, GrayImage)
invert_image (GrayImage, ImageInvert)
threshold (GrayImage, Regions2, 92,135)
opening_rectangle1 (Regions2, RegionOpening1, 3, 3)
connection (RegionOpening1, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 568.08, 1372.46)
*字符就被提取了,注意這里我暫時不是識別漢字,要識別漢字也是可以的,可以看視頻鏈接
*進行字符排序方便識別后觀察,因為人都是習慣從左到右
sort_region (SelectedRegions, SortedRegions, 'first_point', 'true', 'column')
*4識別顯示,注意這里識別用的halcon自帶字庫,同時帶NoRej的表示非拒絕,識別要求不嚴格
read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
*注意ocr套路,一般可以多個區域一起識別,也可以單個區域識別,注意上面的工作就是為了得到這個區域
do_ocr_multi_class_mlp (SortedRegions, ImageInvert, OCRHandle, Class, Confidence)

area_center (SortedRegions, Area1, Row1, Column1)

for index := 0 to 5 by 1
    disp_message (WindowHandle, Class[index], 'window', Row1[0], Column1[index], 'black', 'true')
endfor

 


免責聲明!

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



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