opencv表面缺陷检测_检测纹理表面凸起、凹痕、划痕缺陷的检测

此示例是一个综合的示例,检测的是皮革纹理表面上出现的凸起、凹痕、划痕上的缺陷。使用的依然是光度立体法,只是不同的缺陷,需要使用的是不同参数所生成的图像。

示例代码如下:

* 使用光度立体的方法检测皮革样品* Initializationdev_update_off ()dev_close_window ()dev_open_window (0, 0, 640, 480, 'black', WindowHandle)set_display_font (WindowHandle, 14, 'mono', 'true', 'false')Message := 'Inspect leather samples using photometric stereo'disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Part 1* * Show input images with different illumination* 先显示并且输入同一图像在不同光照下的图像* 此缺陷检测,检测的是纹理凸起缺陷* 1. 一次性读入多个图像read_image (Images, 'photometric_stereo/leather_1_0' + [1:4])* 循环,一次选择一张图像for I := 1 to 4 by 1    Message := 'Sample 1: Acquire image ' + I + ' of 4'    select_obj (Images, ObjectSelected, I)    dev_display (ObjectSelected)    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')    wait_seconds (0.5)endfor* * * Apply photometric stereo to determine the albedoTilts := [6.1,95.0,-176.1,-86.8]Slants := [41.4,42.6,41.7,40.9]ResultType := ['gradient','albedo']* 2. 用该算子得到反照率图像和表面梯度图像photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])* * Display the albedo image* 显示反照率图像dev_display (Albedo)disp_message (WindowHandle, 'The defect is clearly visible in the albedo image', 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Detect defects* 3. 缺陷检测* * 该二值化处理有几个处理:均值处理->使用标准差进行二值化处理var_threshold (Albedo, Region, 15, 15, 0.2, 0.05, 'light')* 计算连通域,得到分隔开的连通域connection (Region, ConnectedRegions)* 以面积特征,特征直方图选择区域select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 99999)* 联合,联合成一个区域union1 (SelectedRegions, RegionUnion)* 闭运算closing_circle (RegionUnion, RegionClosing, 3.5)* 计算连通域,得到分开的连通域connection (RegionClosing, Defects)* 计算得到区域面积和中心坐标area_center (Defects, Area, Row, Column)* 生成圆, 圈住缺陷部分gen_circle (Circle, Row, Column, gen_tuple_const(|Row|,sqrt(Area) + 30))* Display the defectsdev_display (Albedo)dev_set_color ('red')dev_set_draw ('margin')dev_set_line_width (4)dev_display (Circle)disp_message (WindowHandle, 'Albedo image with defect', 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Part 2* * Show input images with different illumination* 读入图像,一次读入多张在不同光照下的图像read_image (Images, 'photometric_stereo/leather_2_0' + [1:4])for I := 1 to 4 by 1    Message := 'Sample 2: Acquire image ' + I + ' of 4'    select_obj (Images, ObjectSelected, I)    dev_display (ObjectSelected)    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')    wait_seconds (0.5)endfor* * * Apply photometric stereo to determine the albedo* 用该算子得到反照率图像和表面梯度图像photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])* 二值化threshold (Albedo, Region1, 128, 255)* * Display the albedo image* 仅仅只使用二值化的方法处理反照率图像,并不能检测到缺陷区域dev_display (Albedo)Message := 'These defects are difficult to detect in the albedo image.'Message[1] := 'Therefore, we use the gradient information to detect them.'disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Detect texture defects using gradient information.* We are looking for areas with little surface changes.* 使用梯度信息检测纹理缺陷,寻找表面变化很小的区域* * 获得高斯曲率图像derivate_vector_field (Gradient, Curl, 1, 'curl')derivate_gauss (Curl, CurlGradient, 1, 'gradient')* * Display changes of the curl of the gradient fielddev_display (CurlGradient)Message := 'Changes in the gradient curl'disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Detect texture defects* 如下代码检测纹理缺陷* 此处缺陷检测,检测的是纹理的凹痕* 对高斯曲率图像进行二值化处理threshold (CurlGradient, Region, 0, 0.01)* 选择区域rank_region (Region, RegionCount, 10, 10, 30)* 计算连通域,得到分隔开的连通域connection (RegionCount, ConnectedRegions)* 以面积特征,特征直方图选择区域select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 2000, 99999)* 联合,联合成一个区域union1 (SelectedRegions, RegionUnion)* 选择区域rank_region (RegionUnion, RegionCount1, 25, 25, 170)* 计算连通域,得到分隔开的连通域connection (RegionCount1, NoTextured)* * Display texture defects* 在反照率图像上显示选择的区域dev_display (Albedo)dev_set_draw ('margin')dev_set_color ('red')dev_set_line_width (3)dev_display (NoTextured)disp_message (WindowHandle, 'Non-textured areas on leather', 'window', 12, 12, 'black', 'true')stop ()* * Detect scratches using curvature information.* We are looking for areas with high curvature* * 使用曲率信息检测划痕,寻找高曲率区域* * 获得高斯曲率图像derivate_vector_field (Gradient, MeanCurvature, 1, 'mean_curvature')* * Display the mean curvature of the surface* 显示表面的平均曲率dev_display (MeanCurvature)Message := 'Mean curvature of the surface'disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')disp_message (WindowHandle, 'Press F5', 'image', 720, 850, 'black', 'true')stop ()* * Detect scratches* 划痕检测* 此处缺陷检测,检测的是划痕* 计算绝对值图像  因为高斯曲率图像中曲率会有负数abs_image (MeanCurvature, ImageAbs)* 二值化处理threshold (ImageAbs, Region2, 0.15, 255)* 计算连通域,得到分隔开的连通域connection (Region2, ConnectedRegions1)* 以面积特征,特征直方图选择区域select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 10, 99999)* 联合,联合成一个区域union1 (SelectedRegions1, RegionUnion1)* 闭运算closing_circle (RegionUnion1, RegionClosing, 1.5)* 计算连通域,得到分隔开的连通域connection (RegionClosing, ConnectedRegions2)* 以最大直径特征,特征直方图选择区域select_shape (ConnectedRegions2, SelectedRegions2, 'max_diameter', 'and', 50, 99999)* 以灰度值选择区域select_gray (SelectedRegions2, MeanCurvature, SelectedRegions3, 'deviation', 'and', 0.2, 255)* * Display scratches* 在反照率图像显示抓痕dev_display (Albedo)dev_set_draw ('margin')dev_set_color ('red')dev_set_line_width (3)dev_display (SelectedRegions3)disp_message (WindowHandle, 'Deep scratch', 'window', 12, 12, 'black', 'true')

