一、Halcon編程之圖像處理
1、讀取圖片
1、讀取單個圖片:
1.1 直接用算子read_image
read_image (Image, 'D:/3.tiff')
2、讀取多個圖片
2.1 定義一個存放圖片路徑的數組,通過循環讀取
imagepath:=[]
imagepath[0]:=['D:/1.tiff']
imagepath[1]:=['D:/2.tiff']
imagepath[2]:=['D:/3.tiff']
for Index := 1 to 5 by 1
read_image (Image,imagepath[Index])
endfor
2.2 循環遍歷讀取
for i := 1 to 3 by 1
read_image (Image,'D:/'+i+'.tiff')
endfor
2.3 循環遍歷讀取
list_files ('D:/Images', ['files','directories','recursive','max_depth 2','max_files 8','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
* Image Acquisition 02: Do something
endfor
3、工具欄—>讀取圖像
4、通過工具欄的【助手】
2、圖像預處理
一般來說,我們采集到的圖像會有一些小黑點,小斑點,不平滑等因素會會影響我們后期的算法,此時就需要我們對其圖片進行預處理。
下面是一些預處理基本算子:
1、消除噪聲:mean_image/binomial_filter
mean_image(Image,ImageMean,MaskWidth,MaskHeight)
參數:
Image:輸入圖像
ImageMean:輸出圖像
MaskWidth:遮掩的寬度【要過濾的寬度】
默認值: 9
建議值: 3, 5, 7, 9, 11, 15, 23, 31, 43, 61, 101
典型區域值: 1 ≤ MaskWidth ≤ 501
MaskHeight:遮掩的高度【要過濾的高度】
默認值: 9
建議值: 3, 5, 7, 9, 11, 15, 23, 31, 43, 61, 101
典型區域值: 1 ≤ MaskHeight ≤ 501
binomial_filter(Image,ImageBinomial,MaskWidth,MaskHeight)
參數:
Image :輸入圖像
ImageBinomial:輸出圖像
MaskWidth :遮掩的寬度【要過濾的寬度】
默認值: 5
可選值: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37
MaskHeight :遮掩高度【要過濾的高度】
默認值: 5
可選值: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37
2、抑制小斑點或者細線:median_image
median_image(Image,ImageMedian,MaskType,Radius,Margin)
參數:
Image:輸入圖像
ImageMedian:輸出圖像
MaskType:掩飾類型
Filter mask type.
默認值: 'circle'
可選值: 'circle', 'square'
可選值 (為計算機設備): 'square'
Radius :半徑
默認值: 1
可選值 (為計算機設備): 1, 2
建議值: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 15, 19, 25, 31, 39, 47, 59
典型區域類型值: 1 ≤ Radius ≤ 4095
Margin :
邊界處理.
默認值: 'mirrored'
可選值 (為計算機設備): 'mirrored'
建議值: 'mirrored', 'cyclic', 'continued', 0, 30, 60, 90, 120, 150, 180, 210, 240, 255
3、平滑:smooth_image
smooth_image(Image,ImageSmooth,Filter,Alpha)
參數:
Image :輸入圖像
ImageSmooth:輸出圖像
Filter :過濾
默認值: 'deriche2'
可選值: 'deriche1', 'deriche2', 'gauss', 'shen'
Alpha :
過濾參數: 小值導致強烈的平滑(亦然用 bei 'gauss').
默認值: 0.5
建議值: 0.1, 0.2, 0.3, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0, 7.0, 10.0
典型的范圍值: 0.01 ≤ Alpha ≤ 50.0
最小增量: 0.01
建議增加: 0.1
限制: Alpha > 0
4、保存邊緣的平滑:anisotropic_diffusion
anisotropic_diffusion(Image,ImageAniso,Mode,Contrast,Theta,Iterations)
參數:
Image:輸入圖像
ImageAniso:輸出圖像
Mode:
擴散系數函數的振幅
默認值: 'weickert'
可選值: 'parabolic', 'perona-malik', 'weickert'
Contrast:
對比,差別
默認值: 5.0
建議值: 2.0, 5.0, 10.0, 20.0, 50.0, 100.0
限制: Contrast > 0
Theta:
時間步
默認值: 1.0
建議值: 0.5, 1.0, 3.0
限制: Theta > 0
Iterations:
迭代次數
默認值: 10
建議值: 1, 3, 10, 100, 500
限制: Iterations >= 1
5、scale_image
scale_image(Image : ImageScaled : Mult, Add : )縮放圖像的灰度值。最佳Mult和Add值的選取由下:
其中:GMin為圖像的最大灰度值,GMax為圖像的最小灰度值。
可配合算子min_max_gray(Regions, Image : : Percent : Min, Max, Range)得到。
min_max_gray(Regions, Image : : Percent : Min, Max, Range)確定區域內的最小和最大灰度值
min_max_gray創建輸入圖像圖像區域內灰度值絕對值的直方圖(參見gray_histo),並計算與輸入圖像區域相對應的像素百分比。然后它會在直方圖的兩邊以這個像素的數量向內移動,並確定最小和最大的灰度值。
6、Sigma_image