error in rgb2gray and imshow
7 views (last 30 days)
Krithika G SASTRA on 12 Apr 2022
title(['Bit plane ',num2str(k-1)]);
Attempt to execute SCRIPT gray as a function:
Error in images.internal.imageDisplayValidateParams (line 45)
common_args.Map = gray(256);
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 240)
Error in plane (line 5)
Bjorn Gustavsson on 12 Apr 2022
Edited: Bjorn Gustavsson on 12 Apr 2022
It seems you have named a script or function gray.m in your C:\Users\amirg\OneDrive\Documents\MATLAB\ directory. That will cause problems, since this is the file matlab will find first when you call gray and expect to get the call to the built-in function gray. Check which order you have the gray-files like this:
which gray -all
In order to resolve this naming-conflict, you will have to rename your gray-file to something like my_gray.m. That way it will not shadow the built-in function.
One thing you might consider doing is to put all your files to the end of matlab's search-path. That way you will not break the functionality of matlab and its built-in functions - and you will rapidly realise when you've managed to name one of your files to something already used by matlab.
More Answers (1)
Image Analyst on 12 Apr 2022
gray() is a function used to build a gray scale colormap. Never use it, or any other built-in functions, as names for your variables.
Also if you have your own functions, like in this case, it could cause problems. Rename your gray.m to some other name.
It's best to use descriptive variable names, instead of single letters like a and c, lest your code look like an impenetrable alphabet soup mess of a program that's hard to read and maintain.
[rows, columns] = size(rgbImage);
bitPlaneImage = zeros(rows,columns,8);
for bitIndex = 1 : 8
title(['Bit plane ',num2str(bitIndex-1)]);
drawnow; % Force it to paint screen immediately.