重点说明:

  1.  第一部分的检测,待检测的是皮革表面的凸起,该检测,直接使用的是反照率图像,进行预处理和Blob后,即可得到皮革凸起的缺陷。

  2. 待检测的是皮革表面的凹痕和划痕,该检测,示例中先直接对反照率图像进行二值化之后,确认该方法无法完成对皮革表面缺陷的检测。

  3. 第二部分的检测,待检测的是皮革表面的凹痕,先使用如下算子得到高斯曲率图像:

    derivate_vector_field (Gradient, Curl, 1, 'curl')

    derivate_gauss (Curl, CurlGradient, 1, 'gradient')

    然后使用该高斯曲率图像Blob分析,即可得到皮革凹痕的缺陷。

  4. 第三部分的检测,待检测的是皮革表面的划痕,先使用如下算子得到高斯曲率图像:

    derivate_vector_field (Gradient, MeanCurvature, 1, 'mean_curvature')

    然后使用该高斯曲率图像Blob分析,即可得到皮革划痕的缺陷。

  5. 算子rank_region,使用一个Mask依次对待处理的整个区域过滤,选择需要的区域。

  6. 算子select_gray,按照灰度值来选择区域。

执行流程:

凸起反照率图像如下:

opencv表面缺陷检测_检测纹理表面凸起、凹痕、划痕缺陷的检测_第1张图片

在反照率图像中标记处凸起缺陷:

opencv表面缺陷检测_检测纹理表面凸起、凹痕、划痕缺陷的检测_第2张图片

凹痕反照率图像:

opencv表面缺陷检测_检测纹理表面凸起、凹痕、划痕缺陷的检测_第3张图片

凹痕高斯曲率图像:

opencv表面缺陷检测_检测纹理表面凸起、凹痕、划痕缺陷的检测_第4张图片

在反照率图像中标记处凹痕:

opencv表面缺陷检测_检测纹理表面凸起、凹痕、划痕缺陷的检测_第5张图片

划痕的高斯曲率图像:

opencv表面缺陷检测_检测纹理表面凸起、凹痕、划痕缺陷的检测_第6张图片

在反照率图像中标记处划痕:

opencv表面缺陷检测_检测纹理表面凸起、凹痕、划痕缺陷的检测_第7张图片

不煮鸡汤,不制造焦虑,不宣扬速成。技术都是脚踏实地,功到渠成。

妙玩科技

公众号ID:miuplay

个人微信号:Miuplay

交流QQ群:940437523

你可能感兴趣的:(opencv表面缺陷检测)