数字图像预处理

编程入门 行业动态 更新时间:2024-10-17 23:35:01

<a href=https://www.elefans.com/category/jswz/34/1763678.html style=数字图像预处理"/>

数字图像预处理

1.背景

        在医学图像处理中, 我们一般会进行一些非刚性变换做data augment,这么做可以使得训练数据的分布更加复杂,提高模型的泛化能力。

# coding:utf-8
# Import stuff
import os
import numpy as np
#import pandas as pd
import cv2
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
import matplotlib.pyplot as plt# Function to distort image
def elastic_transform(image, alpha, sigma, alpha_affine, random_state=None):# If size is None, then a single value is generated and returned.if random_state is None:random_state = np.random.RandomState(None)# The form of shape is as follows (weight, heght, channels)shape = image.shapeshape_size = shape[:2]# Random affine# 对于仿射变换,我们只需要知道变换前的三个点与其对应的变换后的点,就可以通过cv2.getAffineTransform求得变换矩阵.center_square = np.float32(shape_size) // 2square_size = min(shape_size) // 3# pts1 是变换前的三个点,pts2 是变换后的三个点pts1 = np.float32([center_square + square_size, [center_square[0] + square_size, center_square[1] - square_size],center_square - square_size])pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)# 进行放射变换M = cv2.getAffineTransform(pts1, pts2)# 默认使用 双线性插值,这里使用 三次样条插值。处理速度会变慢,但是可以最大程度的保留细节,适用于医学图像image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101, flags=cv2.INTER_CUBIC)# random_state.rand(*shape) 会产生一个和 shape 一样打的服从[0,1]均匀分布的矩阵# * 2 - 1 是为了将分布平移到 [-1, 1] 的区间# gaussian_filter:将高斯滤波器作用于刚刚的卷积上,其中卷积核的大小由参数 sigma 决定# 这个卷积核具体是如何计算的可以参考如下的链接# .15.1/scipy/ndimage/filters.py#L180# 实际上 dx 和 dy 就是在计算论文中弹性变换的那三步:产生一个随机的位移,将卷积核作用在上面,用 alpha 决定尺度的大小dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alphady = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alphadz = np.zeros_like(dx)# np.meshgrid 生成网格点坐标矩阵,并在生成的网格点坐标矩阵上加上刚刚的到的dx dyx, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))# indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1)), np.reshape(z, (-1, 1))return map_coordinates(image, indices, order=3, mode='reflect').reshape(shape)# Define function to draw a grid
def draw_grid(im, grid_size):# Draw grid linesfor i in range(0, im.shape[1], grid_size):cv2.line(im, (i, 0), (i, im.shape[0]), color=(255,))for j in range(0, im.shape[0], grid_size):cv2.line(im, (0, j), (im.shape[1], j), color=(255,))if __name__ == '__main__':img_path = 'data'mask_path = 'mask'img_list = sorted(os.listdir(img_path))mask_list = sorted(os.listdir(mask_path))img_num = len(img_list)mask_num = len(mask_list)assert img_num == mask_num, 'img nuimber is not equal to mask num.'count_total = 0for i in range(img_num):im = cv2.imread(os.path.join(img_path, img_list[i]), -1)im_mask = cv2.imread(os.path.join(mask_path, mask_list[i]), -1)# # Draw grid lines# draw_grid(im, 50)# draw_grid(im_mask, 50)# Merge images into separete channels (shape will be (cols, rols, 2))im_merge = np.concatenate((im[..., None], im_mask[..., None]), axis=2)# get img and mask shortname(img_shotname, img_extension) = os.path.splitext(img_list[i])(mask_shotname, mask_extension) = os.path.splitext(mask_list[i])# Elastic deformation 10 timescount = 0while count < 15:# Apply transformation on imageim_merge_t = elastic_transform(im_merge, im_merge.shape[1] * 2, im_merge.shape[1] * 0.08,im_merge.shape[1] * 0.08)# Split image and maskim_t = im_merge_t[..., 0]im_mask_t = im_merge_t[..., 1]# save the new imgs and maskscv2.imwrite(os.path.join(img_path, img_shotname + '-' + str(count) + img_extension), im_t)cv2.imwrite(os.path.join(mask_path, mask_shotname + '-' + str(count) + mask_extension), im_mask_t)count += 1count_total += 1if count_total % 100 == 0:print('Elastic deformation generated {} imgs', format(count_total))# # Display result# print 'Display result'# plt.figure(figsize = (16,14))# plt.imshow(np.c_[np.r_[im, im_mask], np.r_[im_t, im_mask_t]], cmap='gray')# plt.show()

2.目录结构

将训练图像放入到data文件夹中,将掩码图像放入到mask文件夹中。

     上面这张图片就是原始训练图像

     上面这张图就是原始图像经过随机仿射和随机弹性变换得到的增强后的图像

     上面这张图是原始图像的mask掩码

     经过随机弹性变换之后得到的掩码Mask

更多推荐

数字图像预处理

本文发布于:2024-02-07 03:09:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1752805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!