Template Matching - NCC

  • Hi ,


    I am tryin to do the template matching: I tested the provided example "normalized cross correlation". The example seems to detect the object with dimension of 12 pixels by using the operator FIR kernel 12x12, is this correct?

    I want to match an image using sub-image as template, how should I revise the design?


    Thanks

    Bingnan

  • Dear Bingnan,

    Thank you for your question.

    When using FIR kernel one has to be careful not to use too large Kernels, since this will increase the used FPGA ressources by a lot. Therefore, it is important to know how large your sub-image will be?

    If it is 12x12 Pixel (or somewhere in that region) you can convert your image file to a text file and load it in the FirOperator by clicking on Coefficents -> File.

    Inside the example directory you can find a NCC.m script that shows you how to convert a tif file into the apropriate textformat (basically writing one pixel per line)

    If your subimage is vastly larger than that more advanced techniques must be used, e.g. cutting the image in tiles by using the ImageBufferMultiROI or downscaling your subimage.

    Greetings,

    Kevin

  • Hi Bingnan,

    You can use the Operator "SplitComponents" to divide the input image in its three componets (R,G,B) and then compute the filter on each one. To combine the results together again to one RGB Image use the operator "MergeComponents"

    Greetings

    Kevin

  • I think the problem is that I am using the gray camera operator, how should I modify the design? (change the RGB settings)

  • Hi Bingan,

    Sorry for the confusion. The error message comes from the upcoming Opreator "SelectComponent". This in the example because an RGB Camera was used.

    Since you already have a gray camera you should be able to simply delete the "SelectComponent" Operator and combine the 12x12 FIRkernelNxM Operator with the NCC H-Box

    Greetings

    kevin

  • Thank you for your advice

    I am trying to use my images, but the result shows failed (blank image)

    I loaded my .txt file into the FirOperator in the NCC hierarchical box. The text file is from the 12x12 Downscaled image I made by OpenCV.

    Could you please check what is the problem? (I noticed that there are other FirOperators in other hierarchical boxes, but I suppose the parameters there are from FIRkernal and don't need to be changed)


    Greetings

    Bingnan

  • Hi Bingan,

    You can find a description on what to adjust in the text-box in the design.

    Furthermore: If you downscale your subimage with the object you try to find, you may need to also downsample your input image so that the relationship between the object in the image, and in the subimage stays the same.

    Note that there are some more things to do then to just use the pixel value, however they are all documented in the text box in the design.

    Greetings,

    Kevin

  • Hi


    I downscaled both template and background images 6times, and followed the instruction on the text box and adjusted my design and images. The result like thispasted-from-clipboard.png

    I am not sure if it properly detected the template-like object, and the area of the pixel with the value 255 seems weird. Is this the problem with the threshold value? Or the template?


    Thanks

    Bingnan

  • And here is the template.


    My goal is to detect the droplet with the number of one particle, as the template shows. I am not sure if there is a better way than template matching.

  • Dear Bingnan,


    thank you for the description of the problem and the VisualApplets design with the corresponding images. I had a look in your design and found that the content of the FIRoperators "Rmean_Mult_K" and "Sum_RxR" are not adapted to your template image. These operators need to be adapted as well as the threshold for visualization. Please see details in the comment box on the top level of the design and under https://docs.baslerweb.com/vis…izedCrossCorrelation.html



    pasted-from-clipboard.png

  • Dear Carmen,


    Thank you for your reply, could you please the attachment at #9, the design loaded the values from the Matlab code.


    Best

    Bingnan

  • Dear Bingnan,

    In the design attachment of Comment 9, some of the FIR operator values in module "Sigma_I" were 0 due to the kernel extension to 15x16 pixels. Please find in the attachment the updated design. With adjustment of the threshold for visualization the middle of the circle objects are visible. However at the botom and top of the input images also matchings are found. I assume this is because of the background contained in the template objects.

  • Hi


    I noticed that for the 'NCC' operator, the K_NumberOfObjectPixels is a value limited to [0,500]. Is that mean the size of the template is limited to the total pixels of 500? Would it be possible to use a larger template?


    Thanks

  • Hi


    I noticed that for the 'NCC' operator, the K_NumberOfObjectPixels is a value limited to [0,500]. Is that mean the size of the template is limited to the total pixels of 500? Would it be possible to use a larger template?


    Thanks

    Because I found out that the template is not correctly detected - now all droplets are detected by the template. The template image, which the target aimed to be detected, is a circle within one small circle inside (a droplet with one particle inside). The droplet without particle inside, for example, aimed not to be detected. Is there any suggestion to improve the design to complete the classification more accurately?

  • I selected the "inside little circle" as a template instead of the whole droplet, but the result seems weird?

  • Dear Bingnan,


    please find atatched the updated design. In this design the small circle is detected

    Following improvements are implemented:

    1. The number of fractional bits for division by number "K" is increased to 10 fractional bits

    2. In module "SigmaI" a module to avoid division by zereo is added.


    The changes are marked in yellow in the following screnshot.


    pasted-from-clipboard.png


    Concerning comment #15: The size of the template is only limited by the FPGA resources. You may change the upper limit of the operator IntParamTranslator "TranslateK" if FPGA resources allow a bigger template.

  • Dears,


    Thank you a lot for your information. I am trying to output a trigger signal if the desired object (the small circle) detected, The hierarchical box ImageTrigger seems not suitable and complicated. To implement this simple IF-THEN function, is there another proper way?


    Best

    Bingnan

  • Dear Bingnan,

    Thank you for reaching out. The ImageTrigger Box is for accessing the camera, my question is what do you exactly mean by a trigger signal. Do you want to trigger the Camera again if you detect the circle, or do you want to acces the GPIO pins from the frame grabber? If it is the latter, I have attached a small VA example on how to access these.

    Please let me know what do you need in detail.

    Greetings,

    Kevin

  • I meant the GPO trigger. The example seems helpful, thank you!