Algorithm for dividing many objects into a certain number of groups?

For example, let's say I have a 2D array of pixels (in other words, an image), and I want to organize them into groups so that the number of groups is ideally supplemented by a certain number (for example, the total number of elements in another 2D pixel matrix). At the moment I am trying to use a combination of coefficients and pixels, but this does not work for anything other than an ideal integer ratio (so 1: 2, 1: 3, 1: 4, etc.). When this fails, it simply scales it to an integer less than, for example, a 1: 2,93 scale will use a 1: 2 scale with part of the cropped image. I would rather not do this, so what algorithms can I use that do not fall into Matrix Multipication? I remember that I saw something similar to what I described at the beginning, but I can not find it. Is this an NP type problem?

For example, let's say I have a 12-by-12-pixel image, and I want to split it into exactly 64 sub-images of nth size. Through analysis it was possible to see that I can divide it into 8 sub-images 2 by 2 and 56 sub-images 2 by 1 to get the exact number of sub-images. In other words, I would get 8 + 56 = 64 sub-images using all 4 (8) +56 (2) = 144 pixels.

Similarly, if I had a 13 by 13 pixel image, and I wanted 81 sub-images of size nth, I would need to split it into 4 sub-images of 2 by 2, 76 2 -by- 1 sub-image and 1 1-on-1 sub-image to get the exact number of sub-images needed. In other words, 4 (4) +76 (2) + 1 = 169 and 4 + 76 + 1 = 81.

Another example: if I wanted to split the same image 13 into 13 images into 36 sub-images of nth size, I would need 14 4-by-2 sub-images, 7 2-by-2 sub-images , 14 sub-images are 2 in 1 and 1 sub-image is 1 in 1. In other words, 8 (13) +4 (10) +2 (12) + 1 = 169 and 13 + 10 + 12 + 1 = 36.

Of course, the image should not be square, and neither the number of sub-images, nor one of them should be simple. In addition, the number of sub-images must be less than the number of pixels in the image. I would probably want to stick to two degrees for the width and height of the sub-images for the convenience of translating one larger sub-image into several auxiliary images, but if I could find an algorithm that did not do this, be better. This is basically what I'm trying to find for an algorithm.

+2
2

, - n. , :

  • w * h
  • n - x * y

,

R = { (x, y) | x in [1..w], y in [1..h], x * y == (w * h) / n }

(x, y) , x * y (w * h) / n, / - . , , , x * y , .. x + y.

:

  • 12 x 12 64 -, R = {(1,2),(2,1)}, 64 1 x 2 - 64 2 x 1 -

  • 13 x 13 81 -, het R = {(1,2),(2,1)}, 64 1 x 2 - 64 2 x 1 -

  • 13 x 13 36 -, R = {(1,4),(2,2),(4,1)}, 36 2 x 2 ( )

, , .

- , , ,

+1

, , - . .

0

All Articles