% cdemo.m % Demonstrating how to transform from different color coordinates % (c) copyright 1997-2005 by Yu Hen Hu % created 9/15/97 % last updated: 9/22/2005 % clear all echo on [x,map]=imread('images/mysail.bmp'); % Matlab represent each image by an index value and a color map % associated with that index. Each row of the color map % contains the R,G,B value of that index. If a picture has 256 % colors, the map will contain at most 256 rows. % For example, x(1,1) = 99, and map(99,:)=[.2784 .2824 .3412]; % after converting this image to rgb format, r(1,1)=.2784, % g(1,1)=.2824, and b(1,1,) = .3412 oldmap=map; figure(1),imshow(x,map) pause % Now let use work only on the first five by five block of the image. bsize=input('demostration size = (default: 5) '); if isempty(bsize), bsize=5; end yx=x(1:bsize,1:bsize); bsize2=bsize*bsize; rgb=ind2rgb(yx,map) % You may verify [r(1,1) g(1,1) b(1,1)]=map(99,:)! pause % Now let us convert the rgb image to YIQ coordinate % For convenience, we convert the 5 by 5 matrices of r,g,b into % row vectors using the command "reshape" rv=reshape(rgb(:,:,1),1,bsize2); gv=reshape(rgb(:,:,2),1,bsize2); bv=reshape(rgb(:,:,3),1,bsize2); % Then construct a 3 by bsize*bsize matrix v = [rv; gv; bv]; % Next multiply v by the 3 by 3 transformation matrix in % eq. 4.6.6 (pp.228 of the text book) yiq=[.299 .587 .114; .596 -.275 -.321; .212 -.523 .311]*v; % next convert each row of yiq into bsize by bsize matrices yi=reshape(yiq(2,:),bsize,bsize); yq=reshape(yiq(3,:),bsize,bsize); y = reshape(yiq(1,:),bsize,bsize) pause % Now to check y is indeed the intensity, let us use image processing % tool box command Igray=rgb2gray(rgb) % You can verify that y and Igray are the same. % Our next goal is to convert the rgb colormap to hsi (hsv) colormap pause hsv=rgb2hsv(map); % Hue, Saturation and Value % Hue is actually cos(H) in the text book(?) % Let us change the saturation values of the color map to 1 and % convert it back to rgb [mh,nh]=size(hsv); newmap = hsv2rgb([hsv(:,1) ones(mh,1) hsv(:,3)]); set(gcf,'ColorMap',newmap) imshow(x)