我正在尝试对角直播视频。在cv.line的帮助下,我提到了尺寸,我的目标是显示我绘制的线条下方的视频,并且应该裁剪上方的视频,
作为初学者,我只能使用以下代码绘制一条线:
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
else:
cv2.line(img=frame, pt1=(700,5), pt2=(5, 450), color=(255, 0, 0), thickness=1, lineType=8, shift=0)
vc.release()
cv2.destroyWindow("preview")
输出:
对此的建议将非常有帮助
最佳答案
这是将掩盖直线上方的点的代码。我在这里添加了评论,以便您可以跟踪发生的情况。有更快的方法可以做到这一点,但是我想要一些易于阅读的东西。
import cv2
import matplotlib.pyplot as plt
import numpy as np
path = r"path\to\img"
img = cv2.imread(path)
#plt.imshow(img)
#plt.show()
pt1 = (86, 0) #ensure this point exists within the image
pt2 = (0, 101) #ensure this point exists within the image
cv2.line(img, pt1, pt2, (255, 255, 255))
#plt.imshow(img)
#plt.show()
#slope of line
m = float(pt2[1] - pt1[1])/float(pt2[0] - pt1[0])
c = pt1[1] - m*pt1[0]
#create mask image
mask1 = np.zeros(img.shape, np.uint8)
#for every point in the image
for x in np.arange(0, 87):
for y in np.arange(0, 102):
#test if point exists above the line,
if y > m*x + c:
mask1[y][x] = (255, 255, 255)
#plt.imshow(mask1)
#plt.show()
fin_img = cv2.merge((img[:, :, 0], img[:, :, 1], img[:, :, 2], mask1[:,:, 0]))
#plt.imshow(fin_img)
#plt.show()
cv2.imwrite('output.png', fin_img)
关于python - 显示行的底部图像,并使用Opencv剪切上方的图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55862530/