简体   繁体   English

如何根据另一个参考点调整图像大小

[英]How to resize an image according to a reference point in another

I currently have two images (im1 and im2) with pixel dimensions of (1725, 1580). 我目前有两个像素尺寸为(1725,1580)的图像(im1和im2)。 im2 however possesses a large border around it that i had to create to ensure that the images were of the same size. 然而,im2拥有一个大的边框,我必须创建它以确保图像大小相同。 im2 was originally (1152, 864). im2最初是(1152,864)。

As such. 因此。 when i overlay im2 ontop of im1 using PIL.Image.blend , im2 appears overlayed onto im1, but is a lot smaller. 当我使用PIL.Image.blend im2叠加在im1上时,im2看起来重叠在im1上,但是要小得多。 I have 2 distinct reference points on the images i think i could use (present on im1 and im2) to rescale im2 (zoom it in somehow?) to overlay im2 ontop of im1. 我有两个不同的参考点,我认为我可以使用(存在于im1和im2上)来重新缩放im2(以某种方式缩放它?)以重叠im2 on im1。

My issue is that i have been looking through various python modules (PIL, scipy, matplotlib etc) but cant seem to really be getting anywhere or find a solution with which i could approach this issue. 我的问题是我一直在寻找各种python模块(PIL,scipy,matplotlib等),但似乎无法真正到达任何地方或找到我可以解决这个问题的解决方案。

I have 2 reference points i think i could use (present on im1 and im2) to rescale im2 (zoom it in somehow?) to overlay im2 ontop of im1. 我有2个参考点,我认为我可以使用(存在于im1和im2上)来重新缩放im2(以某种方式缩放它?)以叠加im2 on im1。

i have looked at various modules but cant to seem to find anything that might work (scipy, PIL, matplotlib) 我看过各种模块,但似乎无法找到任何可行的东西(scipy,PIL,matplotlib)

#im1 https://i.imgur.com/dF8uyPw.jpg
#im2 https://i.imgur.com/o4RAhOQ.png
#im2_resized https://i.imgur.com/jfWz1LE.png

im1 = Image.open("pit5Film/Pit_5_5mm_inf.tif")
im2 = Image.open("pit5Overlay/overlay_132.png")

old_size = im2.size
new_size = im1.size
im2_resized = Image.new("RGB", new_size) 
im2_resized.paste(im2,((round((new_size[0]-old_size[0])/2)),round(((new_size[1]-old_size[1])/2))))

Image.blend(im1,im2_resized,0.2)

I think you are trying to do an "affine distortion" . 我想你正试图做一个“仿射失真” I can maybe work out how to do it in OpenCV or PIL , but for the minute, here's what I did with ImageMagick . 我可以在OpenCVPIL中找出如何做到这一点,但是就这一刻而言,这就是我对ImageMagick的所作所为。

First, I located the centre of the registration hole (?) on both the left and right side of the first image. 首先,我在第一张图像的左侧和右侧找到了定位孔(?)的中心。 I got these coordinates: 我有这些坐标:

422,775    # left hole centre 1st picture
1246,799   # right hole centre 1st picture

在此输入图像描述

Then I found these same features in the second picture at: 然后我在第二张图片中找到了这些相同的功能:

514,426    # left hole centre 2nd picture
668,426    # right hole centre 2nd picture

在此输入图像描述

Then I ran this in Terminal to do the 2-point affine transformation: 然后我在终端运行这个进行2点仿射变换:

convert imageA.jpg -virtual-pixel white                        \
    -distort affine '422,775 514,426 1246,799 668,426' +repage \
    imageB.png -compose overlay -composite result.jpg

在此输入图像描述

There is loads of great information from Anthony Thyssen here if you fancy a read. 有一个从安东尼蒂森的大量信息加载在这里 ,如果你喜欢读。

This is how to do it in Python Wand, which is based upon Imagemagick. 这是如何在Python Wand中实现的,它基于Imagemagick。 I use Mark Setchell's images and the Python Wand equivalent command. 我使用Mark Setchell的图像和Python Wand等效命令。 The distort command needs Imagemagick 7, according to the documentation. 根据文档,distort命令需要Imagemagick 7。 Using Python Wand 0.5.5, the current version. 使用Python Wand 0.5.5,当前版本。

Script: 脚本:

#!/bin/python3.7

from wand.image import Image
from wand.color import Color
from wand.display import display

with Image(filename='imageA.jpg') as Aimg:
    with Image(filename='imageB.jpg') as Bimg:
        Aimg.virtual_pixel = 'background'
        Aimg.background_color = Color('white')
        arguments = (422, 775, 514, 426, 1246, 799, 668, 426)
        Aimg.distort('affine', arguments)
        Aimg.composite(Bimg, 0, 0, 'overlay')
        Aimg.save(filename='image_BoverlayA_composite.png')
        display(Aimg)


Calling Command: 呼叫命令:

python3.7 wand_affine_overlay.py


Result: 结果: 在此输入图像描述

ADDITION: 加成:

If you want to trim the image to its minimum bounding box, then add trim to the command as follows, where the trim value is in the range 0 to quantum range. 如果要将图像修剪到其最小边界框,则按如下方式将修剪添加到命令,其中修剪值在0到量程范围内。

#!/bin/python3.7

from wand.image import Image
from wand.color import Color
from wand.display import display

with Image(filename='imageA.jpg') as Aimg:
    with Image(filename='imageB.jpg') as Bimg:
        Aimg.virtual_pixel = 'background'
        Aimg.background_color = Color('white')
        arguments = (422, 775, 514, 426, 1246, 799, 668, 426)
        Aimg.distort('affine', arguments)
        Aimg.composite(Bimg, 0, 0, 'overlay')
        Aimg.trim(fuzz=10000)
        Aimg.save(filename='image_BoverlayA_composite.png')
        display(Aimg)


在此输入图像描述

batter way for resizing an image using OpenCV 使用OpenCV调整图像大小的方法

dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

for blend two images using OpenCV 1: load both image 使用OpenCV 1混合两个图像:加载两个图像

src1 = cv.imread(cv.samples.findFile('img1.jpg'))
src2 = cv.imread(cv.samples.findFile('img2.jpg'))

2: blend both images (alpha is in between 0 to 1 any float) 2:混合两个图像(alpha在0到1之间任意浮动)

beta = (1.0 - alpha)
dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)

3: for displaying result 3:显示结果

cv.imshow('dst', dst)
cv.waitKey(0)

if you wont to resize an image according to a reference point consider this awsome blog by PYIMAGESEARCH : PYIMAGESEARCH 4 POINT 如果您不想根据参考点重新调整图像大小,请考虑PYIMAGESEARCH这个非常棒的博客: PYIMAGESEARCH 4 POINT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM