halcon SVM 缺陷检测分类

一、概述

训练数据

二、算子解释

compactness

Halcon 算子 compactness_halcon compactness-****博客

*计算输入区域的紧凑度
compactness (Region, Compactness)
 原理解释

convexity  每个输入区域的凸度

Halcon 算子 convexity_halcon convexity-****博客

*计算每个输入区域的凸度
convexity (Region, Convexity)   

 原理解释
moments_region_central_invar 计算区域的四个矩

 

三、halcon 代码


get_system ('image_dir', HalconImages)
get_system ('operating_system', OS)
if (OS{0:2} == 'Win')
    tuple_split (HalconImages, ';', HalconImages)
else
    tuple_split (HalconImages, ':', HalconImages)
endif
ReadOK := false
for k := 0 to |HalconImages| - 1 by 1
    try
        read_image (Image, HalconImages[k] + '/halogen_bulb/halogen_bulb_01.png')
        ReadPath := HalconImages[k] + '/halogen_bulb/'
        ReadOK := true
        break
    catch (Exception)
    endtry
endfor
if (not ReadOK)
    disp_message (WindowHandle, 'Could not find the images in $HALCONIMAGES', 'window', 12, 12, 'black', 'true')
    stop ()
endif
read_image (Image, 'halogen_bulb/halogen_bulb_01.png')
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
* 
ClassNames := ['good','bad','none']
Colors := ['forest green','red','red']
Nu := 0.05
KernelParam := 0.02
* 
* Create an SVM classifier
*NumFeatures 输入变量数
*KernelType  :内核类型  线性”,“多项式均匀”,“多项式不均匀”,“ rbf”
*KernelParam :内核参数  内核函数的附加参数。如果是RBF内核,则的值 。对于多项式核,度
*Nu      SVM的正则化常数。
*NumClasses   类数。
*Mode  SVM的模式    “新颖性检测”,“一个对所有”,“一个对一个”
*Preprocessing   用于转换特征向量的预处理类型。
*NumComponents  预处理参数:变换后的特征数量
*SVMHandle  SVM句柄。
create_class_svm (7, 'rbf', KernelParam, Nu, |ClassNames|, 'one-versus-one', 'principal_components', 7, SVMHandle)
* 
* Add samples
* 这里是只训练好的 还有 坏的的以及那个none 的
add_samples_to_svm (ClassNames, SVMHandle, WindowHandle, ReadPath)
*add_samples_to_svm 函数实现
* for ClassNumber := 0 to |ClassNames| - 1 by 1
*     list_files (ReadPath + ClassNames[ClassNumber], 'files', Files)
*     Selection := regexp_select(Files,'.*[.]png')
*     for Index := 0 to |Selection| - 1 by 1
*         read_image (Image, Selection[Index])
*         dev_display (Image)
        * 'Add Samples...', -1
*         threshold (Image, Region, 0, 40)
*         calculate_features (Region, Features)
*         add_sample_class_svm (SVMHandle, Features, ClassNumber)
*     endfor
* endfor
 *==========================================
 *calculate_features 的实现
*  area_center (Region, Area, Row, Column)
* 计算输入区域的紧凑度
* compactness (Region, Compactness)
* 四个不变的矩
* moments_region_central_invar (Region, PSI1, PSI2, PSI3, PSI4)
*计算每个输入区域的凸度
* convexity (Region, Convexity)   
* Features := real([Area,Compactness,PSI1,PSI2,PSI3,PSI4,Convexity])
* return ()
        
return ()
dev_clear_window ()
* 
* Train the classifier
disp_message (WindowHandle, 'Training...', 'window', 12, 12, 'black', 'true')
*训练
*Epsilon (input_control) 精度
*TrainMode    'add_sv_to_train_set', 'default'
train_class_svm (SVMHandle, 0.001, 'default')
disp_message (WindowHandle, 'Training completed', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Classify halogen bulbs

classify_regions_with_svm (SVMHandle, Colors, ClassNames, ReadPath)
* 
    *list_files (ReadPath, ['files','recursive'], Files)
    *Selection := regexp_select(Files,'.*[.]png')
    *read_image (Image, Selection[0])
    *dev_close_window ()
    *get_image_size (Image, Width, Height)
    *dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)
    *set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
    *for Index := 0 to |Selection| - 1 by 1
    *    read_image (Image, Selection[Index])
    *    threshold (Image, Region, 0, 40)
    *    calculate_features (Region, Features)
    *    *特征
    *    classify_class_svm (SVMHandle, Features, 1, Class)
    *    dev_display (Image)
    *    dev_set_color (Colors[Class])
    *    dev_display (Region)
    *    disp_message (WindowHandle, 'Classified as:' + ClassNames[Class], 'window', 12, 12, 'black', 'true')
    *    disp_continue_message (WindowHandle, 'black', 'true')
    *    stop ()
    *endfor
    *dev_display (Image)
    *return ()
* Clear the classifier from memory
clear_class_svm (SVMHandle)

上一篇:物联网应用系统与网关


下一篇:threejs 场景构建技巧与优化策略探讨