Search code examples
computer-visionhalcon

Extract grid data from noisy image in Halcon


I am trying to get clean grid data from this image (don't mind the tilt, it is incidental).

enter image description here

This means no noisy dots and a uniform grid representing the situation. I am doing fairly well using morphology, doing:

closing_rectangle1 ()
opening_rectangle1 ()
select_shape ('rectangularity')

in different orders and amounts to clean up some noise.

enter image description here

It is not as complete and uniform as I'd like to achieve, so I'm wondering if Halcon has a better way of doing this (other than morphology or as an addition to) that I am not aware of. The rectangle-orientation and pattern in the grid can be different but the size is always the same.


Solution

  • read_image (Image, 'D:/OneDrive - Subpixel d.o.o/Other/Stackoverflow/5/uZKGw.png')
    threshold (Image, Region, 128, 255)
    connection (Region, ConnectedRegions)
    select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
    
    ** One region is disconnected, which interferes with watershed algorithm
    dilation_circle (SelectedRegions, RegionDilation, 1.5)
    
    get_image_size (Image, Width, Height)
    gen_image_const (Image1, 'byte', Width, Height)
    paint_region (RegionDilation, Image1, ImageResult, 255, 'fill')
    
    watersheds (ImageResult, Basins, Watersheds)
    select_shape (Basins, SelectedRegions1, 'area', 'and', 4000, 8000)
    
    ** Transform everything to rectangles 2 (rect1 if you can ignore the angle)
    shape_trans (SelectedRegions1, RegionTrans, 'rectangle2')
    
    ** Reduce the regions a bit to achive separation
    erosion_circle (RegionTrans, RegionErosion, 3.5)
    

    enter image description here