* 功能是:通过木板纹理识别树种
* 过程是:特征提取:纹理区+图片(灰度共生矩阵的标量4、边缘灰度直方图1)组成的特征向量;用于训练多层感知机;最后用于识别木板
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('E:/03 CV(古)/Halcon/代码/木板纹理识别', ['files','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)
FeaturesExtended:=[]
FeaturesExtended1:=[]
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
dev_clear_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/5, Height/5, 'black', WindowHandle)
dev_display (Image)
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Regions, 31, 254)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 40825.7, 500000)
Classes:=['apple','beech','cherry','maple','oak']
*整个图片的特征向量
cooc_feature_image (Image, Image, 6, 90, Energy, Correlation, Homogeneity, Contrast)#计算共生矩阵并导出其灰度值特征
sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)#轮廓特征:使用Sobel算子检测边缘(幅度)特征
gray_histo_abs (EdgeAmplitude, EdgeAmplitude, 8, AbsoluteHistoEdgeAmplitude) #计算灰度值分布。Quantization=8表示输入灰度值的量子化。
FeaturesExtended:=[Energy, Correlation, Homogeneity, Contrast]
FeaturesExtended:=[FeaturesExtended,AbsoluteHistoEdgeAmplitude]
*纹理区域的特征向量
cooc_feature_image (SelectedRegions, Image, 6, 90, Energy, Correlation, Homogeneity, Contrast)
sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)
gray_histo_abs (EdgeAmplitude, EdgeAmplitude, 8, AbsoluteHistoEdgeAmplitude)
FeaturesExtended1:=[FeaturesExtended,Energy, Correlation, Homogeneity, Contrast]
FeaturesExtended1:=[FeaturesExtended1,AbsoluteHistoEdgeAmplitude]
NumFeatures:=|FeaturesExtended1|
NumClasses:=|Classes|
NumHidden:=15
*2创建分类器
create_class_mlp (NumFeatures, 15, 5, 'softmax', 'normalization', 10, 42, MLPHandle)
for i := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[i])
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Regions, 31, 254)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 40825.7, 500000)
cooc_feature_image (SelectedRegions, Image, 6, 90, Energy, Correlation, Homogeneity, Contrast)
sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)
gray_histo_abs (EdgeAmplitude, EdgeAmplitude, 8, AbsoluteHistoEdgeAmplitude)
FeaturesExtended:=[Energy, Correlation, Homogeneity, Contrast]
FeaturesExtended:=[FeaturesExtended,AbsoluteHistoEdgeAmplitude]
cooc_feature_image (Image, Image, 6, 90, Energy, Correlation, Homogeneity, Contrast)
sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)
gray_histo_abs (EdgeAmplitude, EdgeAmplitude, 8, AbsoluteHistoEdgeAmplitude)
FeaturesExtended1:=[FeaturesExtended,Energy, Correlation, Homogeneity, Contrast]
FeaturesExtended1:=[FeaturesExtended1,AbsoluteHistoEdgeAmplitude]
FeatureVector:=real(FeaturesExtended1)#实数化
*添加样本
add_sample_class_mlp (MLPHandle, FeatureVector, i)
endfor
*训练分类器
train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
stop ()
write_class_mlp (MLPHandle, 'D://1.gmc')
*测试
for i := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[i])
rgb1_to_gray(Image, GrayImage)
threshold (GrayImage, Regions, 31, 254)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 40825.7, 500000)
cooc_feature_image (SelectedRegions, Image, 6, 90, Energy, Correlation, Homogeneity, Contrast)
sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)
gray_histo_abs (EdgeAmplitude, EdgeAmplitude, 8, AbsoluteHistoEdgeAmplitude)
FeaturesExtended := [Energy,Correlation,Homogeneity,Contrast]
FeaturesExtended := [FeaturesExtended,AbsoluteHistoEdgeAmplitude]
cooc_feature_image (Image, Image, 6, 90, Energy, Correlation, Homogeneity, Contrast)
sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)
gray_histo_abs (EdgeAmplitude, EdgeAmplitude, 8, AbsoluteHistoEdgeAmplitude)
FeaturesExtended1 := [FeaturesExtended,Energy,Correlation,Homogeneity,Contrast]
FeaturesExtended1 := [FeaturesExtended1,AbsoluteHistoEdgeAmplitude]
FeatureVector := real(FeaturesExtended1)
******3 识别
classify_class_mlp (MLPHandle, FeatureVector, 1, FoundClassIDs, k)
dev_display(Image)
ImageFiles1:= 'found class: ' + Classes[FoundClassIDs[0]]
disp_message(WindowHandle, ImageFiles1, 'image', 12, 12, 'black', 'true')
stop()
endfor
endfor
总结:
1.使用多层感知机的步骤和颜色识别是一模一样的,都是分三步,如代码里注释所写。 2.不过它们还是有些差别的。颜色识别输入是图片或者图片区域,而纹理识别输入的是72维特征向量,所以在添加样本时有细微差别,前者是add_samples_image_class_mlp,后者是add_sample_class_mlp。
|