MATLAB: Undefined function or variable ‘img’. The first assignment to a local variable determines its class.

code genrationimage proessingvariable definition

Hello, I am trying to convert matlab code to simulink model for image processing project. I am adding the R,G,B outputs from android camera and taking the sum as the image input – 'img_in' in my function block and doing the processing. here is a snippet to show how i tried declaring the image, and other variables to 0.
function lambda = my_function(img_in)
img = zeros(size(img_in));
Ro = 0;
Co = 0;
interestRo = 0;
interestCo = 0;
width = 0;
height = 0;
BW = zeros(height+1,width+1);
% read input image
img = imread(img_in);
img = imresize(img, [480 640]);
% rgb to grayscale conversion
img = rgb2gray(img);
[Ro, Co] = size(img);
%%Dividing in 4*4 and taking the lower mid one
interestRo = round(Ro/10);
interestCo = round(Co/4);
width = 10*interestRo;
height = 2*interestCo;
BW = imcrop(img,[interestCo interestRo width height]);
But I still get lost of errors of the type:
### Generating code into build folder :----Simulink\proj_grt_rtw
The file name or URL argument must be a character vector.
Function 'MATLAB Function' (#23.199.213), line 14, column 7:
"imread(img_in)"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'img'. The first assignment to a local variable determines its class.
Function 'MATLAB Function' (#23.231.234), line 15, column 16:
"img"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'img'. The first assignment to a local variable determines its class.
Function 'MATLAB Function' (#23.293.296), line 17, column 16:
"img"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'img'. The first assignment to a local variable determines its class.
Function 'MATLAB Function' (#23.316.319), line 19, column 17:
"img"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'Ro'. The first assignment to a local variable determines its class.
Function 'MATLAB Function' (#23.301.303), line 19, column 2:
"Ro"
Please help me in understanding what I could do to get rid of the errors.
Thanks.

Best Answer

Simulink does not offer characters as one of the signal types, so it is not possible that img_in is a character vector. The trick to handling character vectors in Simulink is to convert them to uint8 or uint16 on the bus, and then char() those when you need to use them as a string.
Your code has
img = zeros(size(img_in));
which allocates img according to the size of img_in . That might be okay of img_in is an array. But then you have
img = imread(img_in);
and for that, img_in needs to be a character vector. If you were going to have a call that returned an image, you would need to have previously assigned img according to the maximum size that could be returned, or you would need to use coder.varsize()
"I am adding the R,G,B outputs from android camera"
If the implication is that you are intending to run this code on the android itself, then you have a problem.
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
  • Supports reading of 8-bit JPEG images only. The input argument filename must be a valid absolute path or relative path.
  • This function generates code that uses a precompiled, platform-specific shared library (Image Processing Toolbox).
  • In a MATLAB Function block, the input argument filename must be a compile-time constant.
This leads us to question about the platform-specific shared library, for which we look at https://www.mathworks.com/help/images/understanding-code-generation-with-image-processing-toolbox.html and we see there,
"Some functions generate C code that use a platform-specific shared library. The Image Processing Toolbox uses this shared library approach to preserve performance optimizations, but this limits the platforms on which you can run this code to only platforms that can host MATLAB. "
In short, you cannot imread() on Android.
But...
"I am adding the R,G,B outputs from android camera"
there is nothing in this code that reads from Android camera. Are you sure that you are not passing in img_in as a 3D array, in which case you should not imread() it??
img = imresize(img, [480 640]);
That resize could be a problem if the original image was not at least 480 x 640 -- you can only expand an array to as large as the first assignment to the variable, unless you use coder.varsize