error in rgb2gray and imshow
6 views (last 30 days)
Show older comments
Krithika G SASTRA
on 12 Apr 2022
Commented: Krithika G SASTRA
on 13 Apr 2022
a=imread('C:\Users\amirg\OneDrive\Pictures\butterfly.jpg');
gray=rgb2gray(a);
[row,col]=size(a);
subplot(3,3,1);
imshow(gray);
title('original image');
c=zeros(row,col,8);
for k=1:8
for i=1:row
for j=1:col
c(i,j,k)=bitget(a(i,j),k);
end
end
subplot(3,3,k+1);
imshow(c(:,:,k));
title(['Bit plane ',num2str(k-1)]);
end
error
Attempt to execute SCRIPT gray as a function:
C:\Users\amirg\OneDrive\Documents\MATLAB\gray.m
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)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in plane (line 5)
imshow(gray);
0 Comments
Accepted Answer
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.
HTH
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.
Fix:
rgbImage=imread('peppers.png');
grayImage=rgb2gray(rgbImage);
[rows, columns] = size(rgbImage);
subplot(3,3,1);
imshow(grayImage);
title('original image');
bitPlaneImage = zeros(rows,columns,8);
for bitIndex = 1 : 8
for row=1:rows
for col=1:columns
bitPlaneImage(row,col,bitIndex)=bitget(rgbImage(row,col),bitIndex);
end
end
subplot(3,3,bitIndex+1);
imshow(bitPlaneImage(:,:,bitIndex));
title(['Bit plane ',num2str(bitIndex-1)]);
drawnow; % Force it to paint screen immediately.
end
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!