opencv破解
Opencv raspicam
嗨,我想 运行 这段代码用 OpenCV 在 raspberry pi B 上使用 raspicam 检测汽车,但遇到错误。
import numpy as np
import cv2
car_cascade = cv2.CascadeClassifier('cars3.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
运行宁代码后returns
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/installopencv/opencv-3.1.0/modules/imgproc/src/color.cpp, line 8000
Traceback (most recent call last):
File "test.py", line 14, in <module>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: /home/pi/installopencv/opencv-3.1.0/modules/imgproc/src/color.cpp:8000: error: (-215) scn == 3 || scn == 4 in function cvtColor
发生错误是因为我使用的是 raspicam 而 "cap = cv2.VideoCapture(0)" 仅适用于网络摄像头吗?我尝试启用 V4L2 模块,但效果不佳
如果您想使用 Raspberry PI 相机模块,请使用 picamera 模块获取帧,而不是 OpenCV'2 videoCapture 模块。特别是你想安装支持数组的模块:
pip install "picamera[array]"
这将使您可以轻松地将帧传递给 OpenCV。
有一个关于如何从头开始的非常好的教程 here
这是它的要点:
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
对于您的情况,您可能希望将格式从 "rgb"
更改为 "yuv"
。
这样,您可以直接提取 y
(luminosity) 通道,这将是您的灰度方法。希望您的速度会有所提高,而不必进行色彩空间转换(从 BGR 到灰度)和从 CSI(而不是 USB)获取帧。
嗨,我想 运行 这段代码用 OpenCV 在 raspberry pi B 上使用 raspicam 检测汽车,但遇到错误。
import numpy as np
import cv2
car_cascade = cv2.CascadeClassifier('cars3.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
运行宁代码后returns
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/installopencv/opencv-3.1.0/modules/imgproc/src/color.cpp, line 8000
Traceback (most recent call last):
File "test.py", line 14, in <module>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: /home/pi/installopencv/opencv-3.1.0/modules/imgproc/src/color.cpp:8000: error: (-215) scn == 3 || scn == 4 in function cvtColor
发生错误是因为我使用的是 raspicam 而 "cap = cv2.VideoCapture(0)" 仅适用于网络摄像头吗?我尝试启用 V4L2 模块,但效果不佳
如果您想使用 Raspberry PI 相机模块,请使用 picamera 模块获取帧,而不是 OpenCV'2 videoCapture 模块。特别是你想安装支持数组的模块:
pip install "picamera[array]"
这将使您可以轻松地将帧传递给 OpenCV。 有一个关于如何从头开始的非常好的教程 here 这是它的要点:
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
对于您的情况,您可能希望将格式从 "rgb"
更改为 "yuv"
。
这样,您可以直接提取 y
(luminosity) 通道,这将是您的灰度方法。希望您的速度会有所提高,而不必进行色彩空间转换(从 BGR 到灰度)和从 CSI(而不是 USB)获取帧。