JuliaImages / ImageFiltering.jl

Julia implementations of multidimensional array convolution and nonlinear stencil operations
Other
99 stars 51 forks source link

Template matching using mapwindow #217

Closed feanor12 closed 3 years ago

feanor12 commented 3 years ago

Is there an example for template matching . I think mapwindow could be used in combination with different functions.

After mapwindow a threshold to determine the matches could be calculated. I am just not sure how to combine matches that are close together.

feanor12 commented 3 years ago

I managed to get something like this working. I think this would be a nice example, but I am not sure if it can be made faster.

       using Images
       using Plots 
       using ImageFiltering
       using ImageFeatures

    function SDIFF(template)
        (patch)->sum((patch .- template).^2)
        end

    # generate a template
    template = zeros(5,5)
    template[1] = 1
    template[2,1] = template[1,2]= 2

    #generate an image
    img = repeat(template,outer=(4,4))

    #check correlation
    res = mapwindow(SDIFF(template),img,size(template),border=Fill(1))

    #select minimum values
    th = res .< 0.1

    #cluster into compenents and calculate centroid
    centroids = component_centroids(label_components(th))[2:end]

    #plot
    plot(Gray.(img))
    scatter!(reverse.(centroids))
johnnychen94 commented 3 years ago

ImageDistances has some predefined metrics for this. The issue with sum((patch .- template).^2) is that you have to compute an unnecessary intermediate matrix (patch .- template).^2 and then sum it up.

function SDIFF(template)
-    (patch)->sum((patch .- template).^2)
+    (patch)->sqeuclidean(patch, template)
end

Others look good to me. If you have some good ideas, would you mind adding a demo to https://juliaimages.org/ImageFiltering.jl/dev/democards/demos/? That would be very much helpful to future readers.