柚子快报官方邀请码778899分享:自动化药片的识别和分类 - 好文阅读
柚子快报官方邀请码778899分享:自动化药片的识别和分类
以下代码是使用HALCON软件编写的,用于图像处理和机器学习中的分类任务。代码的主要功能是训练一个支持向量机(SVM)分类器,用于识别和分类不同种类的药片
dev_close_window () dev_update_off () read_image (Image, ‘color/pills_class_01’) dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_set_draw (‘margin’) dev_set_line_width (2) set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’) dev_set_colored (12) * PillNames := [‘big_round_red’,‘round_green’,‘small_round_red’,‘yellow_trans’,‘brown’,‘brown_green’] PillNames := [PillNames,‘brown_grain’,‘purple’,‘turquese’,‘pink’] PillColors := [‘#D08080’,‘#ADC691’,‘#FFB0A1’,‘#D5C398’,‘#B59C87’,‘#BCB3B8’,‘#B7ACA1’,‘#908E99’,‘#97B9BC’,‘#C0ABA9’] *
Check, which features and feature groups are available query_feature_group_names (AvailableGroupNames) query_feature_names_by_group (AvailableGroupNames, AvailableFeatureNames, AvailableCorrespondingGroups)Generate list of color and region features using thecalculate_feature_set library procedures. FeatureGroups := [‘region’,‘color’] get_feature_names (FeatureGroups, FeatureNames) get_feature_lengths (FeatureNames, FeatureLengths)Create and prepare classifier training data structure create_class_train_data (sum(FeatureLengths), ClassTrainDataHandle) set_feature_lengths_class_train_data (ClassTrainDataHandle, FeatureLengths, FeatureNames)Training loop
for I := 1 to 10 by 1 * Segment pills in training image read_image (Image, ‘color/pills_class_’ + I$‘.2d’) segment_pills (Image, Pills) * Display segmentation result dev_display (Image) dev_set_color (‘white’) dev_display (Pills) disp_message (WindowHandle, ‘Collecting ’ + PillNames[I - 1] + ’ samples’, ‘window’, 12, 12, ‘black’, ‘true’) * * Calculate features for all segmented pills and store * them in the training data structure count_obj (Pills, Number) calculate_features (Pills, Image, FeatureNames, Features) add_sample_class_train_data (ClassTrainDataHandle, ‘feature_column’, Features, I - 1) * * Visualize processed pills dev_set_color (PillColors[I - 1]) dev_display (Pills) GroupList := sum(‘’’ + FeatureGroups + ‘’, ') tuple_str_first_n (GroupList, strlen(GroupList) - 3, GroupList) Message := ‘Calculate ’ + |FeatureNames| + ’ features from following feature groups:’ disp_message (WindowHandle, [Message,GroupList], ‘window’, 40, 12, ‘black’, ‘true’) disp_continue_message (WindowHandle, ‘black’, ‘true’) stop () endfor *
Automatically select suitable features from the training data disp_message (WindowHandle, ‘Selecting optimal features…’, ‘window’, 90, 12, ‘black’, ‘true’) select_feature_set_svm (ClassTrainDataHandle, ‘greedy’, [], [], SVMHandle, SelectedFeatures, Score) disp_message (WindowHandle, [‘Selected:’,SelectedFeatures], ‘window’, 120, 12, ‘black’, ‘true’) disp_continue_message (WindowHandle, ‘black’, ‘true’) stop ()Free memory, because the training data is no longer used clear_class_train_data (ClassTrainDataHandle)Classify pills in test imagesusing the automatically trained classifier with theautomatically selected features dev_set_line_width (4) dev_set_colored (12) for I := 1 to 3 by 1
Segment pills in test image read_image (Image, ‘color/pills_test_’ + I$‘.2d’) dev_display (Image) segment_pills (Image, Pills)For all pills, calculate selected featuresusing the calculate_features procedure from thecalculate_feature_set library and classify them. PillsIDs := [] count_obj (Pills, NPills) for P := 1 to NPills by 1 select_obj (Pills, PillSelected, P) calculate_features (PillSelected, Image, SelectedFeatures, Features) classify_class_svm (SVMHandle, real(Features), 1, Class)
Display results PillsIDs := [PillsIDs,Class] dev_set_color (PillColors[Class]) dev_display (PillSelected) area_center (PillSelected, Area, Row, Column) disp_message (WindowHandle, Class + 1, ‘image’, Row, Column - 10, ‘black’, ‘true’) endfor disp_message (WindowHandle, ‘Classify image ’ + I + ’ of 3 using following features:’, ‘window’, 12, 12, ‘black’, ‘true’) disp_message (WindowHandle, SelectedFeatures, ‘window’, 40, 12, ‘black’, ‘true’) if (I < 3) disp_continue_message (WindowHandle, ‘black’, ‘true’) stop () endif endfor Clean up memory clear_class_svm (SVMHandle) 拼多多帮助力入口
以下是代码的主要步骤和功能:
关闭窗口和关闭更新:使用dev_close_window和dev_update_off函数关闭所有打开的窗口并关闭图像更新。
读取和显示图像:读取名为color/pills_class_1的图像,并使用dev_open_window_fit_image函数打开一个窗口以适应图像大小。
设置绘制属性:设置绘制边缘、线宽和字体等属性。
定义药片名称和颜色:定义了药片的名称列表PillNames和对应的颜色列表PillColors。
查询特征和特征组:使用query_feature_group_names和query_feature_names_by_group函数查询可用的特征和特征组。
获取特征名称和长度:使用get_feature_names和get_feature_lengths函数获取特征名称和长度。
创建训练数据结构:使用create_class_train_data函数创建分类器的训练数据结构,并设置特征长度。
训练循环:
读取训练图像并分割药片。 显示分割结果。 计算分割药片的特征并添加到训练数据结构中。 可视化处理后的药片并显示相关信息。 自动选择特征:使用select_feature_set_svm函数从训练数据中自动选择最优特征。
清理训练数据:使用clear_class_train_data函数清理不再使用的训练数据。
分类测试图像中的药片:
读取测试图像并分割药片。 计算所选特征并使用SVM分类器对药片进行分类。 显示分类结果。 清理分类器内存:使用clear_class_svm函数清理分类器使用的内存。
整体来看,这段代码的主要功能是:
使用HALCON软件对药片图像进行预处理和特征提取。 训练一个SVM分类器,用于自动识别和分类不同类型的药片。 自动选择最优特征以提高分类器的性能。 在测试图像上应用训练好的分类器,并显示分类结果。 这种图像处理和机器学习技术在医药行业中有广泛的应用,可以帮助自动化药片的识别和分类过程,提高药品生产的效率和准确性。
柚子快报官方邀请码778899分享:自动化药片的识别和分类
好文阅读