y i m getting Subscripted assignment dimension mismatch error?

clear all
clc
a=imread('vc1.gif');
a=double(a);
r=input('which bit image do you want to see 1=MSB 8=LSB');
[row col]=size(a);
for x=1:1:row
for y=1:1:col
c=dec2bin(a(x,y),8);
d=c(r);
w(x,y)=double(d);
if w(x,y)==49
w(x,y)=255;
else
w(x,y)=0;
end
end
end
figure(1)
imshow(uint8(a))
figure(2)
imshow(uint8(w))

1 Kommentar

Instead of that whole long, inefficient double for loop, you really only need one line:
bitPlaneImage = bitand(grayImage, 2^bp);
See how I do it in my full-blown demo below.

Antworten (2)

Wayne King
Wayne King am 30 Jan. 2013
I do not have trouble running this code with the following (although I get a display warning at the end)
a = imread('ngc6543a.jpg');
Can you please indicate what the size of a is after your call to imread()
Image Analyst
Image Analyst am 30 Jan. 2013
Even though that might seem intuitive, that's an inefficient way to do it. Here, try it my way - run my demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of Original Grayscale Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's use this spot for the output image.
subplot(2, 3, 5);
for bp = 0 : 7
% Compute the bit plane image.
bitPlaneImage = bitand(grayImage, 2^bp);
% Display it as pure black and white (not gray scale).
imshow(bitPlaneImage, []);
caption = sprintf('You are now viewing bitplane %d.', bp);
title(caption, 'FontSize', fontSize);
% If it's not the last bit plane, ask them if they want to continue.
if bp ~= 7
message = sprintf('%s\nDo you want to continue', caption);
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
break;
end
end
end
msgbox('Done with demo');

Diese Frage ist geschlossen.

Gefragt:

am 30 Jan. 2013

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by