我正在尝试制作更多的数据集,以便在Tensorflow中为数据挖掘训练模型。我将边界框的标签添加到原始图像中。我想将图像旋转45度,并修改新的精确边界框(矩形)的xml文件来标记新创建的图像。它正在调整大小并提取到窗口,以避免丢失图像上的任何内容。
让我告诉你我是如何尝试的:
def rotateImage(mat, angle):
height, width = mat.shape[:2]
image_center = (width / 2, height / 2)
rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1)
radians = math.radians(angle)
sin = math.sin(radians)
cos = math.cos(radians)
bound_w = int((height * abs(sin)) + (width * abs(cos)))
bound_h = int((height * abs(cos)) + (width * abs(sin)))
rotation_mat[0, 2] += ((bound_w / 2) - image_center[0])
rotation_mat[1, 2] += ((bound_h / 2) - image_center[1])
rotated_mat = cv2.warpAffine(mat, rotation_mat, (bound_w, bound_h))
return rotated_mat
image = cv2.imread("test.jpg")
angle = 45
rotated_45_image = image.copy()
rotated_45_image = rotateImage(rotated_45_image, angle=45)
tree_for_45_rotated = ET.parse(file_name + ".xml")
root = tree_for_xml.getroot()
for object in root.iter("object"):
xmin = object.find("bndbox").find("xmin")
ymin = object.find("bndbox").find("ymin")
xmax = object.find("bndbox").find("xmax")
ymax = object.find("bndbox").find("ymax")
print(xmin.text, ymin.text, xmax.text, ymax.text)
print("new")
new_xmin = math.cos(angle) * int(xmin.text) - math.sin(angle) * int(ymin.text)
new_xmax = math.cos(angle) * int(xmax.text) - math.sin(angle) * int(ymin.text)
new_ymin = math.sin(angle) * int(xmin.text) + math.cos(angle) * int(ymin.text)
new_ymax = math.sin(angle) * int(xmin.text) + math.cos(angle) * int(ymax.text)
print(new_xmin, new_ymin, new_xmax, new_ymax)
顺便说一下,我正在使用Python和OpenCV。我无法计算精确的新坐标来标记图像。
谢谢