使用Julia进行图像处理--用于扩充训练集的图像增强

8 篇文章 4 订阅
7 篇文章 76 订阅

前言

JuliaImages将图片表示为二维数组,每个像素都可以是标量或一维数组:

  1. 灰度图像将像素表示为标量值,它们称为灰度
  2. RGB是三维数组,以三种不同的颜色采用加色法表示每个点
  3. 具有透明背景的PNG图像具有RGBA四个通道

“访问”像素

在Julia中使用load命令时,它将读取图像并将其编码为RGB,下示代码展示了Julia如何管理单个像素的信息:

using Images
img = load("pictures/custom_image_name.jpg")
img[1:1, 1:1, :]

在Julia执行了上示的命令之后,看到以下输出:

julia> img[1:1, 1:1, :]
1×1×1 Array{RGB{N0f8},3} with eltype RGB{Normed{UInt8,8}}:
[:, :, 1] =
 RGB{N0f8}(0.353,0.286,0.216)

像素由一个RGB对象组成,其中0.349,0.282,0.212是与三个通道中的每个通道的值相对应的值。
图像以从0到1的浮点值表示:

  1. 0代表黑色
  2. 1代表白色

浮点数是由1/255缩放的8位整数。例如,白色表示为(1.0,1.0,1.0),黑色表示为(0,0,0),灰色表示为(0.5,0.5,0.5)。这并不是通用的非常常见的表示形式,但是由于在神经网络中使用图像之前的预处理步骤中被广泛使用。

将像素转换成数字数组

当在Julia中加载彩色图像时,该图像将存储为RGB4对象的二维数组,这是通过使用channelview函数将RGB4分解为多个通道来实现的:

using Images, ImageView
img = load("pictures/custom_image_name.jpg")
img_channel_view = channelview(img)
img_channel_view

可以在Julia REPL中看到类似于以下内容的输出:

julia> img_channel_view
img_channel_view = channelview(img)
3×360×640 reinterpret(N0f8, ::Array{RGB{N0f8},3}):
[:, :, 1] =
 0.353  0.353  0.345  0.329  0.302

现在,每个像素都由三个不同的值表示,可以使用矩阵运算来更改像素的内容。用大于0.7的值更新所有颜色,使其为0.9:

using Images, ImageView
img = load("pictures/custom_image_name.jpg")
img_channel_view = channelview(img)
img_channel_view[img_channel_view .> 0.7] .= 0.9
imshow(img)

这将导致背景颜色发生变化:

像素转换

将数字数组转换为色彩

为了可视化,需要将数字数组转换为颜色。这是通过colorview功能实现的。使用下面的代码以生成一个随机的三维数组并将其转换为RGB图像:

using Images, ImageView
random_img_array = rand(3, 8, 8); # channel, height, width
img = colorview(RGB, random_img_array);
imshow(img)

可视化

重要的是要注意,通道维度在第一位,然后是高度,最后是宽度。例如,如果通道维不是第一个,而是最后一个,则应使用permuteddimsview或permutedims函数将它们按正确的顺序放置:

using Images, ImageView
random_img_array = rand(40, 100, 3); # height, width, channel
img_perm = permuteddimsview(random_img_array, (3, 1, 2))
img = colorview(RGB, Float16.(img_perm))
imshow(img)

完成相同功能的另一种实现方法如下:

using Images, ImageView
random_img_array = rand(40, 100, 3); # height, width, channel
img_perm = permutedims(random_img_array, [3, 1, 2])
img = colorview(RGB, img_perm)
imshow(img)

两种方法的区别在于分配内存的时刻。第一个选项在Float16时分配内存,第二个选项在permutedims时分配。

改变色彩饱和度

学习了如何将颜色转换为多维通道,现在,将学习如何在RGB配色方案中更改特定通道的内容,以模拟照片应用程序中的滤镜。
首先加载图像,然后执行以下步骤:

  1. 将图像转换为channelview,并将通道设置为最后一个维度:
using Images, ImageView
img = load("pictures/custom_image_name.jpg");
img_ch_view = channelview(img); # extract channels
img_ch_view = permuteddimsview(img_ch_view, (2, 3, 1)); 
# reorder channels
  1. 将现有图像分为两部分,并对第二部分应用滤镜:
