python - 如何在opencv中使视频源全屏?
问题描述
当我尝试应用精明的边缘检测时,视频的边界也会被检测到,我想知道如何删除它。我正在使用内置网络摄像头获取视频,发现原始帧也有边框。如何让视频全屏?
预期产出
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()
解决方案
似乎您的相机输出带有边框的图像,因此您可以查看设置并尝试禁用它,从而解决问题。
另一种选择是创建一个较小的图像,没有边框。你可以用
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()
推荐阅读
- elm - Elm 的余数运算符是什么
- java - 如何在Java中拆分字符串并比较是否相等?
- javascript - 如何将物体隐藏在立方体孔中?
- javascript - 当表单上有未填写的字段时,如何不将值设置为 Google 工作表?
- c++ - 计数输出不正确/尝试使用开放寻址创建 HashTable/Set 时遇到困难
- asp.net-mvc - 来自控制器的 Asp.net 核心将 html 作为字符串或 json 返回到 ajax 调用
- javascript - useCallback 具有空依赖项,那么作为回调参数传递的是什么?
- python - ValueError:无法将字符串转换为浮点数:'Y'
- php - 使用“to”而不是打印数组中的每个数字
- python - 我如何阅读不和谐消息并将其用作变量?Python