gpt4 book ai didi

python - 使用opencv在python中以灰度显示相机供稿

转载 作者:行者123 更新时间:2023-12-02 16:47:29 24 4
gpt4 key购买 nike

我一直试图以灰度显示来自笔记本电脑网络摄像头的相机提要,并且已经使用以下代码完成了该操作:

    import cv2
import numpy as np
clicked = False
def onMouse(event, x, y, flags, param):
global clicked
if event == cv2.cv.CV_EVENT_LBUTTONUP:
clicked = True

cv2.namedWindow('image capture', cv2.WINDOW_NORMAL)
cv2.setMouseCallback('image capture', onMouse)
#initialize the camera object with VideoCapture
camera = cv2.VideoCapture(0)
sucess, frame = camera.read()
cv2.imwrite('snapshot.png', frame)
gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE)
while sucess and cv2.waitKey(1) == -1 and not clicked:
cv2.imwrite('snapshot.png', frame)
gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE)
cv2.imshow('image capture', gray)
sucess, frame = camera.read()

cv2.imwrite('snapshot.png', frame)
print 'photo taken press any key to exit'
cv2.waitKey()
cv2.destroyAllWindows()

在这里,我所做的是将帧保存在“snapshot.png”中,然后再次将其重新加载为灰度并显示该灰度图像。是否有任何方法可以直接读取灰度图像而不是经历所有这些混乱情况。提前致谢。

最佳答案

哇,真是一团糟;)

您只想要:

gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )

关于python - 使用opencv在python中以灰度显示相机供稿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com