python中有没有可以在原图上画线的模块?
Is there a module in python that can draw lines on an original image?
如标题所述,我希望在附录中可视化我的结果。
其中红线是由给定特定角度的程序绘制的。 python中是否有提供此类功能的模块?我该怎么做?
作为评论,链接的 awnser Plot over an image background in python 展示了如何使用 matplotlib 在现有图像上绘制简单的几何图形。如果你想要更复杂的东西(也可能随着时间的推移而改变),matplotlib 可以工作,但可能不会按照你想要的方式执行,并且包含很多你想要覆盖的所有元素的代码。
相反,您应该将叠加层作为图像提供并使用 opencv
旋转它并叠加矩阵。
我给你举个简单的例子:
这里有背景图片:
我们要覆盖以下圆圈:
还有这个十字,但是旋转了 36 度:
以下代码将提供所需的结果:
import cv2
import numpy as np
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
def overlay_images(img1, img2):
if img1.shape[2] != 4 or img2.shape[2] != 4:
print('png needs Alphachannel!')
return
alpha_img1 = img1[:,:,3] / 255.0
alpha_img2 = img2[:,:,3] / 255.0
for color in range(0, 3):
img1[:,:,color] = alpha_img2 * img2[:,:,color] + \
alpha_img1 * img1[:,:,color] * (1 - alpha_img2)
img1[:,:,3] = (1 - (1 - alpha_img2) * (1 - alpha_img1)) * 255
return img1
img_bg = cv2.imread('img1.png',cv2.IMREAD_UNCHANGED)
img_circle = cv2.imread('img2.png',cv2.IMREAD_UNCHANGED)
img_cross = cv2.imread('img3.png',cv2.IMREAD_UNCHANGED)
img_rotate_36 = rotate_image(img_cross, 36)
img1_2 = overlay_images(img_bg, img_circle)
img1_2_3 = overlay_images(img1_2, img_rotate_36)
cv2.imwrite('img_rotated.png', img1_2_3)
cv2.imshow('Rotatet Image', img1_2_3)
cv2.waitKey(0)
cv2.destroyAllWindows()
有趣的部分是旋转和叠加。我将这种情况用于叠加此解决方案 because the cv2.addWeighted
function seems to have problems with alpha channel. The algorithm used is called alpha composition. The rotation is based on 并且是基于旋转矩阵的简单仿射变换。
我在上面的代码中将这两个应用程序都放在一个函数中。
如标题所述,我希望在附录中可视化我的结果。
其中红线是由给定特定角度的程序绘制的。 python中是否有提供此类功能的模块?我该怎么做?
作为评论,链接的 awnser Plot over an image background in python 展示了如何使用 matplotlib 在现有图像上绘制简单的几何图形。如果你想要更复杂的东西(也可能随着时间的推移而改变),matplotlib 可以工作,但可能不会按照你想要的方式执行,并且包含很多你想要覆盖的所有元素的代码。
相反,您应该将叠加层作为图像提供并使用 opencv
旋转它并叠加矩阵。
我给你举个简单的例子:
这里有背景图片:
我们要覆盖以下圆圈:
还有这个十字,但是旋转了 36 度:
以下代码将提供所需的结果:
import cv2
import numpy as np
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
def overlay_images(img1, img2):
if img1.shape[2] != 4 or img2.shape[2] != 4:
print('png needs Alphachannel!')
return
alpha_img1 = img1[:,:,3] / 255.0
alpha_img2 = img2[:,:,3] / 255.0
for color in range(0, 3):
img1[:,:,color] = alpha_img2 * img2[:,:,color] + \
alpha_img1 * img1[:,:,color] * (1 - alpha_img2)
img1[:,:,3] = (1 - (1 - alpha_img2) * (1 - alpha_img1)) * 255
return img1
img_bg = cv2.imread('img1.png',cv2.IMREAD_UNCHANGED)
img_circle = cv2.imread('img2.png',cv2.IMREAD_UNCHANGED)
img_cross = cv2.imread('img3.png',cv2.IMREAD_UNCHANGED)
img_rotate_36 = rotate_image(img_cross, 36)
img1_2 = overlay_images(img_bg, img_circle)
img1_2_3 = overlay_images(img1_2, img_rotate_36)
cv2.imwrite('img_rotated.png', img1_2_3)
cv2.imshow('Rotatet Image', img1_2_3)
cv2.waitKey(0)
cv2.destroyAllWindows()
有趣的部分是旋转和叠加。我将这种情况用于叠加此解决方案 because the cv2.addWeighted
function seems to have problems with alpha channel. The algorithm used is called alpha composition. The rotation is based on 并且是基于旋转矩阵的简单仿射变换。
我在上面的代码中将这两个应用程序都放在一个函数中。