imbase = ['../text/images/']; % Get some images bi = imread([imbase,'circles.tif']); % Binary gs = imread([imbase,'cameraman.tif']); % Grayscale co = imread([imbase,'flowers.tif']); % RGB color [in inmap] = imread([imbase,'emu.tif']); % Indexed color % Let's look at their types and sizes whos figure(1); clf % Let's see how image command works for grayscale image image(gs) impixelinfo % Play with colormaps colormap(jet(256)) colormap(jet(64)) % Why is everything red? colormap(hot(256)) colormap(gray(256)) % Gray scale colormap(flipud(gray)) % Negative gray scale. What does flipud do? % Now try a color image figure(1); clf image(co) % Explore imshow function figure(1); clf imshow(gs) % Works fine for uint8 data % Now add some noise % Convert gs to double and add random component gs2 = double(gs)+100*randn(size(gs)); % Display the results imshow(gs2) % Major problems % Rescale m = min(min(gs2)); M = max(max(gs2)); s = 1/(M-m); gs2_scaled = s*(gs2-m); imshow(gs2_scaled) % Bitplanes c = imread('../text/images/caribou.tif'); % Get all eight bitplanes b0 = logical(bitget(c,1)); b1 = logical(bitget(c,2)); b2 = logical(bitget(c,3)); b3 = logical(bitget(c,4)); b4 = logical(bitget(c,5)); b5 = logical(bitget(c,6)); b6 = logical(bitget(c,7)); b7 = logical(bitget(c,8)); % ... and plot them figure(1);clf subplot(421);imshow(b0);title('b0') subplot(422);imshow(b1);title('b1') subplot(423);imshow(b2);title('b2') subplot(424);imshow(b3);title('b3') subplot(425);imshow(b4);title('b4') subplot(426);imshow(b5);title('b5') subplot(427);imshow(b6);title('b6') subplot(428);imshow(b7);title('b7') % Use of "for" loops for performing image operations [nr nc] = size(c); % Not necessary to pre-allocate output image, but a good idea if you know % the size. Tends to speeed tings up. bitplane3 = zeros(nr,nc); % Could use zeros(size(c)) if you wished % Use variables idxr and idxc to index fros and columns % Notes: % - Nested for loop structure % - Syntax of the loop for idxr = 1:nr for idxc = 1:nc bitplane3(idxr,idxc) = logical(bitget(c(idxr,idxc),3)); end end % Now the functional approach tic bitplane3a = bitplane(c,3); toc tic bitplane3a = bitplane_no_for(c,3); toc % Check the results max(max(bitplane3-bitplane3a))