简体   繁体   中英

OpenCV Template Matching for Round Images in Java

I dont always have square/rectangle images, sometimes I should match round images as well. Below are 2 images for example. Ball is the template image and the second is Source where the template should be searched. I can make the background of template transparent, but this gives error, making it white decreases the match score, because as you see on the Source image there is no white around the ball. These are just 2 examples pictures. Do you have suggestions/solutions?

要搜索的模板图像

源图像

I think you can also use histogram backprojection for this. There you can use an arbitrary shape mask as well. Convolve the mask with the backprojected image and you'll detect a peak in the region where the object occurs in the image as shown in the images (color mapped and scaled) below.

backprojected:

在此处输入图片说明

convolved:

在此处输入图片说明

EDIT:

This is based on this paper . I was experimenting it and was hoping to post in a blog. This is in C++.

// model histogram: this is the football template
calcHist(&model32fc3, 1, (const int*)channels, modelMask, histModel, 3, (const int*)histSize, (const float**)ranges, true, false);
// image histogram
calcHist(&image32fc3, 1, (const int*)channels, Mat(), histImage, 3, (const int*)histSize, (const float**)ranges, true, false);
// ratio histogram
divide(histModel, histImage, histRatio);
cv::min(histRatio, 1.0, histRatio);
// backproject ratio histogram
calcBackProject(&image32fc3, 1, (const int*)channels, histRatio, backprj, (const float**)ranges);
// convolve the kernel with the backprojected image
filter2D(backprj, conv, CV_32F, modelMask);

That's OK, you can still use matchTemplate() and get excellent results:

在此处输入图片说明

You can find a decent tutorial on OpenCV's documentation . By the way, this is the output of the demo shared there.

If you know the pixels belonging to the template, you can write your matcher

sum of absolute differences trial (pseudo code)

Mat I, T // image and template
vector<Point> template_pixels
Rect sliding_window
vector<double> match_rates

for all rows in image
update sliding_window
    for all cols in image
    update sliding_window
    Mat W = I(sliding_window)
    sum = 0
         for all rows in template
              for all cols in template
              if(template_pixels contains pixel i)
                   sum += abs(W(i) - T(i))
              end for
         end for
    match_rates.pushback(sum)
    end for
end for

minMaxLoc(match_rates)

and optimize it with multithreading on image rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM