How can I export each image match using a template in opencv?

asked 2019-06-24 16:14:51 -0500

updated 2019-06-24 20:18:34 -0500

supra56 gravatar image

I need to export each match template in diferents images for example.

In this url there is an example about how to draw the border of the match. https://docs.opencv.org/trunk/d4/dc6/...

In this example I need to be able to export 19 coins.

I can export just the first coin using this:

--#path template str(sys.argv[ai[1]])
trainImg_c=cv2.imread(str(sys.argv[ai[1]]),0)
trainKP_c,trainDesc_c=detector.detectAndCompute(trainImg_c,None)
--#path of the image str(sys.argv[1])
QueryImg_c=cv2.imread(str(sys.argv[1]),0)
queryKP_c,queryDesc_c=detector.detectAndCompute(QueryImg_c,None)
matches_c=flann.knnMatch(queryDesc_c,trainDesc_c,k=2)

goodMatch_c = []

for m_c,n_c in matches_c:
    if(m_c.distance<0.75*n_c.distance):
        goodMatch_c.append(m_c)

tp=[]
qp=[]
for m_c in goodMatch_c:
    tp.append(trainKP_c[m_c.trainIdx].pt)
    qp.append(queryKP_c[m_c.queryIdx].pt)
tp,qp=np.float32((tp,qp))
H,status=cv2.findHomography(tp,qp,cv2.RANSAC,3.0)
h,w=trainImg_c.shape
trainBorder=np.float32([[[0,0],[0,h-1],[w-1,h-1],[w-1,0]]])

queryBorder=cv2.perspectiveTransform(trainBorder,H)
cv2.polylines(QueryImg_c,[np.int32(queryBorder)],True,(0,255,0),5)  

perspectiveM=cv2.getPerspectiveTransform(np.float32(queryBorder),trainBorder)
found = cv2.warpPerspective(QueryImg_c,perspectiveM,(w,h))  

cv2.imwrite(destiny_file_type, found)
edit retag flag offensive close merge delete

Comments

1

blindly copy/pasting arbitrary code snippets won't get you anywhere.

berak gravatar imageberak ( 2019-06-25 00:53:39 -0500 )edit
1

The code doesn't seem to match the question

Chris gravatar imageChris ( 2019-06-29 10:33:22 -0500 )edit