Effacer les filtres
Effacer les filtres

How to import an image in a deployed application for Raspbery Pi

5 vues (au cours des 30 derniers jours)
Hello,
I want to process pictures taken with "Camera Module 3" in a MATLAB application that is deployed to a Raspberry Pi. There might be two options but none is working:
1. Take image with libcamera-still and convert it to a mat file. Then I need to import the file into the deployed application. But I have read about deployed applications: "The function must not have any input or output arguments".
2. Write code in the application that triggers the camera to take an image. The example "Working with Raspberry Pi Camera Board" seems promising. But I could only take pictures with "Camera Module 2" which works with raspicam-stack. But "Camera Module 3" requires the newer libcamera-stack. With the newer model it get a timeout-error because no data is received (R2024a Prerelease).
Any help would be greatly appreciated.
  2 commentaires
Prasanth Sunkara
Prasanth Sunkara le 12 Jan 2024
Try the "webcam" function instead. It is based on generic v4l2 driver unlike the cameraboard function
Matthias Schütz
Matthias Schütz le 17 Jan 2024
Just tried the "webcam" function and got this error message: Timeout while receiving data from remote server.
The "webcam" function works for me only with a setup of raspicam-stack and "Camera Module 2".

Connectez-vous pour commenter.

Réponse acceptée

Abhishek Kumar Singh
Abhishek Kumar Singh le 22 Jan 2024
Modifié(e) : Abhishek Kumar Singh le 22 Jan 2024
Hi Matthias
This situation is quite unique, and I couldn't find any specific guidance on using the 'Camera Module 3' with MATLAB. But I guess there's a alternative way that should work, even though it's a bit roundabout. Before we dive into that, just ensure that you have covered some basic troubleshooting steps like:
  1. Update your Raspberry Pi by make sure the firmware and all packages are up to date.
  2. Test the camera by usign the libcamera-hello command to check that the camera is working fine outside of MATLAB.
  3. Try these troubleshooting steps provided in documentation: https://www.mathworks.com/help/supportpkg/raspberrypiio/ug/issues-with-the-raspberry-pi-camera-board.html
If those steps don't solve the problem, here's what next that can be tried:
Adding on to your first option, lets go with writing a MATLAB Function that's meant to capture an image. Normally, you might use the system command to run libcamera-still, but this command can't be turned into standalone C/C++ code (it's not "code generatable"). MATLAB's tools for turning code into standalone applications don't allow you to run external commands or interact with the operating system in this way.
Instead, you'll need to write your own C function that can run system commands. This C function will use the standard system() function available in C. Here's a simple version of what the C code might look like:
//Add any additional libraries required
#include <stdlib.h>
void executeCommand(const char *cmd) {
int status = system(cmd); //Handle the status as per your choice
}
Below is a sample of header file that you would need:
#ifndef EXECUTECOMMAND_H
#define EXECUTECOMMAND_H
void executeCommand(const char *cmd);
#endif
Then you can proceed with connecting the C Function to MATLAB function using some specific coder commands. Here's a snippet of MATLAB code that shows how to do this:
function captureAndProcessImage()
% Include the header file
coder.cinclude('executeCommand.h');
% Define the command
command = 'libcamera-still -o /home/pi/image.jpg';
% Convert MATLAB string to C string for code generation
commandCStr = coder.opaque('const char *', 'NULL');
commandCStr = coder.ceval('(const char *)', coder.rref(command));
% Call the custom C function to execute the command
coder.ceval('executeCommand', commandCStr);
% Read the captured image into MATLAB(only for testing in MATLAB)
img = imread('/home/pi/image.jpg');
% Process the like convert to grayscale
img = rgb2gray(img);
% you can include code to save in a file format of your choice,
% preferably MAT file
end
After you generate the code from MATLAB, you'll compile it on the Raspberry Pi. This will create a program that can take pictures and process them right on the Pi. If your application wants this on need basis or somewhat delayed processing, you can save it in a predefined file path and and the make the other part of deployed application read from here. The deployed MATLAB application could include a loop that checks for the existence of the MAT file and load it.
Here's few documentation links you might find useful:
  1. Calling custom C code: https://www.mathworks.com/help/coder/ug/call-cc-code-from-matlab-code.html
  2. coder.ceval() documentation: https://www.mathworks.com/help/simulink/slref/coder.ceval.html
And that's it! I hope this helps you get your camera working with MATLAB on the Raspberry Pi. :)
  3 commentaires
Abhishek Kumar Singh
Abhishek Kumar Singh le 22 Jan 2024
You are indeed correct. I used imread just for testing code for accuracy in MATLAB environment. You should remove it before codegen. Additionaly, you can modify/remove structures according to your use case. :)
Matthias Schütz
Matthias Schütz le 27 Jan 2024
Just made it using stb_image library to read the image and store the data as arrays of pixel data.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by