获取精确尺寸的裁剪图像

时间:2019-07-19 12:26:47

标签: python-3.x

我制作了一个python脚本,用于使用labelImg工具获取具有裁剪坐标作为输出的裁剪图像。但是我没有获得具有精确裁剪尺寸的裁剪图像。

这是我获取剪切图像作为输出的逻辑:

def labelimg_process(folder ,image_crop_size = 112, image_size = 224,log = False):
    # Date
    now = datetime.datetime.now()
    day = now.day
    month = now.month
    year = now.year
    date = '%i-%i-%i' % (day,month,year)

    # Paden naar de mappen
    image_path = os.path.join(folder,'Images',"")
    markers_path = os.path.join(folder,'Markers',"")
    crop_path = os.path.join(folder,'Crop_files',"")
    image_path = os.path.join(folder,'Images',"")

 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

            # 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]
if __name__ == "__main__":
    folder_path = input('Enter folder path?')
    labelimg_process(folder_path ,image_crop_size = 200, image_size = 224,log = False)

输入图像: 这是我要裁剪的图像。蓝色表示裁剪的图像。 It is the image i want to crop.In that the blue color indicates the cropped image.

输出图像: 输出图像不完美。 The output image is not perfect.

任何人都可以建议我需要更改逻辑的地方。 预先感谢。

0 个答案:

没有答案