Reputation: 1947
I have an image here:
I wanna locate the question number, i.e.:
So, generally, for normal shape, I can use shape detection or template matching for this guy:
However, there is number within the region.
Anyone know this situation?
Opencv: 3.2.0
Python: 2.7.10
Edit 1
Here is code for template matching:
#!/usr/bin/env python
import cv2
import numpy as np
img_rgb = cv2.imread('papere1.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('no.png',0)
#template = cv2.imread('2.jpg',0)
#template = cv2.imread('papere3.jpg',0)
#cv2.imshow('origin',img_rgb)
#cv2.waitKey(0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.4
loc = np.where( res >= threshold)
print loc
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2.imshow('Detected',img_rgb)
cv2.waitKey(0)
And here is template png:
However, target comes up only when I set threshold < 0.45, and even this target is not accurate...
Edit 2
with above code set threshold to 0.6, I got this:
So, seems good, but we can see target with 6 is missed. And I think the more digit number within target will get the lower match.
Thanks.
Upvotes: 1
Views: 755
Reputation: 3115
Following discussion in comments and chat,
The Steps to do to achieve this are as follows:
Pre-requisite - If you don't want to implement pyramid for template matching (if it isn't required due to overkill), make sure the template is as big as the one in the main image.
Step 1 : Run cv2.matchTemplate
with appropriate correlation measure.
Step 2 : Set an appropriate threshold for the measure for correct detection.
Step 3: The OP mentioned the squares having digits so digit recognition from here and setting the pixels for the contours as black and then running cv2.matchTemplate
should work as well.
PS. The OP mentioned doing digit recognition after detecting the squares so this kind of solves the other problem too.
Upvotes: 1