Halcon Programming - texture-based detection mara

Surface defect detection is very important in a machine vision application. Machine vision is a set of optical, electromechanical and three areas of the computer is not a new technology. But the detection of surface flaws in computer science or academia primarily aimed at professional image processing control in the direction of doing, but this one is mainly visual optical system is an optical engineering doing. Few researchers combine these three are good, but domestic machine vision to do this (note that is not a machine vision computer vision) are basically small companies.

    This piece of software will not say bad overall domestic software environment. As far as I know, Japan, Germany and the United States have a lot of relatively mature software in machine vision. Professor Chen Bing flag China Agricultural University to get a lot of dates in between remain in agricultural robot machine vision, and he also wrote a book introduces visual image processing, the latter also appendix main push their own software. German software is halcon to introduce the following. Halcon mentioned and it is necessary to open the opencv comparison, in the image processing opencv is little known, but the image forming function module is not much, but because it is open, and not specifically on the various aspects of the image processing testing needs expand, then again, after all, is open source.

    halcon is commercial software, the major domestic Daheng company is the agent, which in image processing, especially in terms of detection measurement has many advantages. Let me take an example in the introduction halcon image algorithm development process.

      Start the software, ctrl + E pop-up routine, the detection of the selective surface detect_mura_defects_texture. This is mura damage on the LCD panel. Appendix images exist on injury     

   Appearing on the black area is messy texture injury. How to detect it?

code show as below:

 * this example shows how to detect mura defects

* in highly textured images

dev_close_window ()
dev_update_off ()
Path := 'lcd/mura_defects_texture_'
read_image (Image, Path+'01')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, 640, 480, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'courier', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('red')
for f := 1 to 2 by 1
read_image (Image, Path+f$'.2i')
decompose3 (Image, R, G, B)
* defects are characterized by dark patches. Hence, by substracting the
* estimated background illumination from the original image the
* defects become more apparent
estimate_background_illumination (B, ImageFFT1)
sub_image (B, ImageFFT1, ImageSub, 2, 100)
* median filter smooths out the fine texture, simplifying the following
* segmentation and final detection of defects
median_image (ImageSub, ImageMedian, 'circle', 9, 'mirrored')
watersheds_threshold (ImageMedian, Basins, 20)
* dark patches corresponding to defects have a very low energy
cooc_feature_image (Basins, ImageMedian, 6, 0, Energy, Correlation, Homogeneity, Contrast)
tuple_find (sgn(Energy-0.05), -1, Indices)
select_obj (Basins, Defects, Indices+1)

dev_display (Image)
dev_display (Defects)
count_obj (Defects, NDefects)
disp_message (WindowHandle, NDefects+' \'mura\' defects detected', 'window', -1, -1, 'red', 'true')
if (f < 2)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor

 

    1对彩色图像进行R G B分解,选取B作为后续图像。这个原因是通过实验处理的,在B图像下黑色斑和背景差异最大吧。

    2 生成背景模板,将图像傅里叶变换到频域中,通过高斯滤波,然后傅里叶反变换回来,得到的图像就是背景模板

     3 背景差分。采用以下函数,增强两幅图像的差异

sub_image ( ImageMinuend, ImageSubtrahend : ImageSub : Mult, Add : )

ImageSub=(ImageMinuend-ImageSubtrahend)*Mult+Add

     4 分水岭算法分割,在分割之前采用中值滤波来抑制小斑点或细线。分水岭后,图像分割为多个轮廓(region)。

     5 计算多个轮廓region的灰度信息,包括能量。相关度、同一度、对比度,通过灰度共生矩阵。 前面的两个参数是灰度级和方向,灰度级是2^,方向即灰度共生矩阵方向。

     6 根据能量信息对多个region进行筛选,筛选后的region就是检测结果。

复制代码

Halcon::tuple_find ((Energy-0.05).Sgn(), -1, &Indices);
image_opencv_test=m_ip->HImageToIplImage(m_hoImage);
int number=Indices.Num();
for (int i=0;i<number;i++)
{
Halcon::HTuple new_Indices;
new_Indices[0]=Indices[i].I()+1;
Halcon::select_obj (Basins, &Defects, new_Indices);
Hlong x1,x2,y1,y2;
Halcon::smallest_rectangle1(Defects,&y1,&x1,&y2,&x2);
cvRectangle(image_opencv_test,cvPoint(x1,y1),cvPoint(x2,y2),cvScalar(0,0,255),2,8,0);
}

复制代码

    判断能量是否大于0.05,通过sgn函数将大于0.05的置1,小于的置-1。通过find

在(Energy-0.05).Sgn()中寻找-1出现的位置,注意这个位置是从0开始的。貌似这个是经过人为排序的,所以indices 中就是前三个 0 1 2
    select_obj (Basins, &Defects, new_Indices);是从Basins提取序号为new_Indices的region 。为什么重新定义一个HTuple 因为HTuple实际上是个数组。find后得到的就是一个indices 数组,里面存在三个数。 
select_obj 的参数必须是HTuple,也就是数组,所以只能重新定义,而且序号从1开始,因此要加1,只能获取数组中的值加1,
获取的代码为new_Indices[0]=Indices[i].I()+1; 也就是说数组中的值可以直接访问,然后转化为需要的类型。
这是我弄的比较笨的方法,直接通过halcon转化c++代码,只有一句。我觉得还是单个取出来比较靠谱。

贴一个检测结果吧:

 
 

Guess you like

Origin www.cnblogs.com/wwwbdabc/p/11087850.html