Run matlab code in raspberry pi

37 views (last 30 days)
vamshi krishna
vamshi krishna on 18 Nov 2015
Commented: Walter Roberson on 24 May 2022
Hey guys,
Since I'm very new to matlab I don't know much information about it. I wrote a code on detecting object using background subtraction method now i have a problem to run that code in raspberry pi.
Here is the code,
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 50);
videoReader = vision.VideoFileReader(vamshi.avi);
for i = 1:150
frame = step(videoReader); % read the next video frame
foreground = step(foregroundDetector, frame);
end
figure; imshow(frame); title('Video Frame');
figure; imshow(foreground); title('Foreground');
se = strel('square', 3);
filteredForeground = imopen(foreground, se);
figure; imshow(filteredForeground); title('Clean Foreground');
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 150);
bbox = step(blobAnalysis, filteredForeground);
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
figure; imshow(result); title('Detected Cars');
>> videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');
videoPlayer.Position(3:4) = [650,400]; % window size: [width, height]
se = strel('square', 3); % morphological filter for noise removal
while ~isDone(videoReader)
frame = step(videoReader); % read the next video frame
% Detect the foreground in the current video frame
foreground = step(foregroundDetector, frame);
% Use morphological opening to remove noise in the foreground
filteredForeground = imopen(foreground, se);
% Detect the connected components with the specified minimum area, and
% compute their bounding boxes
bbox = step(blobAnalysis, filteredForeground);
% Draw bounding boxes around the detected cars
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
% Display the number of cars found in the video frame
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
step(videoPlayer, result); % display the results
end
release(videoReader); % close the video file
Can Anybody help me.

Answers (5)

Mohith Prabhu
Mohith Prabhu on 10 Oct 2018
With the R2018b release of MATLAB, you can deploy your MATLAB code on Raspberry Pi as a standalone executable.
Refer the Deploying MATLAB functions on Raspberry Pi for more information.

Walter Roberson
Walter Roberson on 18 Nov 2015
It appears to me that you need to convert your code into a Simulink model that uses a MATLAB Function block, and you can then use Simulink to generate the code for the Raspberry.

Nguyen Toan
Nguyen Toan on 30 Jan 2018
I build a matlab function to return matching point of tow image using detectSURFFeatures. Then i porting it to simulink modle using matlab function block. but has an error with message "detectSURFFeatures is not supported in Simulink." somebody help me!
  1 Comment
Walter Roberson
Walter Roberson on 30 Jan 2018
MATLAB Function Blocks are expected to be compiled and deployed to hardware. That is a potential problem for detectSURFFeatures and some other computer vision routines. Code generation to C/C++ is supported for detectSURFFeatures, but it does that by creating calls to the opencv library -- and opencv is not necessarily available for embedded hardware systems.
The design mechanism used to get around this would be to embed coder.ceval() calls in your MATLAB Function blocks that make calls to opencv routines, as you can install opencv on the raspberry https://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/
Another mechanism would be to use coder.extrinsic to make calls to routines. When those were run in Normal acceleration mode or the first kind of simulation acceleration for modeling on the host, then coder.extrinsic would look for a MATLAB routine with the given name, but for deployment to target, the coder would expect that you have supplied a library routine with that name.

Sign in to comment.


Madhu Govindarajan
Madhu Govindarajan on 30 Jan 2018
Here is my shot at the answer to your question.
Step 1) You will have to look at the documentation of all the functions you are using to see if they are code generation capable. Example, at the bottom of this page (https://www.mathworks.com/help/vision/ref/vision.foregrounddetector-system-object.html ) there is a section titled Extended capabilities. This will tell you if you can generate code usign MATLAB Coder for this function. Step 2) you will have to create a single function that does exactly what this script is doing but with the actual input images. Step 3) Use this tool along with MATLAB Coder to generate code that can be run on the hardware - https://www.mathworks.com/matlabcentral/fileexchange/62243-run-on-hardware
If it does work, please accept the answer as that will help others who might have need for a similar workflow.

Antonio  Ofogo
Antonio Ofogo on 24 May 2022
Hi there. I'm working on a space detection project in a parking lot, I wrote a matlab code to do it and it works perfectly on my machine but it is supposed to be deployed on Raspberry Pi 3, how do I do it please?
  1 Comment
Walter Roberson
Walter Roberson on 24 May 2022
https://www.mathworks.com/help/supportpkg/raspberrypiio/run-on-hardware.html

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!