computer-visioncross-correlationhalcon

Improving NCC Template Matching Accuracy in HALCON


I've been working with NCC template matching in HALCON and am facing challenges with accurately locating my template in the search image. Despite trying various preprocessing techniques and utilizing Regions of Interest (ROIs), I consistently encounter issues with precision and sometimes fail to detect the template entirely. I've attached images of both the template and the search image below.

Search Image: enter image description here

Template Image:

gen_rectangle1 (ROI, 651.344, 888.522, 730.939, 931.704)
reduce_domain(image, ROI, templimage)

Result Image: enter image description here

impath := '/path/to/search_image.bmp'

read_image(image, impath)
rgb1_to_gray(image, image)
gen_rectangle1 (ROI, 545.35, 739.29, 612.312, 776.902)
reduce_domain(image, ROI, templimage)

* create model
create_ncc_model (templimage, 'auto', rad(0), rad(359), 'auto', 'use_polarity', NCCModelID)

* find model
find_ncc_model(image, NCCModelID, rad(0), rad(359), 0.68, 0, 0.8, 'true', 0, Row, Column, Angle, Score)
dev_clear_window()
dev_display(image)
dev_display_ncc_matching_results(NCCModelID, 'red', Row, Column, Angle, 0)

Could anyone suggest strategies to enhance the accuracy and reliability of the NCC template matching in this context? Are there specific preprocessing methods or settings in HALCON that could help in consistently identifying the template correctly?

Thank you for any insights or advice!


Solution

  • I think shape-based matching would give you better results, but if you have to use NCC this is result I have so far:

    enter image description here

    dev_update_off ()
    
    read_image (Image, 'C:/Users/Perkovic/Desktop/Jff7mFg2.png')
    rgb1_to_gray (Image, Image)
    
    L1 := 41
    L2 := 25
    gen_rectangle2 (ROI, 691.4061, 911.5726, rad(-88.09105), L1, L2)
    reduce_domain(Image, ROI, templimage)
    gen_rectangle2 (ROI_0, 718.03, 909.1579, rad(90), 13.33325, 21.16724)
    gen_rectangle2 (TMP_Region, 680.0816, 909.1579, rad(90.66528), 29.40351, 13.56099)
    union2 (ROI_0, TMP_Region, ROI_0)
    
    ** Reduce domain
    threshold (Image, Region, 0, 80)
    fill_up (Region, RegionFillUp)
    connection (RegionFillUp, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 10e3)
    dilation_circle (SelectedRegions, RegionDilation, 70.5)
    union1 (RegionDilation, RegionUnion)
    reduce_domain (Image, RegionUnion, ImageReduced)
    
    * create model
    create_ncc_model (templimage, 'auto', -0.39, 6.29, 'auto', 'use_polarity', NCCModelID)
    
    * find model
    dev_clear_window()
    dev_display(Image)
    while (true)
        find_ncc_model(ImageReduced, NCCModelID, -0.39, 6.29, 0.6, 1, 0.6, 'true', 0, Row, Column, Angle, Score)
        
        if (Score == [])
            break
        endif
        dev_display_ncc_matching_results(NCCModelID, 'red', Row, Column, Angle, 0)
        
        dev_disp_text (Score, 'image', Row, Column, 'black', [], [])
        
        ** Remove rectangle from reduced image
        gen_rectangle2 (Rectangle, Row, Column, Angle, L2, L1)
        get_domain (ImageReduced, Domain)
        difference (Domain, Rectangle, RegionDifference)
        reduce_domain (ImageReduced, RegionDifference, ImageReduced)
    endwhile