PiCamera and OpenCV imdecode always returning None
Question:
I have a problem using OpenCV on Raspberry PI with the PiCamera. This is my code:
camera = PiCamera()
camera.resolution = ( 320, 240 )
camera.framerate = 60
rawCapture = PiRGBArray( camera, size=( 320, 240 ) )
time.sleep(1)
start = time.time()
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# and occupied/unoccupied text
images = frame.array
# show the frame
cv2.imshow("Frame", images)
cv_img = images.astype(np.uint8)
gray = cv2.imdecode(np.fromstring(cv_img, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
image = cv2.imdecode(np.fromstring(cv_img, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
print("IMAGE: ",image)
print("GRAY: ",gray)
image and gray are always None while images and cv_image contains the right frame. I can’t really understand why it won’t work, Could you please help me?
Thank you in advance.
Answers:
I’ve found picamera.array
to be a reliable way to bridge between picamara
and cv2
. The docs have some examples that aren’t far off from yours.
Once you’re squarely in cv2
territory,
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
is the typical way to do the grayscale conversion. Doing it by way of imdecode
is excessive.
I have a problem using OpenCV on Raspberry PI with the PiCamera. This is my code:
camera = PiCamera()
camera.resolution = ( 320, 240 )
camera.framerate = 60
rawCapture = PiRGBArray( camera, size=( 320, 240 ) )
time.sleep(1)
start = time.time()
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# and occupied/unoccupied text
images = frame.array
# show the frame
cv2.imshow("Frame", images)
cv_img = images.astype(np.uint8)
gray = cv2.imdecode(np.fromstring(cv_img, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
image = cv2.imdecode(np.fromstring(cv_img, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
print("IMAGE: ",image)
print("GRAY: ",gray)
image and gray are always None while images and cv_image contains the right frame. I can’t really understand why it won’t work, Could you please help me?
Thank you in advance.
I’ve found picamera.array
to be a reliable way to bridge between picamara
and cv2
. The docs have some examples that aren’t far off from yours.
Once you’re squarely in cv2
territory,
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
is the typical way to do the grayscale conversion. Doing it by way of imdecode
is excessive.