error in rgb2gray and imshow
Show older comments
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);
Accepted Answer
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
1 Comment
Krithika G SASTRA
on 13 Apr 2022
Categories
Find more on Images in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!