Undefined function or variable 'img'. The first assignment to a local variable determines its class.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Anuja Vats
le 8 Oct 2017
Commenté : Anuja Vats
le 9 Oct 2017
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.
1 commentaire
Réponse acceptée
Walter Roberson
le 8 Oct 2017
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
3 commentaires
Walter Roberson
le 8 Oct 2017
Q1) the result of the add is going to be a 2D array the same size as an image plane. You would not apply imread() to that, as it is already an image. Your next step of rgb2gray would not make any sense as the result is not a color image.
There might be some point in adding the channels if you were doing luminance correction, but in that case you would be wanting to pass both the original panes and the sum on to the next step.
Q2) Generally, yes, but there are other approaches that have advantages, such as
assert(size(img_in,1) <= 640, 'image too tall')
assert(size(img_in,2) <= 480, 'image too wide')
Q3) You can resize an image to larger, as long as the output variable has been initialized to be large enough to hold the result. Your line
img = imresize(img, [480 640]);
had the potential for taking the existing img and making it larger. But if you had done
bimg = zeros(480, 640);
bimg = imresize(img, [480 640]);
then there would not be a problem.
Do be careful about 640 x 480 compared to 480 x 640.
One thing I have to wonder is whether that Add block should instead just be a Concatenate block ?
If the output is only going into the MATLAB Function block then you might as well keep the channels as separate signals and combine them inside the function if you need to.
If you are doing summation, then what is your plan to deal with overflow of uint8 ?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Computer Vision with Simulink dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!