Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

i do not have a java sdk around, so you'll have to take a Kotlin answer instead here;)

starting like this: our template image description

>>> var tem = Imgcodecs.imread("logo_2.png", 0)
>>> var img = Imgcodecs.imread("multi.png", 0)
>>> var res = Mat()
>>> Imgproc.matchTemplate(img, tem, res, Imgproc.TM_SQDIFF)
>>> var mm = Core.minMaxLoc(res)

now, if we were looking for a single instance, we would be done here, mm.minLoc would be the found position. (or, maxLoc, if TM_ccoefs was used)

for multiple instances, we have to locate the "valleys" (or, "peaks" with ccoefs).

naively, you would traverse the result image, do a 1st order differenciation, and keep the minima/maxima, but that's unpractical in java, so let's try to threshold the result image, until only single points are left, like this

(believe me, there are points in that image, squint real hard ;):

image description

>>> var bin = Mat()
>>> var thresh = mm.minVal*1.05
>>> Imgproc.threshold(res, bin, thresh, 255.0, -1)
>>> var idx = Mat()
>>> Imgcodecs.imwrite("bin.png", bin)
true
>>> Core.findNonZero(bin, idx)
>>> idx.dump()
[130, 60;
 471, 177;
 234, 253]
>>> // done xD. now where's my cookie ?

i do not have a java sdk around, so you'll have to take a Kotlin answer instead here;)

starting like this: our template image description

>>> var tem = Imgcodecs.imread("logo_2.png", 0)
>>> var img = Imgcodecs.imread("multi.png", 0)
>>> var res = Mat()
>>> Imgproc.matchTemplate(img, tem, res, Imgproc.TM_SQDIFF)
>>> var mm = Core.minMaxLoc(res)

now, if we were looking for a single instance, we would be done here, mm.minLoc would be the found position. (or, maxLoc, if TM_ccoefs was used)

for multiple instances, we have to locate the "valleys" (or, "peaks" with ccoefs).

naively, you would traverse the result image, do a 1st order differenciation, and keep the minima/maxima, but that's unpractical in java, so let's try to threshold the result image, until only single points are left, like this

(believe me, there are points in that image, squint real hard ;):

image description

>>> var bin = Mat()
>>> var thresh = mm.minVal*1.05
>>> Imgproc.threshold(res, bin, thresh, 255.0, -1)
>>> var idx = Mat()
>>> Imgcodecs.imwrite("bin.png", bin)
true
>>> var idx = Mat()
>>> Core.findNonZero(bin, idx)
>>> idx.dump()
[130, 60;
 471, 177;
 234, 253]
>>> // done xD. now where's my cookie ?

i do not have a java sdk around, so you'll have to take a Kotlin answer instead here;)

starting like this: our template image description

>>> var tem = Imgcodecs.imread("logo_2.png", 0)
>>> var img = Imgcodecs.imread("multi.png", 0)
>>> var res = Mat()
>>> Imgproc.matchTemplate(img, tem, res, Imgproc.TM_SQDIFF)
>>> var mm = Core.minMaxLoc(res)

now, if we were looking for a single instance, we would be done here, mm.minLoc would be the found position. (or, maxLoc, if TM_ccoefs was used)

for multiple instances, we have to locate the "valleys" (or, "peaks" with ccoefs). ccoefs) in res.

naively, you would traverse the result image, do a 1st order differenciation, and keep the minima/maxima, but that's unpractical in java, so let's try to threshold the result image, until only single points are left, like this

(believe me, there are points in that image, squint real hard ;):

image description

>>> var bin = Mat()
>>> var thresh = mm.minVal*1.05
>>> Imgproc.threshold(res, bin, thresh, 255.0, -1)
-1) // invert, since we're looking for minima
>>> Imgcodecs.imwrite("bin.png", bin)
bin) // for visualization
true
>>> var idx = Mat()
>>> Core.findNonZero(bin, idx)
idx) // get the positions
>>> idx.dump()
[130, 60;
 471, 177;
 234, 253]
>>> // done xD. now where's my cookie ?

first, you should look at the c++ tutorial for this ..

i do not have a java sdk around, so you'll have to take a Kotlin answer instead here;)

starting like this: our template image description

>>> var tem = Imgcodecs.imread("logo_2.png", 0)
>>> var img = Imgcodecs.imread("multi.png", 0)
>>> var res = Mat()
>>> Imgproc.matchTemplate(img, tem, res, Imgproc.TM_SQDIFF)
>>> var mm = Core.minMaxLoc(res)

now, if we were looking for a single instance, we would be done here, mm.minLoc would be the found position. (or, maxLoc, if TM_ccoefs was used)

for multiple instances, we have to locate the "valleys" (or, "peaks" with ccoefs) in res.

naively, you would traverse the result image, do a 1st order differenciation, and keep the minima/maxima, but that's unpractical in java, so let's try to threshold the result image, until only single points are left, like this

(believe me, there are points in that image, squint real hard ;):

image description

>>> var bin = Mat()
>>> var thresh = mm.minVal*1.05
>>> Imgproc.threshold(res, bin, thresh, 255.0, -1) // invert, since we're looking for minima
>>> Imgcodecs.imwrite("bin.png", bin) // for visualization
true
>>> var idx = Mat()
>>> Core.findNonZero(bin, idx) // get the positions
>>> idx.dump()
[130, 60;
 471, 177;
 234, 253]
>>> // done xD. now where's my cookie ?