【语义分割专题】语义分割前期预处理工作--数据处理

本文分享了研究生阶段在语义分割项目中如何通过数据增强技术提升模型鲁棒性,包括几何变换(如翻转、平移)、随机裁剪缩放、纹理增强(亮度对比度、HSV、运动模糊等),以及标准化方法(min-max归一化、z-score标准化)。这些技巧对于提高模型性能和泛化能力至关重要。

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

数据预处理

马上毕业了,毕业之前将研究生阶段做过的一些工作做一些分享。本期将分享语义分割的一些前期预处理以及论文。

1 数据增强

1.1 几何增强

原图:
在这里插入图片描述
在这里插入图片描述

import cv2
import matplotlib.pyplot as plt
%matplotlib inline
from skimage import io


# img = io.imread('image.jpg')
# label = io.imread('label.jpg')

img = cv2.imread('image.jpg')
label = cv2.imread('label.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2默认为bgr顺序
label = cv2.cvtColor(label, cv2.COLOR_BGR2RGB) # cv2默认为bgr顺序
io.imshow(img)
<matplotlib.image.AxesImage at 0x274358194c8>

在这里插入图片描述

io.imshow(label)
<matplotlib.image.AxesImage at 0x274358e2e08>

在这里插入图片描述

水平翻转

  • 水平翻转: 水平镜像图像有助于增加方向的不变性(例如,行人可以以不同的方向出现)。在自然场景下,不建议垂直翻转,因为物体的垂直外观在场景中增加了重要的一致性(例如,网络知道天空是由它的位置决定的),但是像遥感图像这种俯瞰图,是可以垂直翻转的。
img_flip = cv2.flip(img, 1) # 1代表水平翻转,0代表垂直翻转,-1代表对角翻转 
label_flip = cv2.flip(label, 1)
io.imshow(img_flip)
<matplotlib.image.AxesImage at 0x2743595c8c8>

在这里插入图片描述

io.imshow(label_flip)
<matplotlib.image.AxesImage at 0x2743699c608>

在这里插入图片描述

平移

  • 平移: 移动图像会阻止CNN总是看到训练图像的相同位置,因此它不会总是从第一层产生相同的激活(移位不变性)。
# 定义平移矩阵 
import numpy as np

M = np.float32([[1, 0, 100], [0, 1, 0]])
# 变换公式:dst(x, y) = src(M11x + M12y + M13, M21x + M22y + M23)
img_shift = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
label_shift = cv2.warpAffine(label, M, (label.shape[1], label.shape[0]))
io.imshow(img_shift)
<matplotlib.image.AxesImage at 0x27436a09248>

在这里插入图片描述

io.imshow(label_shift)
<matplotlib.image.AxesImage at 0x27436a73d08>

在这里插入图片描述

随机裁剪和缩放

  • 随机裁剪和缩放: 随机调整图像大小有助于模型看到每个对象的不同比例,并提高网络对不同图像分辨率的不变性。
h, w = img.shape[0], img.shape[1]
# 获取随机裁剪长度 
crop_scale = np.random.randint(h//2, h) / h
crop_h = int(crop_scale * h)
crop_w = int(crop_scale * w)
h_begin = np.random.randint(0, h - crop_h)
w_begin = np.random.randint(0, w - crop_w)

img_crop = img[h_begin: h_begin+crop_h, w_begin:w_begin+crop_w, :]
label_crop = label[h_begin: h_begin+crop_h, w_begin:w_begin+crop_w]

# resize函数使用时dsize先是w再是h,正好相反 
img_resize = cv2.resize(img_crop, (w, h))
label_resize = cv2.resize(label_crop, (w, h))
io.imshow(img_resize)
<matplotlib.image.AxesImage at 0x27436adefc8>

在这里插入图片描述

io.imshow(label_resize)
<matplotlib.image.AxesImage at 0x27436b524c8>

在这里插入图片描述

1.2 纹理增强

亮度和对比度

  • 亮度和对比度: 物体在图像中的清晰度取决于场景照明和相机灵敏度。通过随机增加或减少图像亮度来增加输入图像的虚拟变化,改善了网络的照明不变性。对比度就是图像中最暗和最亮区域之间的分隔。通过随机增强来增加这个范围有助于增加对阴影的不变性,并且通常会提高网络在低光照条件下的性能。
from albumentations import (
    Compose,
    RandomBrightnessContrast
)

aug = Compose([RandomBrightnessContrast(brightness_limit=(0, 0.4), contrast_limit=(0, 0.4), p=1)])

augmented = aug(image=img, mask=label)
image_bright_contrast = augmented['image']
gt_bright_contrast = augmented['mask']
io.imshow(image_bright_contrast)
<matplotlib.image.AxesImage at 0x27438dd1d08>

在这里插入图片描述

io.imshow(gt_bright_contrast)
<matplotlib.image.AxesImage at 0x27438e42d08>

在这里插入图片描述

  • 色调 饱和度 明度(HSV): 色调的取值范围是0~360度,用来表示颜色的类别,其中红色是0度,绿色是120度,蓝色是240度。饱和度高,颜色则深而艳。亮度的用来表示颜色的明暗程度。
from albumentations import (
    Compose,
    HueSaturationValue
)

aug = Compose([HueSaturationValue(hue_shift_limit=20,
                                  sat_shift_limit=30,
                                  val_shift_limit=20,
                                  p=1)])

augmented = aug(image=img, mask=label)
image_hsv = augmented['image']
gt_hsv = augmented['mask']
io.imshow(image_hsv)
<matplotlib.image.AxesImage at 0x27438eb0b08>

在这里插入图片描述

io.imshow(gt_hsv)
<matplotlib.image.AxesImage at 0x27438f16d88>

在这里插入图片描述

  • 运动模糊: 对图像进行模糊可以模拟部分拍摄场景的运动模糊,让网络对模糊图像的边界也具有较强的识别能力。
from albumentations import (
    Compose,
    MotionBlur
)

aug = Compose([MotionBlur(blur_limit=7, p=1.0)])

augmented = aug(image=img, mask=label)
image_MotionBlur = augmented['image']
gt_MotionBlur = augmented['mask']
io.imshow(image_MotionBlur)
<matplotlib.image.AxesImage at 0x2743906e448>

在这里插入图片描述

io.imshow(gt_MotionBlur)
<matplotlib.image.AxesImage at 0x274390d6c48>

在这里插入图片描述

  • 颜色抖动: 向每个RGB像素添加小的随机噪声有助于获得对一些相机失真的不变性。
from albumentations import (
    Compose,
    RGBShift
)

aug = Compose([RGBShift(r_shift_limit=20,
                        g_shift_limit=20,
                        b_shift_limit=20,
                        p=1.0)])

augmented = aug(image=img, mask=label)
image_rgbshift = augmented['image']
gt_rgbshift = augmented['mask']
io.imshow(image_rgbshift)
<matplotlib.image.AxesImage at 0x2743a10eec8>

在这里插入图片描述

io.imshow(gt_rgbshift)
<matplotlib.image.AxesImage at 0x2743a17e408>

在这里插入图片描述

限制对比度的自适应直方图均衡化:

from albumentations import (
    Compose,
    CLAHE
)

aug = Compose([CLAHE(clip_limit=2,
                     tile_grid_size=(8, 8),
                     p=1)])

augmented = aug(image=img, mask=label)
image_CLAHE = augmented['image']
gt_CLAHE = augmented['mask']
io.imshow(image_CLAHE)
<matplotlib.image.AxesImage at 0x2743a1e2c88>

在这里插入图片描述

io.imshow(gt_CLAHE)
<matplotlib.image.AxesImage at 0x2743a5b0288>

在这里插入图片描述

其他增强

  • 超像素: 随机将图像上某个超像素块内的颜色替换为超像素的均值。
from albumentations import Compose
from albumentations.imgaug.transforms import IAASuperpixels

aug = Compose([IAASuperpixels(p_replace=0.1,
                              n_segments=500,
                              p=1)])

augmented = aug(image=img, mask=label)
image_Superpixels = augmented['image']
gt_Superpixels = augmented['mask']
io.imshow(image_Superpixels)
<matplotlib.image.AxesImage at 0x2743a612ac8>

在这里插入图片描述

io.imshow(gt_Superpixels)
<matplotlib.image.AxesImage at 0x2743a658fc8>

在这里插入图片描述

  • 锐化: 增强图像细节。
from albumentations import Compose
from albumentations.imgaug.transforms import IAASharpen

aug = Compose([IAASharpen(p=1)])

augmented = aug(image=img, mask=label)
image_Sharpen = augmented['image']
gt_Sharpen = augmented['mask']
io.imshow(image_Sharpen)
<matplotlib.image.AxesImage at 0x2743a704c08>

在这里插入图片描述

io.imshow(gt_Sharpen)
<matplotlib.image.AxesImage at 0x2743b7457c8>

在这里插入图片描述

  • 透视变换:
from albumentations import Compose
from albumentations.imgaug.transforms import IAAPerspective

aug = Compose([IAAPerspective(p=1)])

augmented = aug(image=img, mask=label)
image_Perspective = augmented['image']
gt_Perspective = augmented['mask']

io.imshow(image_Perspective)
<matplotlib.image.AxesImage at 0x2743b7c9a08>

在这里插入图片描述

io.imshow(gt_Perspective)
<matplotlib.image.AxesImage at 0x2743b835288>

在这里插入图片描述

2 标准化与归一化

2.1 min-max归一化

  • 公式:对数据的数值范围进行特定缩放,但不改变其数据分布的一种线性特征变换。
import matplotlib.pyplot as plt
import cv2
import numpy as np
import seaborn as sns

image = cv2.imread('image.jpg')[:, :, 0]
min_ = np.min(image)
max_ = np.max(image)

new_image = (image-min_) / (max_-min_)

flat_image = np.reshape(image, -1)
flat_new_image = np.reshape(new_image, -1)
fig, ax = plt.subplots(1, 2)
sns.distplot(flat_image, ax=ax[0])
sns.distplot(flat_new_image, ax=ax[1])
plt.show()

在这里插入图片描述

2.2 z-score 标准化

  • 公式:将数值范围缩放到0附近, 但没有改变数据分布;
import matplotlib.pyplot as plt
import cv2
import numpy as np
import seaborn as sns

image = cv2.imread('image.jpg')[:, :, 0]
mean_ = np.mean(image)
std_ = np.std(image)

new_image = (image-mean_) / std_

flat_image = np.reshape(image, -1)
flat_new_image = np.reshape(new_image, -1)
fig, ax = plt.subplots(1, 2)
sns.distplot(flat_image, ax=ax[0])
sns.distplot(flat_new_image, ax=ax[1])
plt.show()

在这里插入图片描述

### 医学图像语义分割中的伪影 #### 伪影产生的原因 医学图像语义分割中,伪影通常是由多种因素引起的。这些因素可以分为成像设备相关的原因以及算法设计上的不足。 1. **成像设备的影响** 成像过程本身可能导致伪影的产生。例如,在MRI扫描中,磁场不均匀性、运动伪影或金属植入物可能引起信号失真[^2]。CT图像也可能受到部分体积效应(partial volume effect)影响,从而导致边界模糊或虚假结构的出现。 2. **数据预处理阶段的问题** 数据采集后的噪声未被有效去除或者标准化不当也会引入伪影。如果图像存在强度不均一的情况,则会影响后续模型的学习能力,使得预测结果偏离真实目标区域。 3. **算法局限性** 当前许多深度学习框架虽然能够实现较高的精度,但在面对复杂的解剖结构时仍可能存在挑战。比如卷积神经网络(CNNs)对于高分辨率输入往往需要较大的计算资源来维持细节信息;而在降低分辨率后再恢复的过程中,“上采样”的方式如果不恰当可能会丢失重要特征并造成新的错误边缘定义[^4]。 #### 解决方案 针对上述提到的各种类型的伪影问题,可以从以下几个方面着手改善: 1. **改进硬件条件与优化参数设置** 提升影像质量首先要依赖于先进的医疗仪器和技术手段。调整合适的曝光时间、层厚以及其他扫描参数有助于减少原始资料里的干扰成分。此外,定期校准机器以保持最佳工作状态也是必不可少的一项措施。 2. **增强数据清洗流程** 利用专门开发的数据增强技术和去噪程序可以帮助提高训练样本的质量。这不仅包括简单的平滑滤波器应用,还可以考虑采用更加智能化的方法如自编码器(autoencoder),它们能够在保留主要形态的同时消除不必要的杂音。 3. **创新架构设计** 针对特定应用场景定制化的网络结构能更好地适应各自特点的需求。例如UHRSNet就是专门为超高分辨率图片所提出的解决方案之一[^3]。另外还有S2VNet这样的新型三维到二维转换思路,它既大幅提升了运算效率又兼顾到了准确性提升的要求[^5]。 4. **多模态融合策略** 结合不同模式下的互补优势来进行综合判断也是一种有效的途径。通过整合来自X射线摄影(X-ray), 超声(Ultrasound), 正电子发射断层扫描(PET)/计算机断层扫描(CT)等多种来源的信息, 可以为最终决策提供更多维度的支持. ```python def preprocess_image(image): """ Preprocess the medical image to reduce artifacts. Args: image (numpy.ndarray): Input medical image Returns: numpy.ndarray: Processed image with reduced artifacts """ import cv2 from skimage.restoration import denoise_tv_chambolle # Apply Gaussian blur for noise reduction blurred = cv2.GaussianBlur(image, (5, 5), 0) # Use Total Variation Denoising for further artifact removal cleaned = denoise_tv_chambolle(blurred, weight=0.1) return cleaned ``` 以上代码片段展示了一种基本的数据预处理方法,其中包含了高斯模糊和全变分降噪两个步骤,旨在初步减轻一些常见的伪影现象。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mind_programmonkey

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值