This is my test frame, I am trying to find which map it is by pasting every single map on top and checking if the images match.
It is cut perfectly and the image I paste on top is also cut perfectly but I am getting this result:
这是我的测试框,我试图通过将每一张地图粘贴在上面并检查图像是否匹配来找出它是哪一张地图。它被完美地剪切了,我粘贴在上面的图像也被完美地剪切了,但我得到了这样的结果:
Why am I getting this behavior and how to fix it? This is my code:
为什么我会有这种行为,以及如何纠正它?这是我的代码:
bottom_image = Image.open("test_frames/test_frame_20.png").convert("RGBA")
top_image = Image.open("../map_images/split.png").convert("RGBA")
lower_hsv = np.array([0, 0, 122])
upper_hsv = np.array([174, 53, 130])
top_image = top_image.resize((bottom_image.size[0], bottom_image.size[1]), Image.ANTIALIAS)
img_array = np.array(top_image)
img_array[..., :3] = [255, 0, 0]
best_result = 9**999
rotation_degrees = 0
red_image = Image.fromarray(img_array)
for i in range(1, 5):
red_image_copy = red_image.copy().rotate(i*90)
result_image = Image.alpha_composite(bottom_image.convert("RGBA"), red_image_copy)
numpy_array = np.array(result_image)
opencv_image = cv2.cvtColor(numpy_array, cv2.COLOR_RGB2BGR)
hsv_image = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_image, lower_hsv, upper_hsv)
result = np.sum(mask)
if result < best_result:
best_result = result
rotation_degrees = i*90
print(rotation_degrees)
result_image = Image.alpha_composite(bottom_image, red_image.rotate(rotation_degrees))
result_image.save("result_image.png")
Just want to repeat that both images are cut perfectly and theres no space to the left and right on the image I paste on top.
我只想重复一遍,两张图片都被完美地剪切了,在我粘贴到上面的图片上,左右都没有空格。
Links to download both images i use:
下载我使用的两张图片的链接:
map: https://ibb.co/bH8K5Ty
test_frame: https://ibb.co/fMRqN3s
地图:https://ibb.co/bH8K5Ty测试帧:https://ibb.co/fMRqN3s
更多回答
minimal reproducible example required. please present pictures that are actually the same size, and oriented properly. your ibb.co links show a 354x334 image and an 894x952 image.
所需的最小可重现示例。请提供大小相同、方向正确的图片。您的ibb.co链接显示了一张354x334图像和一张894x952图像。
@Christoph Rackwitz the code resizes it to the same size as the frame image and rotates it in the for cycle. That is also my question. It resizes it to the same size but its not indentical. I cannot just resize the top image because sometime the frames will have different sizes of the map and I need it to be done dynamically
@Christoph Rackwitz代码将其大小调整为与帧图像相同的大小,并在for循环中旋转它。这也是我的问题。它将其大小调整为相同的大小,但不是无关紧要的。我不能仅仅调整顶部图像的大小,因为有时框架会有不同大小的地图,我需要动态完成它
优秀答案推荐
Your input pictures aren't the same size. One is significantly larger.
您输入的图片大小不一样。一个明显更大。
They are almost square, but not really. One is 894x952 (tall), the other is 354x334 (wide).
它们几乎是正方形,但并不是真正的正方形。一个是894x952(高),另一个是354x334(宽)。
One of your input pictures is rotated by 90 degrees relative to the other.
您的一张输入图片相对于另一张图片旋转了90度。
You swapped, or rather failed to swap, the width and height before the resize operation.
在调整大小操作之前,您交换了宽度和高度,或者更确切地说,交换失败。
The resize operation then squashed the picture in one direction and stretched it in the other.
然后,调整大小操作在一个方向上挤压图片,在另一个方向上拉伸图片。
Please step through your code with a debugger and also inspect the results of every operation.
请使用调试器单步执行代码,并检查每个操作的结果。
更多回答
The only line in code where I resize is this: top_image = top_image.resize((bottom_image.size[0], bottom_image.size[1]), Image.ANTIALIAS)
I replaced it with top_image.resize(bottom_image.size, Image.ANTIALIAS)
but still same result. If I swap the width and height I will just get an Error because of Image.alpha_composite
代码中我调整大小的唯一行是:top_Image=top_Image.resize((Bottom_Image.Size[0],Bottom_Image.Size[1]),Image.ANTIALIAS)我用top_Image.resize(Bottom_Image.Size,Image.ANTIALIAS)替换了它,但结果仍然是一样的。如果我交换宽度和高度,我只会得到一个错误,因为Image.Alpha_Complex
I have edited my answer a few times. please re-read it. then investigate your code. it's not an issue with opencv or PIL, it's an issue of logic. please debug your code. that's not a request. that is a requirement of questions on SO. How to Ask, minimal reproducible example.
我已经修改了我的答案几次。请重读一遍。然后研究您的代码。这不是OpenCV或PIL的问题,而是逻辑问题。请调试您的代码。这不是请求。这是关于SO的问题的一个要求。怎么问,最小可重现的例子。
The problem was that since the map is not a square, when rotated it would actually leave some space on top and bottom. Thus the only solution would be to have 4 images of each rotations of the map so they can be cut perfectly instead of rotating with code.
问题是,由于地图不是正方形,当旋转时,它实际上会在顶部和底部留下一些空间。因此,唯一的解决方案是为地图的每一次旋转提供4幅图像,这样它们就可以被完美地剪切,而不是用代码旋转。
我是一名优秀的程序员,十分优秀!