目标检测 图片翻转对应xml变换



1. 背景描述:

在利用CNN做目标检测时,数据量不足时,旋转源图像进行数据的扩充。

例:
源图像如下图所示:
这里写图片描述
标记所得xml文件中目标信息如下:

<object>
        <name>airplane</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>431</xmin>
            <ymin>367</ymin>
            <xmax>607</xmax>
            <ymax>453</ymax>
        </bndbox>
    </object>
    <object>
        <name>airplane</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>570</xmin>
            <ymin>419</ymin>
            <xmax>768</xmax>
            <ymax>512</ymax>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

想要将源图像旋转任意角度,相对应xml文件中的bndbox信息则需要更新。

2. 思路:

参考博客(http://blog.csdn.net/u014540717/article/details/53301195)

找到原图中标记方框的四个边中点坐标,计算其旋转后的坐标位置,然后利用cv2.boundingRect函数找到四个新坐标的外接矩形作为新的xml文件中的bndbox值写入。

3. 代码实现过程:

# coding:utf-8
Copyright@hitzym, Dec,09,2017 at HIT # blog:http://blog.csdn.net/yinhuan1649/article/category/7330626
import cv2
import math
import numpy as np
import xml.etree.ElementTree as ET
import os
def rotate_image(src, angle, scale=1):
    w = src.shape[1]
    h = src.shape[0]
    # 角度变弧度
    rangle = np.deg2rad(angle)  # angle in radians
    # now calculate new image width and height
    nw = (abs(np.sin(rangle) * h) + abs(np.cos(rangle) * w)) * scale
    nh = (abs(np.cos(rangle) * h) + abs(np.sin(rangle) * w)) * scale
    # ask OpenCV for the rotation matrix
    rot_mat = cv2.getRotationMatrix2D((nw * 0.5, nh * 0.5), angle, scale)
    # calculate the move from the old center to the new center combined
    # with the rotation
    rot_move = np.dot(rot_mat, np.array([(nw - w) * 0.5, (nh - h) * 0.5, 0]))
    # the move only affects the translation, so update the translation
    # part of the transform
    rot_mat[0, 2] += rot_move[0]
    rot_mat[1, 2] += rot_move[1]
    dst = cv2.warpAffine(src, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))), flags=cv2.INTER_LANCZOS4)
    # 仿射变换
    return dst

对应修改xml文件

 def rotate_xml(src, xmin, ymin, xmax, ymax, angle, scale=1.):
    w = src.shape[1]
    h = src.shape[0]
    rangle = np.deg2rad(angle)  # angle in radians
    # now calculate new image width and height
    # 获取旋转后图像的长和宽
    nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale
    nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale
    # ask OpenCV for the rotation matrix
    rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale)
    # calculate the move from the old center to the new center combined
    # with the rotation
    rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0]))
    # the move only affects the translation, so update the translation
    # part of the transform
    rot_mat[0, 2] += rot_move[0]
    rot_mat[1, 2] += rot_move[1]                                   # rot_mat是最终的旋转矩阵
    # point1 = np.dot(rot_mat, np.array([xmin, ymin, 1]))          #这种新画出的框大一圈
    # point2 = np.dot(rot_mat, np.array([xmax, ymin, 1]))
    # point3 = np.dot(rot_mat, np.array([xmax, ymax, 1]))
    # point4 = np.dot(rot_mat, np.array([xmin, ymax, 1]))
    point1 = np.dot(rot_mat, np.array([(xmin+xmax)/2, ymin, 1]))   # 获取原始矩形的四个中点,然后将这四个点转换到旋转后的坐标系下
    point2 = np.dot(rot_mat, np.array([xmax, (ymin+ymax)/2, 1]))
    point3 = np.dot(rot_mat, np.array([(xmin+xmax)/2, ymax, 1]))
    point4 = np.dot(rot_mat, np.array([xmin, (ymin+ymax)/2, 1]))
    concat = np.vstack((point1, point2, point3, point4))            # 合并np.array
    # 改变array类型
    concat = concat.astype(np.int32)
    rx, ry, rw, rh = cv2.boundingRect(concat)                        #rx,ry,为新的外接框左上角坐标,rw为框宽度,rh为高度,新的xmax=rx+rw,新的ymax=ry+rh
    return rx, ry, rw, rh

使图像旋转60,90,120,150,210,240,300度

xmlpath = './xml/'          #源图像路径
imgpath = './imgs/'         #源图像所对应的xml文件路径
rotated_imgpath = './rotatedimg/'
rotated_xmlpath = './rotatedxml/'
for angle in (60, 90, 120, 150, 180, 210, 240, 300):
    for i in os.listdir(imgpath):
        a, b = os.path.splitext(i)                            #分离出文件名a
        img = cv2.imread(imgpath + a + '.jpg')
        rotated_img = rotate_image(img,angle)
        cv2.imwrite(rotated_imgpath + a + '_'+ str(angle) +'d.jpg',rotated_img)
        print str(i) + ' has been rotated for '+ str(angle)+'°'
        tree = ET.parse(xmlpath + a + '.xml')
        root = tree.getroot()
        for box in root.iter('bndbox'):
            xmin = float(box.find('xmin').text)
            ymin = float(box.find('ymin').text)
            xmax = float(box.find('xmax').text)
            ymax = float(box.find('ymax').text)
            x, y, w, h = rotate_xml(img, xmin, ymin, xmax, ymax, angle)
            # cv2.rectangle(rotated_img, (x, y), (x+w, y+h), [0, 0, 255], 2)   #可在该步骤测试新画的框位置是否正确
            # cv2.imshow('xmlbnd',rotated_img)
            # cv2.waitKey(200)
            box.find('xmin').text = str(x)
            box.find('ymin').text = str(y)
            box.find('xmax').text = str(x+w)
            box.find('ymax').text = str(y+h)
        tree.write(rotated_xmlpath + a + '_'+ str(angle) +'d.xml')
        print str(a) + '.xml has been rotated for  '+ str(angle)+'°'

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

4. 测试旋转结果

将xml中的bounding box 显示在图片上用来测试旋转后结果是否正确

注:
- xml文件需要和其对应的jpg文件文件名一样
- e.g. monkey001.jpg 对应 monkey001.xml
- 上代码

# coding:utf-8

Copyright@hitzym, Dec,09,2017 at HIT # blog:http://blog.csdn.net/yinhuan1649/article/category/7330626

import cv2
import xml.etree.ElementTree as ET
import os

imgpath = './testimgs/'          #旋转后的图像路径
xmlpath = './testxml/'           #旋转后的xml文件路径
for img in os.listdir(imgpath):
    a, b = os.path.splitext(img)
    img = cv2.imread(imgpath + a +'.jpg')
    tree = ET.parse(xmlpath + a + '.xml')
    root = tree.getroot()
    for box in root.iter('bndbox'):
        x1 = int(box.find('xmin').text)
        y1 = int(box.find('ymin').text)
        x2 = int(box.find('xmax').text)
        y2 = int(box.find('ymax').text)
        cv2.rectangle(img,(x1,y1),(x2, y2), [0,255,0], 2)
    cv2.imshow("test", img)
    # cv2.waitKey(1000)
    if 1 == cv2.waitKey(0):
        pass
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

原图:image
结果图:image
这是旋转60°的结果图

主要参考了博客(http://blog.csdn.net/u014540717/article/details/53301195)

转载博客出自:https://blog.csdn.net/yinhuan1649/article/details/78761776

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值