Main Content

使用网格插值对图像重采样

此示例说明如何使用 griddedInterpolant 对图像中的像素重采样。对图像重采样有助于调整分辨率和大小,也可用于在缩放后对像素进行平滑处理。

加载图像

加载并显示图像 ngc6543a.jpg,这是哈勃太空望远镜对行星状星云 NGC 6543 拍摄的图像。此图像显示了几个有趣的结构,如同心气体壳、高速气体喷射和异常气体结。表示该图像的矩阵 Auint8 整数的 650×600×3 矩阵。

A = imread('ngc6543a.jpg');
imshow(A)

Figure contains an axes object. The axes object contains an object of type image.

创建插值

为该图像创建一个网格插值对象。griddedInterpolant 仅适用于双精度和单精度矩阵,因此请将 uint8 矩阵转换为 double。要对该图像的每个 RGB 通道进行插值,请指定两个网格向量来说明前二个维度中的样本点。网格向量在元胞数组 {xg1,xg2,...,xgN} 中组合为列向量。使用此公式,griddedInterpolant 将三维矩阵视为包含在同一网格上定义的多个二维数据集。

sz = size(A);
xg = 1:sz(1);
yg = 1:sz(2);
F = griddedInterpolant({xg,yg},double(A));

对图像像素重采样

使用前两个矩阵维度的大小对图像进行重采样,使其大小为原来的 120%。也就是说,对于原始图像中的每 5 个像素,插值图像具有 6 个像素。使用语法 F({xq,yq}) 计算查询点处的插值。griddedInterpolant 计算三维图像中每页的查询点处的插值。

xq = (0:5/6:sz(1))';
yq = (0:5/6:sz(2))';
vq = uint8(F({xq,yq}));
imshow(vq)
title('Higher Resolution')

Figure contains an axes object. The axes object with title Higher Resolution contains an object of type image.

同样,可通过查询比原始图像少 55% 的点的插值来减小图像大小。虽然可以直接对原始图像矩阵进行索引以生成较低分辨率的图像,但是插值使您能够在非整数像素位置对图像重采样。

xq = (0:1.55:sz(1))';
yq = (0:1.55:sz(2))';
vq = uint8(F({xq,yq}));
figure
imshow(vq)
title('Lower Resolution')

Figure contains an axes object. The axes object with title Lower Resolution contains an object of type image.

去除缩放伪影

当您放大图像时,目标区域中的像素会变得更大,并且图像中的细节很快会丢失。您可以使用图像重采样去除这些缩放伪影。

放大原始图像中心的亮点。(对 A 进行索引是为了使此亮点在图像中居中,以便后续缩放不会将其推出边框外)。

imshow(A(1:570,10:600,:),'InitialMagnification','fit')
zoom(10)
title('Original Image, 10x Zoom')

Figure contains an axes object. The axes object with title Original Image, 10x Zoom contains an object of type image.

查询插值 F,以大约 10 倍的分辨率重新生成此缩放图像。比较几种不同插值方法的结果。

xq = (1:0.1:sz(1))';
yq = (1:0.1:sz(2))';
F.Method = 'linear';
vq = uint8(F({xq,yq}));
imshow(vq(1:5700,150:5900,:),'InitialMagnification','fit')
zoom(10)
title('Linear method')

Figure contains an axes object. The axes object with title Linear method contains an object of type image.

F.Method = 'cubic';
vq = uint8(F({xq,yq}));
imshow(vq(1:5700,150:5900,:),'InitialMagnification','fit')
zoom(10)
title('Cubic method')

Figure contains an axes object. The axes object with title Cubic method contains an object of type image.

F.Method = 'spline';
vq = uint8(F({xq,yq}));
imshow(vq(1:5700,150:5900,:),'InitialMagnification','fit')
zoom(10)
title('Spline method')

Figure contains an axes object. The axes object with title Spline method contains an object of type image.

另请参阅

|

相关主题