首页 > 解决方案 > 如何在opencv中使视频源全屏?

问题描述

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

原始图像 Canny Edge 输出

预期产出

无国界 无国界

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()

标签: pythonopencv

解决方案


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

另一种选择是创建一个较小的图像,没有边框。你可以用
sub_image = image[y1:y2,x1:x2]
For your images x1 is 0 and x2 is the image width 来做到这一点。y1 是从顶部算起的第一个非黑色像素,y2 是从底部算起的第一个非黑色像素。

我在下面添加了代码来查找 Y1 和 Y2 值并将它们打印到屏幕上。它也显示了结果。一旦找到正确的 Y 值,您只需将创建 sub_image 的行添加到您的代码中。

结果: 在此处输入图像描述

代码:

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()

推荐阅读