justadudewhohacks / justadudewhohacks/opencv4nodejs
Imencode - OpenCV Error: (!image.empty()) in imencode, in file... RPI4
- Dominant language
- C++
- Stars
- 5.1k
- Forks
- 826
- PR merge metrics
- No merged PRs in 30d
Description
Hello. I'm trying to get a video stream behind the cameras on the RPI 4, but I'm getting the following error:

Using the libcamera-hello command, the image is displayed. I suspect there may be an error in the camera's image format.

Platform: Raspbian Bullseye 11 (32-bit)
OpenCV: v4.5.5
NodeJS: v16
My code:
const cv2 = require('@u4/opencv4nodejs');
const path = require('path');
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
app.set('view engine', 'ejs');
const FPS = 30;
const Vcap = new cv2.VideoCapture(0);
Vcap.set(cv2.CAP_PROP_FRAME_WIDTH, 300);
Vcap.set(cv2.CAP_PROP_FRAME_HEIGHT, 300);
app.get('/', async(req, res, next) => {
res.render('index');
});
let frame;
setInterval(() => {
frame = Vcap.read()
const image = cv2.imencode('.jpg', frame).toString('base64')
io.emit('image', image);
}, 1000 / FPS);
server.listen(3030, () => console.log('open up your browser'));
Contributor guide
Research direction
Start with the VideoCapture setup, Vcap.read(), and the cv2.imencode('.jpg', frame) call shown in the issue. Compare the captured frame behavior with the working libcamera-hello command and verify whether read() returns a usable image before encoding. Done means identifying the cause of the empty image and documenting or confirming a reproducible fix for the RPI4 setup.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, linux, node.js
- Domain
- computer-vision
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100