I'm trying to use opencv to detect components in electronic diagrams photos. I am using ORB feature detector with BFMathcer.To detect the multiple occurences I'm using a sliding window approach for now. In my first test I tried to detect diodes on a circuit image, but end up with no key points detected on the diode template.
My first solution was to stretch the image before detecting key points, but I was getting poor matches.
Diode template
Circuit used
(Sorry about the links, can`t embed images...)
Is there a better way to extract key points? Or any other suggestions?
It would be good if the solution was scale/rotation invariant.
cv::Mat template_img, capture;
std::vector<cv::KeyPoint> mTemplate_key_points;
cv::Mat mTemplate_descriptors;
std::vector<cv::KeyPoint> key_pts;
cv::Mat descriptors;
auto ORB = cv::ORB::create(2000);
ORB->detectAndCompute(template_img, cv::noArray(), mTemplate_key_points, mTemplate_descriptors);
ORB->detectAndCompute(capture, cv::noArray(), key_pts, descriptors);
cv::BFMatcher matcher(cv::NORM_HAMMING);
std::vector< std::vector<cv::DMatch> > nn_matches;
matcher.knnMatch(mTemplate_descriptors, descriptors, nn_matches,2);
std::vector<cv::KeyPoint> matched1, matched2;
float feature_distance =0;
for (auto& descriptor_match: nn_matches)
{
float dist1 = descriptor_match[0].distance;
float dist2 = descriptor_match[1].distance;
feature_distance += dist2 -dist1;
if(dist1 < 0.8f * dist2)
{
matched1.push_back(mTemplate_key_points[descriptor_match[0].queryIdx]);
matched2.push_back(key_pts[descriptor_match[0].trainIdx]);
}
}
std::vector<cv::Point2f> template_hom;
for (auto pt : matched1)
{
template_hom.push_back(pt.pt);
}
std::vector<cv::Point2f> image_hom;
for (auto pt : matched2)
{
image_hom.push_back(pt.pt);
}
auto my_homography = cv::findHomography(image_hom, template_hom);
cv::Mat cuted_component;
cv::warpPerspective(distorted, cuted_component, my_homography, cv::Size(300,300));