tf.image.sample_distorted_bounding_box(含其他对图形的操作)

本文介绍了一个用于图像预处理的函数sample_distorted_bounding_box,它能生成随机裁剪的边界框,适用于增强图像数据集。该函数通过指定的边界框参数确保裁剪区域包含一定比例的对象信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此函数为图像生成单个随机变形的边界框。函数输出的是可用于裁剪原始图像的单个边框。返回值为3个张量:begin,size和 bboxes。前2个张量用于 tf.slice 剪裁图像。后者可以用于 tf.image.draw_bounding_boxes 函数来画出边界框。

image_size: 是包含 [height, width, channels] 三个值的一维数组。数值类型必须是 uint8,int8,int16,int32,int64 中的一种。

bounding_boxes: 是一个 shape 为 [batch, N, 4] 的三维数组,数据类型为float32,第一个batch是因为函数是处理一组图片的,N表示描述与图像相关联的N个边界框的形状,而标注框由4个数字 [y_min, x_min, y_max, x_max] 表示出来。例如:tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]]) 的 shape 为 [1,2,4] 表示一张图片中的两个标注框;tf.constant([[[ 0.  0.  1.  1.]]]) 的 shape 为 [1,1,4]表示一张图片中的一个标注框.

min_object_covered:(必选)数组类型为 float,默认为 0.1。图像的裁剪区域必须包含所提供的任意一个边界框的至少 min_object_covered 的内容。该参数的值应为非负数,当为0时,裁剪区域不必与提供的任何边界框有重叠部分。

sample_distorted_bounding_box(
    image_size
,
    bounding_boxes
,
    seed
=None,
    seed2
=None,
    min_object_covered
=None,
    aspect_ratio_range
=None,
    area_range
=None,
    max_attempts
=None,
    use_image_if_no_bounding_boxes
=None,
    name

Return:一个Tensor对象的元组(begin,size,bboxes)。

begin: 和 image_size 具有相同的类型。包含 [offset_height, offset_width, 0] 的一维数组。作为 tf.slice 的输入。

size: 和 image_size 具有相同的类型。包含 [target_height, target_width, -1] 的一维数组。作为 tf.slice 的输入。

bboxes:shape为 [1, 1, 4] 的三维矩阵,数据类型为float32,表示随机变形后的边界框。作为 tf.image.draw_bounding_boxes 的输入。

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
#读取原始图像,获取图像的原始数据
image_raw_data = tf.gfile.FastGFile("/home/cvx/Downloads/image.jpeg", 'rb').read()

with tf.Session() as sess:
    #将图像使用jpeg的格式解码从而得到图像对应的三维矩阵,tf.image.decode_png函数对png格式的图像进行解码,
    #解码后的结果是一个tensor
    image_data = tf.image.decode_jpeg(image_raw_data)
    #print(image_data.eval())
    #plt.imshow(image_data.eval())
    #plt.show()
    #将数据的类型转化成实数,但是报错了
    #image_data = tf.image.convert_image_dtype(image_data, dtype=tf.float32)



    resized = tf.image.resize_images(image_data, [300, 300], method=3)
    #print(resized.dtype)
    #print(image_data.get_shape())

    # TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片
    resized = np.asarray(resized.eval(), dtype='uint8')
    img_data = tf.image.convert_image_dtype(resized, dtype=tf.uint8)
    #plt.imshow(img_data.eval())
    #plt.show()
    encoded_image = tf.image.encode_jpeg(img_data)
    with tf.gfile.GFile("/home/cvx/Downloads/re3-image.jpeg", "wb") as f:
        f.write(encoded_image.eval())

    # 调整图像大小
    padded = tf.image.resize_image_with_crop_or_pad(image_data, 3000, 3000)
    padded = np.asarray(padded.eval(), dtype='uint8')
    padded_img_data = tf.image.convert_image_dtype(padded, dtype=tf.uint8)
    #plt.imshow(padded_img_data.eval())
    #plt.show()
    encoded_image = tf.image.encode_jpeg(padded_img_data)
    with tf.gfile.GFile("/home/cvx/Downloads/re4-image.jpeg", "wb") as f:
        f.write(encoded_image.eval())

    transposed = tf.image.transpose_image(image_data)
    #plt.imshow(transposed.eval())
    #plt.show()

    filpped = tf.image.random_flip_up_down(image_data)
    #plt.imshow(filpped.eval())
    #plt.show()

    adjusted = tf.image.random_brightness(image_data, 0.5)
    #plt.imshow(adjusted.eval())
    #plt.show()

    adjusted_constrast = tf.image.random_contrast(image_data, 0,  20)
    #plt.imshow(adjusted_constrast.eval())
    #plt.show()

    adjusted_hue = tf.image.random_hue(image_data, 0.5)
    #plt.imshow(adjusted_hue.eval())
    #plt.show()

    adjusted_saturation = tf.image.adjust_saturation(image_data, -5)
    #plt.imshow(adjusted_saturation.eval())
    #plt.show()


    adjusted_whitening = tf.image.per_image_standardization(image_data)
    #有负值,所以画图画不出来
    #plt.imshow(adjusted_whitening.eval())
    #plt.show()

    #缩小图像,让标注框更清晰
    resized_image = tf.image.resize_images(image_data, (200, 200), method=1)
    batched = tf.expand_dims(tf.image.convert_image_dtype(resized_image, tf.float32), 0)
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
    result = tf.image.draw_bounding_boxes(batched, boxes)
    #resized_image_show = np.asarray(result.eval(), dtype=tf.uint8)
    result1 = tf.squeeze(result, axis=0)
    #plt.imshow(result1.eval())
    #plt.show()


    #随机截取图像上有信息含量的部分,也可以提高模型健壮性
    #此函数为图像生成单个随机变形的边界框。函数输出的是可用于裁剪原始图像的单个边框。
    #返回值为3个张量:begin,size和 bboxes。前2个张量用于 tf.slice 剪裁图像。
    #后者可以用于 tf.image.draw_bounding_boxes 函数来画出边界框。
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
    print(tf.shape(image_data))
    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(image_data), bounding_boxes=boxes, min_object_covered=0.1)
    batched = tf.expand_dims(tf.image.convert_image_dtype(image_data, tf.float32), 0)
    image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw)
    distorted_image = tf.slice(image_data, begin, size)
    plt.imshow(distorted_image.eval())
    plt.show()


