如何在cv2.transform中使用warp_matrix(来自cv2.findTransformECC) [英] How to use warp_matrix (from cv2.findTransformECC) with cv2.transform

查看:690
本文介绍了如何在cv2.transform中使用warp_matrix(来自cv2.findTransformECC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由点[[x1,y1],[x2,y2],...,[xn,yn]]的数组定义的形状,以及一个图像(img1),其中(几乎)是我需要找到该形状的位置,这意味着如果在图像(img2)上的任意位置绘制此形状,我会发现仿射变换从img1变为img2。我设法做到了cv2.findTransformECC。我得到了warp_matrix。

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

代码:

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)

结果:
[img3] https ://i.imgur.com/zCXJoyJ.png

然后我想直接在想要使用cv2.transform的合适位置绘制形状但是我的工作很奇怪,好像旋转角度与不良信号一起使用。

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.

以下代码是我的问题,请参见img4中的结果:

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)

推荐答案

尝试在转换图像时使用的cv2.findTransformECC函数中交换图像的顺序,并使用新的扭曲矩阵转换点。在浏览opencv文档后,也请尝试更改与cv2.findTransformECC的 criteria属性关联的值。我肯定这些会有所帮助,因为我过去曾遇到过类似的问题。

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.

这篇关于如何在cv2.transform中使用warp_matrix(来自cv2.findTransformECC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