x_coords = Int(size(img, 2) / 2):size(img, 2); 
  1. 首先将第一个通道的饱和度增加10%,并将其限制为最大值1。这是通过将现有通道值乘以1.1与min函数相结合来完成的。还请记住,第一个通道对应于红色:
img_ch_view[:, x_coords, 1] = min.(img_ch_view[:, x_coords, 1] .* 1.1, 1);
imshow(img)
  1. 通过将其饱和度增加20%来更新第二个通道。第二层对应于绿色:
img_ch_view[:, x_coords, 2] = min.(img_ch_view[:, x_coords, 2] .* 1.2, 1);
imshow(img)
  1. 更改与蓝色相对应的第三个通道,并将其强度增加40%:
img_ch_view[:, x_coords, 3] = min.(img_ch_view[:, x_coords, 3] .* 1.4, 1);
imshow(img)

改变色彩饱和度

色阶是从0.01.0,其他值不被接受。
此处并没有分配内存,而是使用视图来更新原始图像的内容。

将图像转化为灰度图像

计算机视觉中最受欢迎的活动之一是转换为图像并使用其灰度版本。在Julia中,这是通过使用Gray函数来实现的:

using Images
img = load("pictures/custom_image_name.jpg");
img_gray = Gray.(img)
imshow(img_gray)

灰度图像

灰度图像广泛用于经典计算机视觉中,用于诸如特征检测,形态学等任务。使用灰度图像的好处是它们以一个单一的维度表示,因此可以快速进行处理和分析。
与RGB图像的三维尺寸相比,Gray返回的尺寸为单个尺寸。使用以下命令将一个通道的灰度图像转换为一个三通道的灰度RGB图像:

img_gray_rgb = RGB.(Gray.(img))

创建自定义滤镜

创建一个颜色飞溅滤镜效果。通过使用表示将要保留的颜色的点或区域来实现颜色飞溅滤镜效果。
从加载图像开始:

using Images, ImageView
# load an image and create a grayscale copy
img = load("pictures/custom_image_name.jpg");
img_gray = RGB.(Gray.(img))

要点如下:

  1. 创建图像的灰度副本,并将其用作最终结果的基础
  2. 结合使用Gray和RGB来获得图像的灰度版本的三通道表示

使用channelview函数在三个通道中表示图像对象。更改维度顺序并将通道尺寸放在最后:

# get channels representation
img_channel_view = channelview(img);
img_gray_channel_view = channelview(img_gray);
# make channel dimension last and crop the required are
img_arr = permuteddimsview(img_channel_view, (2, 3, 1));
img_gray_arr = permuteddimsview(img_gray_channel_view, (2, 3, 1));

使用遮罩来跟踪要保留的像素和颜色。最初,将其设置为保留所有像素:

# create a mask with all values being true
img_mask = fill(true, size(img));

在继续更新遮罩之前,需要在原始图像中定义一个点区域。使用该区域提取要保留的颜色。可以将值更改为任何大小,只要它适合原始区域并进行预览即可:

# spot are with colors to retain
img_spot_height = 430:460
img_spot_width = 430:460
# preview color area (optional)
imshow(img[img_spot_height, img_spot_width])

下一部分将执行以下操作:

  1. 在for循环中遍历图像的三个通道,并分别处理每个通道
  2. 通过通道裁剪感兴趣的区域
  3. 确定该区域的最小和最大颜色
  4. 通过检查通道颜色值是否在最小/最大范围内来创建通道蒙版
    在图像和通道蒙版上应用逻辑AND操作

上述操作的代码如下:

for channel_id = 1:3
    # select current channel and crop areas of interest
    current_channel = view(img_arr, :, :, channel_id)
    current_channel_area = current_channel[img_spot_height,img_spot_width, :]
    # identify min and max values in a cropped area
    channel_min = minimum(current_channel_area)
    channel_max = maximum(current_channel_area)
    # merge existing mask with a channel specific mask
    channel_mask = channel_min .< current_channel .< channel_max
    img_mask = img_mask .& channel_mask
end

最后一步是应用img_mask并使用灰度合并源图像:

img_masked = img_arr .* img_mask .+ img_gray_arr .* .~(img_mask);
final_image = colorview(RGB, permutedims(img_masked, (3, 1, 2)))
imshow(final_image)