### 几何失真原因分析 几何失真通常由多种因素引起,主要包括外部环境干扰、传感器测量误差以及算法建模中的假设偏差。在视觉惯性里程计(VIO)领域,结构信息的利用可以显著改善点线特征提取过程中的精度[^1]。然而,在实际应用中,由于相机内外参校准不精确或者动态场景下的运动模糊效应,可能导致两元素之间的相对位置关系发生畸变。 另外,在图像恢复任务中发现,空间自适应网络能够通过学习局部区域内的变形模式来有效缓解因镜头光学特性引起的径向和切向形变问题[^2]。这表明采用数据驱动的方法同样适用于处理复杂条件下的多源失配现象。 对于分布式威尔金森功率分配器的设计而言,则需注意其物理实现过程中可能出现的不对称布局所引发的整体性能下降情况。具体来说就是当输入端口与输出支路间存在阻抗不平衡状态时会破坏原有理想模型从而造成最终合成信号相位偏移过大进而影响整个系统的稳定性表现[^3]。 ### 解决方案探讨 针对上述提到的各种可能诱因分别给出如下几种可行对策: #### 改进硬件配置 - 使用更高分辨率摄像头并配合精密加工制造出来的透镜组以减少像差带来的负面影响; #### 软件优化策略 - 引入基于深度学习框架构建起来的空间变换预测模块以便实时补偿检测到的变化趋势;同时还可以考虑加入额外约束项比如平滑正则化因子进一步增强鲁棒性效果。 ```python import torch.nn as nn class SpatialTransformer(nn.Module): def __init__(self, input_channels=2): super(SpatialTransformer, self).__init__() self.localization = nn.Sequential( nn.Conv2d(input_channels, 8, kernel_size=7), ... ) def forward(self, x): theta = self.localization(x) grid = F.affine_grid(theta, x.size()) output = F.grid_sample(x, grid) return output ``` #### 参数调整建议 - 对于射频电路部分应严格遵循理论计算所得最佳工作区间范围设定各元件参数值,并定期维护保养确保长期处于良好运行状况之下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值