我想根据图像中的一个锚计算变换矩阵(旋转、缩放和平移)。
我的图像是一张标签的图片,它总是包含一个数据矩阵。我使用一个第三方库来检测数据矩阵,然后,我得到它的大小,方向(使用数据矩阵的结果)。cv2.minAreaRect(dm_contour)
),和位置.我用这些参数建立了我所谓的 "锚".在第二步,我得到了我所谓的作业,它由用户定义的ROI和用户定义的图片锚组成。
在第二步中,我得到了我所谓的工作,它由用户定义的ROI和用户定义ROI的图片的锚组成。
通过这几个步骤,我可以根据新的标签上下文正确地放置我的ROI,如果它只有一个转换(向左、向右、向上、向下移动)。
但是一旦我尝试在旋转的标签上替换ROI,它就无法工作。
如果我认为我的问题在于我的旋转矩阵和整个 "翻译到原点再回到位置 "的过程。但我找不到我做错了什么......
我的代码转换ROI位置看起来是这样的。
def process_job(anchor, img, job, file_path):
"""
Process job file on current picture
@param anchor = Current scene anchor
@param img = Current picture
@param job = Job object
@param file_path = Job file path
"""
print("Processing job " + file_path)
""" Unpack detected anchor """
a_x, a_y = (anchor[0], anchor[1])
rotation = anchor[2]
anchor_size = int(anchor[3])
for item_i in job:
item = job[item_i]
if 'anchor' in item:
""" Apply size rate """
size_rate = anchor_size / int(item['anchor']['size'])
"""" Item anchor pos """
i_a_x, i_a_y = int(item['anchor']['x']), int(item['anchor']['y'])
""" Calculate transformation """
""" Scaling """
S = np.array([
[size_rate, 0, 0],
[ 0, size_rate, 0],
[ 0, 0, 1]
])
""" Rotation """
angle = rotation - int(item['anchor']['o'])
theta = np.radians(angle)
c, s = np.cos(theta), np.sin(theta)
R = np.array((
(c, s, 0),
(-s, c, 0),
(0, 0, 1)
))
""" Translation """
x_scale = a_x - i_a_x
y_scale = a_y - i_a_y
T = np.array([
[1, 0, x_scale],
[0, 1, y_scale],
[0, 0, 1]
])
""" Shear """
shx_factor = 0
Shx = np.array([
[1, shx_factor, 0],
[0, 1, 0],
[0, 0, 1]
])
shy_factor = 0
Shy = np.array([
[1,0, 0],
[shy_factor, 1, 0],
[0, 0, 1]
])
print("Scaling: " + str(size_rate) + " Rotation:" + str(angle) + " Translation:" + str((x_scale, y_scale)))
if 'rect' in item:
""" Unpack rectangle """
""" (r_x1, r_y1) top-left corner """
""" (r_x2, r_y2) bottom right corner """
r_x1, r_y1, r_x2, r_y2 = (int(item['rect']['x1']), int(item['rect']['y1']), int(item['rect']['x2']), int(item['rect']['y2']))
""" As np arrays """
rect_1 = np.array([r_x1, r_y1, 1])
rect_2 = np.array([r_x2, r_y2, 1])
""" Translate to origen """
T_c_1 = np.array([
[1, 0, -r_x1],
[0, 1, -r_y1],
[0, 0, 1]
])
""" Translate to origen """
T_c_2 = np.array([
[1, 0, -r_x2],
[0, 1, -r_y2],
[0, 0, 1]
])
""" Back to postion """
T_r1 = np.array([
[1, 0, r_x1],
[0, 1, r_y1],
[0, 0, 1]
])
""" Back to postion """
T_r2 = np.array([
[1, 0, r_x2],
[0, 1, r_y2],
[0, 0, 1]
])
""" Apply transformations """
final_1 = T @ T_r1 @ R @ T_c_1 @ S @ rect_1
final_2 = T @ T_r2 @ R @ T_c_2 @ S @ rect_2
x1, y1, x2, y2 = final_1[0], final_1[1], final_2[0], final_2[1]
print("From " + str((r_x1, r_y1, r_x2, r_y2)))
print("To " + str((int(x1), int(y1), int(x2), int(y2))))
cv2.line(img, (int(x1), int(y1)), (int(x2), int(y2)), \
(0,0,0), 2)
cv2.imwrite('./output/job.png', img)
而这里是我的图像的fex样本。
提前感谢您的帮助。
所以,我..,
我甚至不知道是否有人花时间读了我的问题,但如果能帮上忙,以下是我的做法。
在我的第一个代码版本中,我试着计算以下的转换矩阵。
但缺少了其中的两个。
我的第一个第二版看起来像 roi_pos = ShX @ ShY @ S @ T @ T_to_pos @ R @ T_to_origin @ item_roi
结果是非常笨拙的,我用我的模型确定的投资回报率没有正确定位在我的测试样本上。但旋转是正确的,不知为何ROI会落在预期结果附近。
然后我想到了优化我的Datamatrix检测,所以我费尽心思实现了我自己的pythonnumpyopenCV版本的DM检测算法.一个锐化的DM检测帮助我更好地评估我的方向和比例参数,但ROI仍然是偏离的。
所以我发现了homography,这正是我想要的。然后计算两个平面之间发生的变换。
有了这个矩阵'H',我知道可以做的是 roi_pos = H @ item_roi
这就更准确了。
就这样,希望对大家有所帮助。