问题
I've got an shape defined by an array of points [[x1,y1],[x2,y2],...,[xn,yn]] and an image (img1) with (almost) by I need to find where is this shape, by that I mean if a draw this shape at an arbitrary place on an image (img2) I find the affine transformation to go from an img1 to img2. I managed to do this cv2.findTransformECC. I get a warp_matrix.
[img1] https://i.imgur.com/097V8YM.png [img2] https://i.imgur.com/dNUrgE8.png
The code :
def get_gradient(im) :
# Calculate the x and y gradients using Sobel operator
grad_x = cv2.Sobel(im,cv2.CV_32F,1,0,ksize=3)
grad_y = cv2.Sobel(im,cv2.CV_32F,0,1,ksize=3)
# Combine the two gradients
grad = cv2.addWeighted(np.absolute(grad_x), 0.5, np.absolute(grad_y), 0.5, 0)
return grad
img1=cv2.imread('img1.png',0)
points=np.array([[ 834, 429],
[ 867, 419],
[ 900, 409],
[ 934, 400],
[ 967, 391],
[1001, 382],
[1035, 375],
[1069, 369],
[1102, 364],
[1136, 361],
[1170, 361],
[1204, 362],
[1238, 365],
[1272, 370],
[1306, 376],
[1340, 385]])
img2=np.zeros_like(img1)
cv2.polylines(img2,[points],False,255,4)
warp_mode = cv2.MOTION_AFFINE
warp_matrix = np.eye(2, 3, dtype=np.float32)
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 3000, 1e-5)
(cc, warp_matrix) = cv2.findTransformECC (get_gradient(img1), get_gradient(img2),warp_matrix, warp_mode, criteria)
img3 = cv2.warpAffine(img2, warp_matrix, (img1.shape[1],img1.shape[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
The result : [img3] https://i.imgur.com/zCXJoyJ.png
Then I want to direcly draw the shape at the good position I tryed to use cv2.transform but I works strangely, as if the angle of rotation was used with the bad sign.
The following code were is my problem, see the result in img4 :
warp_points=cv2.transform(np.reshape(points,(points.shape[0],1,2)),warp_matrix)
img4=img1.copy()
cv2.polylines(img4,[warp_points],False,100,4) #100 : gray
[img4] https://i.imgur.com/JvUHBVK.png
By advance thx, (and sorry for any english mistakes, it's not my mother tong)
回答1:
Try swapping the order of the images in the cv2.findTransformECC function you have used while transforming the images and use the new warp matrix to transform your points. Also try changing values associated with the "criteria" attribute of the cv2.findTransformECC after going through the opencv documentation. I am sure these will help as I have faced a similar issue in the past.
来源:https://stackoverflow.com/questions/54463722/how-to-use-warp-matrix-from-cv2-findtransformecc-with-cv2-transform