如何在opencv中将视频供稿全屏显示?

时间:2019-01-29 18:57:56

标签: python opencv

当我尝试应用Canny边缘检测时,视频的边界也被检测到,我想知道如何删除它。我正在使用内置网络摄像头来获取视频,并发现原始框架也有边框。如何使视频全屏显示?

Original Image Canny Edge output

预期产量

Without Border Without Border

import cv2

windowName = "Live"
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(windowName,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cam = cv2.VideoCapture(0)

while True:  
    _,img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 50, 150)
    cv2.imshow(windowName, edges)
    key = cv2.waitKey(30)
    if key == 27:
        break

cam.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

您的相机似乎输出带边框的图像,因此您可以查看设置并尝试禁用它,从而解决问题。

另一种选择是创建一个没有边框的较小图像。你可以这样
sub_image = image[y1:y2,x1:x2]
对于您的图像,x1为0,x2为图像宽度。 y1是从顶部开始的第一个非黑色像素,y2是从底部开始的第一个非黑色像素。

我在下面添加了代码,这些代码可找到Y1和Y2值并将其打印到屏幕上。它还显示结果。找到正确的Y值后,您只需在代码中添加创建sub_image的行。

结果: enter image description here

代码:

import numpy as np 
import cv2
# load image
img = cv2.imread("image.png",0)
# sum each row of the image
sumOfRows = np.sum(img, axis=1)

# Find the first and last row that has a sum value greater than zero, 
# which means its not all black. Store the found values in variables
for i in range(len(sumOfRows)):
    if sumOfRows[i] > 0:
        y1 = i
        print('First row: ' + str(i))
        break

for i in range(len(sumOfRows)-1,-1,-1):
    if sumOfRows[i] > 0:
        y2 = i
        print('Last row: ' + str(i))
        break

# create a new image based on the found values
roi = img[y1:y2,0:img.shape[1]]

#show image
cv2.imshow("Result", roi)
cv2.imshow("Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()