Reputation: 583
I have a huge circuit diagram that contains symbols like diodes, resistors, transistors.
I have to select symbols (based on a input template) from this image and highlight those using a rectangular bounding box. The symbols may be rotated (and scaled) - but for now let's not consider scaling/rotation. There could be multiple symbols and lines in the same column/row.
The image size is 5600x3600. It is grayscale, converted to b/w and then the whole thing can be mentioned as 0s and 1s where 1 == black pixel. I think white pixels can be considered as "don't-cares"
What is the correct and faster method to do this?
I have a template image extracted from the larger image - for example "diode.png". Let's say that at least one symbol matches exactly without scaling or rotation.
Brute force template/matrix comparison approach works for me - but it is very slow.. takes 45 minutes (on the latest MBP-2.4ghz-Intel i5 / 8GB) to detect one exact match. I am using numpy wherever I could - but probably I am not using certain numpy iterators.
Unfortunately, I cannot do this outside python and cannot reduce the image size below 5600x3600 without making symbols unrecognisable.
Another way to put the problem is to : find all matching sub-matrices from a large matrix.
What is the correct and performing method to achieve the above? I do not understand signal processing much - but could that help here? The template size (submatrix) in here is minimum 50x50
EDIT: OpenCV is not an option for me - skimage is available. However, please do not let that stop your inputs.
I cannot share the actual diagrams but a sample is here. For example diodes are labeled as D1 and D2 here. The full diagram is as clear as this but on a canvas of 5600 x 3600
I would also like to add that match_template from skimage works perfectly with smaller tiles extracted from the larger image
Upvotes: 2
Views: 3916
Reputation: 842
Did you try Sum of Absolute Difference ? It is one of the simplest way to do template matching but it is also a brute force method.
I applied it for logo spotting in document images (A4, 300 dpi, 2500 * 3500 pixels). It is slow but it don't take 45 minutes ! In order to speed up you can split the image (chose overlapping zones in order to don't miss your symbol).
Upvotes: 2
Reputation: 32521
The term you're looking for is "template matching".
It's normally assumed that due to image noise and other factors, there will not be an exact match for the template. Instead you look for the image region with a minimal error score.
You can find an implementation using normalized cross-correlation in scikit-image:
It would help if you added your existing code and a sample image and template.
Upvotes: 2