使用python更改图像的形状以采用封闭图像的形状

问题描述 投票:0回答:1

我是python的新手。我想对第一张图像进行变形,使其充满第二张图像的闭合路径。我有两个图像。一个是源图像,另一个是封闭图像。是否可以在任何类型的封闭路径中填充图像。您能否建议使用python库,或者如果您可以共享一些代码,那就太好了。我还尝试使用角点检测算法并使它们与地图功能保持一致,但我做不到。

Two images and the end result which I'm expecting to get

python opencv image-processing polynomials distortion
1个回答
0
投票

这里是使用Python / OpenCV使用遮罩将一个图像叠加到另一个图像的示例。

t恤图片:

enter image description here

图案图像:

enter image description here

t恤面具图像:

enter image description here

 - Read the 3 images and get shapes
 - Convert the mask to gray and binarize
 - Resize the pattern so that its smallest dimension is the size of the largest dimension of the tshirt image
 - Crop it to exactly the same size as the tshirt image
 - Apply the mask to the pattern image
 - Apply the inverse mask to the tshirt image
 - Add the two together
 - Save the results

import cv2
import numpy as np

# read shirt image and get its max dimension
img = cv2.imread('tshirt.jpg')
hh, ww = img.shape[:2]
maxwh = max(hh, ww)

# read pattern image and get its size and minimum dimension
pattern = cv2.imread('tshirt_pattern.jpg')
ht, wd = pattern.shape[:2]
minwh = min(ht,wd)

# read shirt mask image
maskimg = cv2.imread('tshirt_mask.png')

# convert mask to gray and binarize
maskimg = cv2.cvtColor(maskimg, cv2.COLOR_BGR2GRAY)
maskimg = cv2.threshold(maskimg, 128, 255, cv2.THRESH_BINARY)[1]

# resize pattern so minimum dimension is size of largest dimension of tshirt image
scale = maxwh/minwh
pattern_enlarge = cv2.resize(pattern, dsize=(0,0), fx=scale, fy=scale)

# limit resized pattern to size of tshirt
pattern_enlarge = pattern_enlarge[0:hh, 0:ww]

# do masked overlay
pattern_masked = cv2.bitwise_and(pattern_enlarge, pattern_enlarge, mask=maskimg)
img_masked = cv2.bitwise_and(img, img, mask=(255-maskimg))
result = cv2.add(img_masked, pattern_masked)

cv2.imshow('image', img)
cv2.imshow('pattern', pattern)
cv2.imshow('mask', maskimg)
cv2.imshow('masked pattern', pattern_masked)
cv2.imshow('masked image', img_masked)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save results
cv2.imwrite('tshirt_masked.jpg', img_masked)
cv2.imwrite('pattern_masked.jpg', pattern_masked)
cv2.imwrite('tshirt_pattern_overlay.jpg', result)

遮罩的图案图片:

enter image description here

蒙面的T恤图片:

enter image description here

结果图像:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.