* This example program shows you how to use the GMM classifier for novelty * detection to perform a web inspection task. To perform the novelty detection, * all pixels belonging to the single trained class are computed, and are then * subtracted from the classification ROI to extract the erroneous pixels. For * the web inspection task, the GMM can consequently be used to detect * textures that do not correspond to the texture of the trained good objects. * 此示例程序向您展示如何使用 GMM 分类器进行新颖性检测以执行 网状检测任务。 *为了执行新颖性检测,将计算属于单个训练类的所有像素,然后从分类 ROI 中减去以提取错误像素。 *因此,对于卷材检测任务,GMM 可用于检测与训练好的物体的纹理不对应的纹理。 * dev_update_off () * ReadPretrainedClassifier := false * 取消注释以下行以从磁盘读取预训练的分类器。训练可能持续长达半分钟。 * ReadPretrainedClassifier := true SaveClassifier := false * 取消注释将 GMM训练样本后将分类器保存到磁盘 * SaveClassifier := true * read_image (Image, 'plastic_mesh/plastic_mesh_01') get_image_size (Image, Width, Height) dev_close_window () dev_open_window (0, 0, Width, Height, 'black', WindowHandle) dev_set_color ('red') set_display_font (WindowHandle, 16, 'mono', 'true', 'false') get_system ('example_dir', HalconExamples) * The texture filters used for the classification will return artifacts at the image * borders because the images of the plastic mesh to be inspected do not * contain an integer number of mesh cells. Because this would lead to wrongly * detected errors at the image borders, we must exclude the area close to the * image border from the training and classification. This is done with the following * rectangle. Note that the image is later scaled down by a factor of two. * 用于分类的纹理过滤器将在图像边框处返回伪影,因为要检查的塑料网格的图像不包含整数个网格单元。 *由于这会导致在图像边界处错误检测到错误,因此我们必须从训练和分类中排除靠近图像边界的区域。 *这是通过以下矩形完成的。 请注意,图像稍后会缩小两倍。 gen_rectangle1 (Rectangle, 10, 10, Height / 2 - 11, Width / 2 - 11) if (ReadPretrainedClassifier) * 从磁盘读取先前训练的GMM模型 dev_display (Image) disp_message (WindowHandle, 'Reading classifier from disk...', 'window', 10, 10, 'cyan', 'false') read_class_gmm (HalconExamples + '/hdevelop/Segmentation/Classification/novelty_detection.gmm', GMMHandle) wait_seconds (1.5) else * 创建GMM模型分类器 create_class_gmm (5, 1, [1,5], 'spherical', 'normalization', 5, 42, GMMHandle) * 以5件完好样品来训练模型 for J := 1 to 5 by 1 read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02') * The images are zoomed down because the resolution of the mesh is very * high. This saves a large amount of processing time. *由于网格的分辨率非常高,因此图像被缩小。 这节省了大量的处理时间。 zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant') dev_display (ImageZoomed) disp_message (WindowHandle, '训练样本添加中...', 'window', 10, 10, 'cyan', 'false') * Generate the texture image. *生成纹理图像 gen_texture_image (ImageZoomed, ImageTexture) * 纹理图像添加至分类器 add_samples_image_class_gmm (ImageTexture, Rectangle, GMMHandle, 2.0) endfor dev_display (ImageZoomed) disp_message (WindowHandle, 'Training GMM...', 'window', 10, 10, 'cyan', 'false') * 训练模型. train_class_gmm (GMMHandle, 100, 0.1, 'training', 0.0001, Centers, Iter) if (SaveClassifier) write_class_gmm (GMMHandle, HalconExamples + '/hdevelop/Segmentation/Classification/novelty_detection.gmm') endif endif * 检测塑料网中的异常 dev_set_draw ('margin') dev_set_line_width (3) for J := 1 to 14 by 1 read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02') zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant') dev_display (ImageZoomed) dev_set_color ('white') dev_display (Rectangle) gen_texture_image (ImageZoomed, ImageTexture) reduce_domain (ImageTexture, Rectangle, ImageTextureReduced) * Classify samples belonging to the trained class with the GMM. *使用 GMM 对属于训练类的样本进行分类。 classify_image_class_gmm (ImageTextureReduced, Correct, GMMHandle, 0.000002) * Subtract them from the ROI to obtain the texture errors. *从 ROI 中减去它们以获得纹理误差。 difference (Rectangle, Correct, Errors) * Postprocess the returned raw errors to remove insignificant parts of the * detected errors. * 对返回的原始错误进行后处理,以删除检测到的错误中无关紧要的部分。 opening_circle (Errors, ErrorsOpening, 3.5) closing_circle (ErrorsOpening, ErrorsClosing, 10.5) connection (ErrorsClosing, ErrorsConnected) select_shape (ErrorsConnected, FinalErrors, 'area', 'and', 300, 1000000) count_obj (FinalErrors, NumErrors) dev_set_color ('red') dev_display (FinalErrors) if (NumErrors > 0) disp_message (WindowHandle, 'Mesh not OK', 'window', 10, 10, 'red', 'false') else disp_message (WindowHandle, 'Mesh OK', 'window', 10, 10, 'forest green', 'false') endif if (J < 14) disp_continue_message (WindowHandle,'cyan', 'false') endif stop () endfor
标签:10,false,GMM,检测,image,dev,分类器,mesh,缺陷 From: https://www.cnblogs.com/echo-efun/p/18059032