using Images,ImageView
img = load("lighthouse.png");
print(size(img));
结果:
(512, 768)
names(ImageTransformations)
结果:
10-element Array{Symbol,1}:
:ImageTransformations
:InvWarpedView
:WarpedView
:center
:imresize
:imrotate
:invwarpedview
:restrict
:warp
:warpedview
方法1:
cropped_img = img[100:400,100:600];
save("cropped_img.jpg",cropped_img);
# 显示图像
cropped_img
结果:
方法2:
cropped_img_view = view(img,250:350,350:550);
save("cropped_img_view.jpg",cropped_img_view);
cropped_img_view
结果:
例:指定图像高宽(H,W)来调整图像大小
# 指定图像高宽(H,W)来调整图像大小
resized_img = imresize(img,(256,384));
resized_img
结果:
例:通过比例参数调整图像大小
# 通过比例参数调整图像大小
scale_percentage = 0.5
new_size = round.(Int,size(img).*scale_percentage)
resized_img1 = imresize(img,new_size)
结果:
例:通过指定图像宽度调整图像大小
# 通过指定图像宽度调整图像大小
new_width = 300
scale_percentage = new_width /size(img)[2]
new_size = round.(Int,size(img).*scale_percentage)
resized_img2 = imresize(img,new_size)
结果:
例:图像缩小一倍
# 图像高缩小一倍
resized_img3 = restrict(img,1)
# 图像宽缩小一倍
resized_img4 = restrict(resized_img3,2)
mosaicview(resized_img3,resized_img4;nrow=1, rowmajor=true)
结果:
# 图像旋转(采用双线性插值)
rotated_img1 = imrotate(resized_img1,pi/4);
# 图像旋转并保存图像原来尺寸
rotated_img2 = imrotate(resized_img1,pi/4,axes(resized_img1));
# 图像旋转空白区域填充红色
Constant = RGB(1,0,0)
rotated_img3 = imrotate(resized_img1,pi/4,Constant);
# 图像显示
mosaicview(resized_img1,rotated_img1,rotated_img2,rotated_img3;fillvalue=RGB(1,1,1), npad=2, nrow=2, rowmajor=true)
结果:
例:获取图像中心点的位置
# 指定图像高宽(H,W)来调整图像大小
resized_img = imresize(img,(257,385));
#获取图像中心点的位置
println(typeof(center(resized_img)))
centered = center(resized_img)
结果:
2-element StaticArrays.SArray{Tuple{2},Float64,1,2} with indices SOneTo(2):
129.0
193.0
例:
# 图像旋转
using CoordinateTransformations
resized_img = imresize(img,(257,385));
tfm = LinearMap(RotMatrix(-pi/4))
println(RotMatrix(-pi/4))
println(tfm)
img = warp(resized_img,tfm)
结果;
[0.7071067811865476 0.7071067811865475; -0.7071067811865475 0.7071067811865476]
LinearMap([0.7071067811865476 0.7071067811865475; -0.7071067811865475 0.7071067811865476])
例:查看图像旋转或平移后的像素在轴上的范围
println("原图",axes(resized_img))
# 图像旋转
tfm = recenter(RotMatrix(-pi/4), center(resized_img))
#println(tfm)
imgw = warp(resized_img, tfm)
println("图像旋转后",axes(imgw))
# 图像平移
using OffsetArrays
tfm = Translation(50, 50)
println(tfm)
imgw = warp(resized_img, tfm)
save("imgw.png",imgw)
println("图像平移后",axes(imgw))
结果:
原图(Base.OneTo(257), Base.OneTo(385))
图像旋转后(-98:356, -34:420)
Translation(50, 50)
图像平移后(-49:207, -49:335)