Home > Net >  Basic algorithm to resize a 2D-matrix
Basic algorithm to resize a 2D-matrix

Time:03-21

Let's say I have a black and white image of a "J", represented as a binary matrix, like this (white = 0, black = 1). If we let each cell in the matrix be a pixel, its size would be 6 x 10.

0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
1 0 0 0 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0

What I want to do is take this image and resize it by factor 10, that is, make it 10 times its current size, to 60 x 100. ( Of course there would be some interpolation need to happen, but it doesn't need to be overly sophisticated.)

I don't really get yet how this is done in practice. Is there a transformation matrix that can handle this? I found an article that talk about using a resize matrix (in my case 2 x 2 I guess). Do I need to apply that then? To every vector (every entry) in the matrix? And how can I then handle the interpolation to scale up the black pixels?

I was wondering if someone could provide either a simple example how this works using a scaling matrix or maybe provide me with some clues what to search for. My searches haven't been successful in order to find a simple/stripped down example.

CodePudding user response:

I'll use a smaller (5x4) version of your example:

. . T .
. . I .
. . I .
L . I .
. V . .

and scale it up by a factor of 3, multiplying by a 15x5 matrix on the left to scale vertically, and a 4x12 matrix on the right to scale horizontally:

1 0 0 0 0     . . T .     1 1 1 0 0 0 0 0 0 0 0 0     . . . . . . T T T . . .
1 0 0 0 0  X  . . I .  X  0 0 0 1 1 1 0 0 0 0 0 0  =  . . . . . . T T T . . .
1 0 0 0 0     . . I .     0 0 0 0 0 0 1 1 1 0 0 0     . . . . . . T T T . . .
0 1 0 0 0     L . I .     0 0 0 0 0 0 0 0 0 1 1 1     . . . . . . I I I . . .
0 1 0 0 0     . V . .                                 . . . . . . I I I . . .
0 1 0 0 0                                             . . . . . . I I I . . .
0 0 1 0 0                                             . . . . . . I I I . . .
0 0 1 0 0                                             . . . . . . I I I . . .
0 0 1 0 0                                             . . . . . . I I I . . .
0 0 0 1 0                                             L L L . . . I I I . . .
0 0 0 1 0                                             L L L . . . I I I . . .
0 0 0 1 0                                             L L L . . . I I I . . .
0 0 0 0 1                                             . . . V V V . . . . . .
0 0 0 0 1                                             . . . V V V . . . . . .
0 0 0 0 1                                             . . . V V V . . . . . .

I trust the pattern is clear.

  • Related