裁剪图像后获取矩形图像的逻辑

时间:2019-07-20 10:46:42

标签: python-3.x

在我的项目中,我正在使用labelImg工具裁剪图像。运行python脚本时,裁剪后的图像将存储在指定的文件夹中。保存裁剪后的图像后,它会在内部生成带有裁剪后的图像坐标的xml文件。但是通过运行python脚本,即使我们将图像裁剪为矩形。

这是我的python脚本:

for xml_file in glob.glob(markers_path + '*.xml'):
        print(xml_file)
        tree = ET.parse(xml_file)
        root = tree.getroot()
        objectlist = root.findall('object')
        for member in root.findall('object'):
            value = (root.find('filename').text,
                     int(root.find('size')[0].text),                       # Photo width
                     int(root.find('size')[1].text),                       # Photo height
                     (int(member[4][2].text) + int(member[4][0].text))/2,  # xcenter
                     (int(member[4][3].text) + int(member[4][1].text))/2,  # ycenter
                     (int(member[4][3].text) - int(member[4][1].text)),    # box height
                     (int(member[4][2].text) - int(member[4][0].text)),    # box width
                     member[0].text                                        # Label
                     )

            value        = list(value)
            filename     = value[0]
            photo_width  = value[1]
            photo_height = value[2]
            xcenter      = value[3]
            ycenter      = value[4]
            box_height   = value[5]
            box_width    = value[6]

def try_devision(x,y):
                try:
                    return x / y
                except ZeroDivisionError:
                    # print('The photo width and height are: {}, {}'.format(photo_width,photo_height))
                    return 0

            box_height_ratio = try_devision(box_height, photo_height)
            box_width_ratio = try_devision(box_width, photo_width)

            # Box vierkant en minimale lengte
            if box_height > box_width:
                if box_height < image_crop_size:
                    box_height = image_crop_size
                f_box_size = box_height
            else:
                if box_width < image_crop_size:
                    box_width = image_crop_size
                f_box_size = box_width
#            f_box_size = box_height
#            f_box_size = box_width

            # Locatie van de bounding box
            if xcenter + f_box_size/2 > photo_width:
                xcenter -= xcenter + f_box_size/2 - photo_width
            elif xcenter - f_box_size/2 < 0:
                xcenter += f_box_size/2 - xcenter

            if ycenter + f_box_size/2 > photo_height:
                ycenter -= ycenter + box_width/2 - photo_height
            elif ycenter - f_box_size/2 < 0:
                ycenter += f_box_size/2 - ycenter

            # Coordinaten van de crop

            x1 = xcenter - f_box_size / 2
            x2 = xcenter + f_box_size / 2
            y1 = ycenter - f_box_size / 2
            y2 = ycenter + f_box_size / 2
            coords = [x1,y1,x2,y2]

这是生成的xml代码:

<?xml version="1.0"?>

-<annotation>

<folder>Images</folder>

<filename>Image.jpg</filename>

<path>C:\Users\santhoshe.e\Falker\crop_images_Final\Images\Image.jpg</path>


<source>
<database>Unknown</database>
</source>

<size>
<width>3000</width>
<height>2000</height>
<depth>3</depth>
</size>

<segmented>0</segmented>
<object>

<name>30-pin-to-USB</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>

<bndbox>

<xmin>850</xmin>
<ymin>226</ymin>
<xmax>1216</xmax>
<ymax>448</ymax>
</bndbox>

</object>
</annotation>

任何人都可以建议我使用xml文件中生成的具有完美坐标的图像的逻辑。

0 个答案:

没有答案