使用 Python 改变图像的形状以采用封闭图像的形状
Altering the shape of an image to take the shape of an enclosed image with Python
我是 Python 的新手。我想扭曲第一个图像,使其填充第二个图像的闭合路径。我有两个图像。一个是源图像,另一个是封闭图像。是否可以在任何类型的封闭路径中填充图像。您能否建议使用 Python 个库,或者如果您可以共享一些代码就太好了。我也曾尝试使用角点检测算法并使它们坚持使用地图功能,但我做不到。
两张图片和我期望得到的最终结果:
这里是使用 Python/OpenCV.
的蒙版将一张图片叠加到另一张图片上的示例
T 恤图片:
图案图片:
T 恤面具图片:
- 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)
蒙版图案图像:
蒙面 T 恤图片:
结果图片:
我是 Python 的新手。我想扭曲第一个图像,使其填充第二个图像的闭合路径。我有两个图像。一个是源图像,另一个是封闭图像。是否可以在任何类型的封闭路径中填充图像。您能否建议使用 Python 个库,或者如果您可以共享一些代码就太好了。我也曾尝试使用角点检测算法并使它们坚持使用地图功能,但我做不到。
两张图片和我期望得到的最终结果:
这里是使用 Python/OpenCV.
的蒙版将一张图片叠加到另一张图片上的示例T 恤图片:
图案图片:
T 恤面具图片:
- 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)
蒙版图案图像:
蒙面 T 恤图片:
结果图片: