Image 理解最近邻图像大小调整算法

Image 理解最近邻图像大小调整算法,image,algorithm,Image,Algorithm,我在读关于它的文章,不确定我是否理解它是如何工作的。以下是对算法的正确解释吗?假设我们想将图像拉伸3倍,从2×2大小的图像拉伸到6×6: Map every pixel value in the old image to its location in the resized image. Refer to these as 'landmarks'. - For example, (0,0) -> (0,0) (0,1) -> (0,3)

我在读关于它的文章,不确定我是否理解它是如何工作的。以下是对算法的正确解释吗?假设我们想将图像拉伸3倍,从2×2大小的图像拉伸到6×6:

Map every pixel value in the old image to its location in the resized image. 
Refer to these as 'landmarks'.
 - For example, (0,0) -> (0,0)
                (0,1) -> (0,3) 
                ...
Starting at (0,1), for every pixel in the resized image that doesn't have a value,
assign to that pixel the value of the closest landmark. 

这是正确的吗?

我将对您的“地标”的位置做一个小的调整。与其将第一个放在(0,0),不如将其放在(1,1):

x
标记地标,
字符是未定义的像素。
我们现在在(1,1)、(1,4)、(4,1)和(4,4)有地标。我们如何知道原始图像中的哪些像素对应于这些地标?最简单的方法是使用整数除法除以比例因子,在本例中为3:

1 div 3 = 0
4 div 3 = 1
现在,我们可以将每个地标像素映射到原始图像中对应的像素:

landmark    original
  (1,1)  =>   (0,0)
  (1,4)  =>   (0,1)
  (4,1)  =>   (1,0)
  (4,4)  =>   (1,1)
现在,如果我们对调整大小的图像中的其他像素进行尝试,会发生什么?比如说,如果我们取像素(2,3),我们能确定它最接近原始图像中的哪个像素吗?那么

2 mod 3 = 0
3 mod 3 = 1
因此,原始图像中对应的像素是(0,1)。这是与我们的地标(1,4)对应的同一个像素,因此它一定是我们最近的邻居。使用新像素调整大小后的图像如下所示:

_ _ _ _ _ _
_ x _ _ x _
_ _ _ _ _ _
_ _ @ _ _ _
_ @ _ _ x _
_ _ _ _ _ _
我已将新像素的符号和与地标(0,1)对应的地标像素更改为
@

但是我们计算了与我们的新像素对应的原始位置,没有任何参考地标,所以我们真的需要那个地标吗?不,我们没有。我们所要做的就是使用整数除法(或floor或truncate,或您的编程语言所称的任何除法;但不是round)将每个像素坐标除以比例因子。这为我们提供了以下伪代码:

Input:  Original[][] - the original image as a 2-dimensional array
        k - the scalar scaling factor
Output: Resized[][] - the resized image

for all elements (i,j) in Resized
    Resized[i][j] := Original[i div k][j div k]
end

事实上,如果使用浮点除法和truncate/floor,则可以将其扩展为接受非整数比例因子>=1。

是。唯一的问题是如何找到最近的点。我恐怕,有了这个提法,就不那么容易了。尝试用一些伪代码编写算法,社区会检查它。@Gangnus是否适合使用像广度优先搜索这样的算法,检查四个基数和顺序方向中的每一个?算法可以。但它可以用很多方法来表述。对于代码实现来说,最短或最简单的解释并不总是最好的。而且很少有效。
Input:  Original[][] - the original image as a 2-dimensional array
        k - the scalar scaling factor
Output: Resized[][] - the resized image

for all elements (i,j) in Resized
    Resized[i][j] := Original[i div k][j div k]
end