色彩喷溅滤镜

填充图片

在某些时候,可能需要为图像添加边框。 ImageFiltering包扩展了padarray函数,并允许以其他方式添加填充。

填充恒定值

在图像周围添加一个具有25像素大小的边框。这是通过组合padarray和Fill函数来完成的:

using Images, ImageView
pad_vertical = 25
pad_horizontal = 25
pad_color = RGB4{N0f8}(0.5,0.5,0.) # border color
img = load("pictures/custom_image_name.jpg");
img = padarray(img, Fill(pad_color, (pad_vertical, pad_horizontal)))
img = parent(img) # reset indices to start from 1
imshow(img)

pad

通过复制图像内容进行填充

使用图像的内容来创建边框。julia有四种在边框增加时可以使用的类型(样式):

类型描述示例
:replicate边界像素扩展出图像边界UUUU-UVWXYZ-ZZZZ
:circular边框像素环绕WXYZ-UVWXYZ-UVWX
:symmetric边界像素相对于像素之间的位置进行反向传播YXWV-UVWXYZ-YXWV
:reflect边界像素相对于边缘本身反向填充XWVU-UVWXYZ-ZYXW

例如,使用:reflect将复制边框像素:

using Images, ImageView
pad_vertical = 25
pad_horizontal = 25
img = load("pictures/custom_image_name.jpg");
img = padarray(img, Pad(:reflect, pad_vertical, pad_horizontal))
img = parent(img) # reset indices to start from 1
imshow(img)

填充

图像模糊

在图像增强中,高斯模糊是减少图像细节并实现模糊效果的一种广泛使用的方法。这是一个非线性操作。
为了在Julia中应用高斯模糊,使用ImageFiltering.jl包中的高斯核和imfilter函数:

using Images, ImageView
img = load("pictures/custom_image_name.jpg");
img_area_range = 320:640
img_area = img[:, img_area_range]
img_area = imfilter(img_area, Kernel.gaussian(5))
img[:, img_area_range] = img_area
imshow(img)

gaussian filter

Julia对高斯核的实现采用一个像素表示周围区域,并将目标像素替换为区域中值。核大小应为正整数,其中1表示最小平滑。
当不适合放入播放框的视频需要增加尺寸且边框模糊时,尝试实现类似Instagram的视频效果。将首先加载图像,创建顶部和底部填充,最后模糊它们:

using Images, ImageVie
border_size = 50
gaussian_kernel_value = 10
img = load("pictures/custom_image_name.jpg");
# add borders
img = padarray(img, Pad(:reflect, border_size, 0))
img = parent(img) # reset the indices after using padarray
# apply blurring to top border
img_area_top = 1:border_size
img[img_area_top, :] = imfilter(img[img_area_top, :], Kernel.gaussian(gaussian_kernel_value))
# apply blurring to bottom border
img_area_bottom = size(img, 1)-border_size:size(img, 1)
img[img_area_bottom, :] = imfilter(img[img_area_bottom, :], Kernel.gaussian(gaussian_kernel_value))
imshow(img)

gaussian filter

图像锐化

实现由GIMP和Photoshop等现代图形编辑器使用的图像锐化技术。这实际上将高斯平滑滤波器应用于图像的副本,并从原始图像中减去平滑后的版本(以加权方式,以使恒定区域的值保持不变):

using Images, ImageView
gaussian_smoothing = 1
intensity = 1
# load an image and apply Gaussian smoothing filter
img = load("pictures/custom_image_name.jpg");
imgb = imfilter(img, Kernel.gaussian(gaussian_smoothing));
# convert images to Float to perform mathematical operations
img_array = Float16.(channelview(img));
imgb_array = Float16.(channelview(imgb));
# create a sharpened version of our image and fix values from 0 to 1
sharpened = img_array .* (1 + intensity) .+ imgb_array .* (-intensity);
sharpened = max.(sharpened, 0);
sharpened = min.(sharpened, 1);
sharpened_image = colorview(RGB, sharpened)
imshow(sharpened_image)
# optional part: comparison
img[:, 1:321] = img[:, 320:640];
img[:, 320:640] = colorview(RGB, sharpened)[:, 320:640];
imshow(img)

图片锐化

后记

Enjoy coding.

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

盼小辉丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值