How to resize an image according to a reference point in another

人盡茶涼 提交于 2021-02-09 10:32:35

问题


I currently have two images (im1 and im2) with pixel dimensions of (1725, 1580). im2 however possesses a large border around it that i had to create to ensure that the images were of the same size. im2 was originally (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. 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.

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.

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.

i have looked at various modules but cant to seem to find anything that might work (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)

回答1:


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.

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:

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.




回答2:


This is how to do it in Python Wand, which is based upon Imagemagick. I use Mark Setchell's images and the Python Wand equivalent command. The distort command needs Imagemagick 7, according to the documentation. Using Python Wand 0.5.5, the current version.

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.

#!/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)





回答3:


batter way for resizing an image using 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

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)

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

3: for displaying result

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



来源:https://stackoverflow.com/questions/57055375/how-to-resize-an-image-according-to-a-reference-point-in-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!