0

I have the a list of images (each of these image is a separate file), let's say they are some jigsaw puzzle pieces and, for each of them, I know it's position (x,y) and rotation in the complete puzzle.

How can I show the complete puzzle by stitching each of these pieces together in a single image (given that i know where to put each of them)?

I don't know if this is important but the pieces are not of regular shape (e.g. they are not squares), and they are all of different sizes

EDIT:

For the moments it seems to be working without the rotation but there is another problem, the pieces seems to not have a transparent background but rather a black one. I have loaded them with opencv2 in the following way:

import glob
folder = './img/2/frag_eroded/'
frags = []
files = glob.glob(folder+"/*.png")
for file in files:
    image = cv2.imread(file, cv2.IMREAD_UNCHANGED)
    image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
    frags.append(image)

Example of resulting image, you can kinda see the the squares around each piece and see how the pieces overlap with their "background" that should be transparent rather then black enter image description here

1 Answers1

1

This depends on how you want to handle it when there's an overlapping transparent area.

Suppose all pixels are either transparent or opaque, and

Suppose each image has RGBA (4-channels including alpha), then you can set all RGB values to zero whenever the pixel is transparent.

Then proceed to add the smaller images to a bigger canvas (initialized to be all zeros RGB). The canvas can either have an alpha layer or not, depending on your preference.

Beware the canvas is big enough to contain all of them. So the first step here would be to make a large enough matrix / opencv image.

How to add images: https://stackoverflow.com/a/68878529/19042045

Neo
  • 124
  • 5
  • I have tried it without the rotation first and it seems to work only with a certain number of rotation, after which i get the error: "could not broadcast input array from shape (110,110,4) into shape (80,110,4)". I will add my current code in an edit of my post – Mario Turco Jul 20 '22 at 16:57
  • 1
    can you do `assert y+h < canvas_y` and `assert x+w < canvas_x`? – Neo Jul 20 '22 at 17:12
  • Yes that was the problem, since i don't rotate the images as they shoudl be rotated, some of the pieces exedes the canvas border, so i just made the canvas a little big bigger and it does work but i do have another problem. I have read the images as png with cv2 and converted them from abgr to rgba but they appear to have a black background as if cv2 removed the transparency layer. I will edit the post with informations regarding this – Mario Turco Jul 20 '22 at 17:15
  • as long as the alpha layer has correct value, the only thing that matters is visualization. Whether it's black or white or grid depends on visualization software. e.g. matplotlib, eog – Neo Jul 20 '22 at 17:16
  • Look at the post for more details please And thank you for you help so far – Mario Turco Jul 20 '22 at 17:24
  • 1
    when you use the equal sign, it directly replaces the pixel. so if i have some black/white margins on the jigsaw pieces it will affect the existing pixels. use "add" instead of "equal". and make sure to set the margin's RGB value to zero before the adds. – Neo Jul 20 '22 at 17:28
  • The link I provided before was not fit for your purpose, just for the inspiration of it. – Neo Jul 20 '22 at 17:29
  • Thanks, the = was the problem. Instead if i do` canvas[(coords)]+=new_piece` it does work. I will now try with the rotation! – Mario Turco Jul 20 '22 at 17:36