'how to find missing component in pcb using feature matching in opencv python

I am working on project where i have to find missing component in PCB, i have try it with image subtraction but as my lighting conditions are not good also images are of not same orientation so i am trying it with feature matching. I am not able to detect each an every component also not able to locate the position of missing component properly. please tell me how should i proceed further.Here is my code

import cv2

img1 = cv2.imread('./pcb_images/frame_51.png')
img1 = cv2.resize(img1, (640, 480))

img2 = cv2.imread('./pcb_images/frame_56.png')
img2 = cv2.resize(img2, (640, 480))

orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
#print(kp1)
#print(kp2)
img_kp = cv2.drawKeypoints(img2, kp2, 0, color=(0,255,0), flags=0)
cv2.imshow("img_kp",img_kp)
# matcher takes normType, which is set to cv2.NORM_L2 for SIFT        and SURF, cv2.NORM_HAMMING for ORB, FAST and BRIEF
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
#print(bf)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance)# draw first 50 matches
#print(matches)


 match_img = cv2.drawMatches(img1, kp1, img2, kp2, matches[:20], None)
 cv2.imshow('Matches', match_img)
 cv2.waitKey()
 cv2.destroyAllWindows()

[![original image]2]2 [defected image3



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source