MATLAB Answers

Matlab & Simulink Coder: Using a polygon shaped ROI to mask an input image

43 views (last 30 days)
Lars
Lars on 27 Mar 2020 at 7:41
Edited: Lars about 15 hours ago
Hi Everyone,
I am trying to use Simulink to take a greyscale image and mask a region of interest (polygonal). My goal is to use these values as input for for my control system. The code should be exported with Simulink Coder to a RT system afterwards.
My first idea was to use the Computer Vision Toolbox but I can't come up with an appropriate function here. My second try was to use a MATLAB function and use poly2mask() to extract the region of interest, but it's obviously not supported for Code Generation.
Any ideas how I could approach my problem next?
Thanks,
Lars

  0 Comments

Sign in to comment.

Answers (1)

Lars
Lars about 15 hours ago
Edited: Lars about 15 hours ago
In the meantime I came up with the following code
function extractedTemps = extractPolygon(tempImage, vertices) %#codegen
% Inputs: Image & vertices(x, y)
% Get image size
[iY, iX] = size(tempImage);
% Coordinates to all pixel
[x, y] = meshgrid(1:iX, 1:iY);
% Check if in polygon
[in] = inpolygon(x,y, vertices(1,:), vertices(2,:));
% Extract Temperatures
coder.varsize('extractedTemps',[21504 1]);
extractedTemps = tempImage(sub2ind(size(tempImage),y(in), x(in)));
Unfortunately diagnostics still gives me the follow error:
Saturate on overflow detected. In MATLAB Function 'ExtractPolygon': function extractedTemps = extractPolygon(tempImage, vertices) %#codegen
Any idea whats happening here?

  0 Comments

Sign in to comment.

Sign in to answer this question.