Run matlab code in raspberry pi

조회 수: 34 (최근 30일)
vamshi krishna
vamshi krishna 2015년 11월 18일
댓글: Walter Roberson 2022년 5월 24일
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.

답변 (5개)

Mohith Prabhu
Mohith Prabhu 2018년 10월 10일
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 2015년 11월 18일
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 2018년 1월 30일
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
Walter Roberson
Walter Roberson 2018년 1월 30일
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.

댓글을 달려면 로그인하십시오.


Madhu Govindarajan
Madhu Govindarajan 2018년 1월 30일
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 2022년 5월 24일
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
Walter Roberson
Walter Roberson 2022년 5월 24일
https://www.mathworks.com/help/supportpkg/raspberrypiio/run-on-hardware.html

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Run on Target Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by